Vikneshwar
Vikneshwar

Reputation: 1033

How to get the first value inside a drop down list as select?

I'm doing a project in asp.net using 3-tier architecture. Using various trial and errors i have successfully binded the values inside a column of a table inside the dropdownlist.

The thing that I'm stuck with right now is that during the pageload event i get the values inside the dropdown box that are available in my table. But the real problem is i have to get the first value inside the dropdownlist as "Select" or "Choose any One". I'm Not able to do this.

Heres my code

        DataTable datatable = new DataTable();
       _bl = new HomeFileUploadBL();
        voo = _bl.SelectNameOfDocument();
        datatable = voo.DocumentName;
        ddlDocument.DataSource = datatable;
        ddlDocument.DataTextField = datatable.Columns["Name"].ToString();
        ddlDocument.DataValueField = datatable.Columns["Name"].ToString();
        ddlDocument.DataBind();

notations are: ddlDocument refers to DropDownList, datatable refers to DataTable, _bl refers to the object of business class, voo.Document refers to the value object class,Document is of the type DataTable.

As soon as the page loads the first value from the table is available inside the dropdownlist.

Also when i click on the dropdownlist my first value shoul be "Select" or "Choose any one"

Am im missing that is really obvious. ?

Upvotes: 1

Views: 1380

Answers (3)

Yograj Gupta
Yograj Gupta

Reputation: 9869

Add this after DataBind

ddlDocument.Items.Insert(0, new ListItem("Select", "0"));

OR

ddlDocument.Items.Insert(0, new ListItem("Choose any one", "0"));

Upvotes: 1

Mayank Pathak
Mayank Pathak

Reputation: 3681

Yes you missed adding select. That you can do manually after binding dropdown it should be like this. I have modified your code. try this one..

DataTable datatable = new DataTable();
    _bl = new HomeFileUploadBL();
    voo = _bl.SelectNameOfDocument();
    datatable = voo.DocumentName;
    ddlDocument.DataSource = datatable;
    ddlDocument.DataTextField = datatable.Columns["Name"].ToString();
    ddlDocument.DataValueField = datatable.Columns["Name"].ToString();
    ddlDocument.DataBind();
    ddlDocument.Items.Insert(0, new ListItem("Select", "0"));

Upvotes: 1

Gowtham
Gowtham

Reputation: 252

Hi try something like this...

DataRow dtRow = null;  if (ddlDocument.Rows.Count > 0)
    {
        dtRow = ddlDocument.NewRow();
        dtRow[0] = 0;
        dtRow[1] = "Select All";
        ddlDocument.Rows.InsertAt(dtRow, 0);
        ddlDocument.AcceptChanges();

    }

Upvotes: 1

Related Questions