Click here to Skip to main content
15,037,009 members
Articles / Web Development / ASP.NET
Posted 21 Dec 2011

Tagged as


9 bookmarked

Binding DropDownList Using List Collection, Enum and DataSet in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.88/5 (12 votes)
21 Dec 2011CPOL
Binding DropDownList Using List Collection, Enum and DataSet in ASP.NET
We can bind DropDownList in different ways by using List, Dictionary, Enum and DataSet.

To bind DropDownList, we need to set some properties:

  • DataSource
  • DataValueField
  • DataTextField

Let's see how to use these properties to bind DropDownList.

Binding DropDownList with List

In this case, the DropDownList both 'Value' and 'Text' field are same.
DropDownList ddl = new DropDownList();
List<string> countries = new List<string>();
ddl.DataSource = countries;

Binding DropDownList with Dictionary

Dictionary<string, string> States = new Dictionary<string, string>();
States.Add("-1","-Select State-");
States.Add("AP", "Andhra Predesh");
States.Add("KA", "Karnataka");
States.Add("TN", "Tamilnadu");
States.Add("KL", "Kerala");
ddl.DataSource = States;
ddl.DataValueField = "Key";
ddl.DataTextField = "Value";

Binding DropDownList with DataSet

My DataSet contains a Class table(class_id,class_name,description).
ddl.DataSource = dataset.Tables[0].DefaultView;
ddl.DataValueField = "class_id"; 
ddl.DataTextField = "class_name";
ListItem item = new ListItem("-Select Class-", "-1");
ddl.Items.Insert(0, item);

Binding DropDownList with Enum

Let’s take Countries ‘enum’ as follows:
enum enCountries:int{India=0,USA,UK,UAE};

Let's see how to bind the DropDownList With Enum:
ddlEnumBind.Items.Add("--Select Country--");
//get enum items to get the respective enum value 
string[] enumNames=Enum.GetNames(typeof(enCountries));
foreach (string item in enumNames)
    //get the enum item value
    int value = (int)Enum.Parse(typeof(enCountries), item);
    ListItem listItem = new ListItem(item, value.ToString());


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rajesh Kumar Chekuri
Software Developer (Senior)
United States United States
If There Is Any suggestions Mail me

Best Regards
Rajesh Kumar Chekuri

Comments and Discussions mvc4 and WCF Pin
asma288129-Mar-13 4:41
Memberasma288129-Mar-13 4:41 
GeneralMy vote of 5 Pin
zeego12-Sep-12 20:33
Memberzeego12-Sep-12 20:33 
SuggestionAnother Easy method for country names in dropdownlist Pin
dharwishnasar11-Jul-12 21:07
Memberdharwishnasar11-Jul-12 21:07 
checkout this link. it is easy to learn and easy to understand
GeneralReason for my vote of 5 Good.. Refreshing Basics,, Pin
rahulpriyan26-Dec-11 21:33
Memberrahulpriyan26-Dec-11 21:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.