Tuesday, 25 December 2012 13:56

How to Populate Dropdown from Dataset

Written by 
Rate this item
(0 votes)

this post shows how to populate dropdown list from
dataset.it's done by the following generic method-

you need to pass the dropdownlist name,its value
field name,text field name,the dataset and the
method will automatically fill the drop down list.

the method is -

public void PopulateList(DropDownList list,
string valueField, string textField, DataSet dataset)
{
if( dataset == null || dataset.Tables.Count == 0 ||
list == null ) return;
//Enable to add empty item to list
for (int irow = 0; irow < dataset.Tables[0].Rows.Count; irow++)
{
list.Items.Add(new ListItem(Convert.ToString
(dataset.Tables[0].Rows[irow][textField]),
Convert.ToString(dataset.Tables[0].Rows[irow][valueField])));
}}

Read 3052 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.

Latest discussions

  • No posts to display.