Saturday, May 28, 2011

How To Bind DropDownList Using List Collection and DataSet in Asp.Net

0 comments 5/28/2011
Sign up to receive our site updates!
We can bind DropDownList in Different ways by using List, Dictionary and DataSet.

To bind DropDownList we need to set some properties
DataSource
DataValueField
DataTextField
Let 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>();
countries.Add("USA");
countries.Add("India");
ddl.DataSource = countries;
ddl.DataBind();


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";
ddl.DataBind();

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";
ddl.DataBind();
ListItem item = new ListItem("-Select Class-", "-1");
ddl.Items.Insert(0, item);

Happy Coding
Your Ad Here

0 comments:

Respects for your's Questions & Opinions

  • Friends
  •  

    Recent Posts

    New Comments

    Copyright 2008 All Rights Reserved Tech Tasks Template by Rajesh Kumar Chekuri

    Share
    Me
    Twitter Facebook Google +1 Pinterest Delicious Digg Stumbleupon More

    Connect to TT Facebook Google +1
    Back to Top