We can use two methods bind data to a drop down. One we can loop through the data source, add each item to the drop down list and add the custom items to the dropdown.
private void BindMethod1()
{
DataTable DtData = new DataTable();
DtData = createBindDatatable();
DropDownList1.Items.Add(new ListItem("Select Below","0"));// Adding the custom item
foreach (DataRow item in DtData.Rows)
{
DropDownList1.Items.Add(new ListItem(item["Country"].ToString(), item["ID"].ToString()));
}
}
Another method is we can assign the data source to the drop down list datasource property and set the data text and value properties.
private void BindMethod2()
{
DataTable DtData = new DataTable();
DtData = createBindDatatable();
DropDownList1.DataSource = DtData;
DropDownList1.DataTextField = "Country";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("Select Below", "0"));// Adding the custom item
}
Monday, December 10, 2012
Adding Custom Item to DataBound Dropdown List
Subscribe to:
Post Comments (Atom)
No comments:
Write comments