This post illustrate you about populating the dropdown list from a dataset. Just pass your dropdownlist and value field name(database column name) and text field name (database column name) and dataset in this method and it will automatically populate the drop down list.
this is very generic method to populate combobox and used for number of dropdown list.
public void PopulateCombo(DropDownList oList, string sValueField, string sTextField, DataSet oDs)
{
if( oDs == null || oDs.Tables.Count == 0 || oList == null ) return;
//Enables us to add an empty item to the list
for (int irow = 0; irow < oDs.Tables[0].Rows.Count; irow++)
{
oList.Items.Add(new ListItem(Convert.ToString(oDs.Tables[0].Rows[irow][sTextField]), Convert.ToString(oDs.Tables[0].Rows[irow][sValueField])));
}
}
How to Use this method:
PopulateCombo(ddlDivision, "ID_DIVISION", "NAME", oDs);
Just call this method and pass these parameters:
ddlDivision : dropdown list
ID_DIVISION: dropdown value field name coming from database column name
NAME: dropdown text field name coming from database column name
oDs: our dataset which have the ID_DIVISION and NAME columns.
nice example
ReplyDelete