Binding a Dictionary to a Drop Down List

Binding data from a Dictionary is not so challenging as “Key” and “Value” pairs can be used for the drop down list properties. Following code samples show the results after assigning data source to the drop down list.

Before

Dictionary genderDictionary = new Dictionary();
genderDictionary.Add(1, "Male");
genderDictionary.Add(2, "Female");
genderDictionary.Add(3, "Transgender");

this.ddlGender.DataSource = genderDictionary;
this.ddlGender.DataBind();

Before Assign Key Value Pair





After

this.ddlGender.DataSource = genderDictionary;
this.ddlGender.DataTextField = "Value";
this.ddlGender.DataValueField = "Key";
this.ddlGender.DataBind();

After Assign Key Value Pair

Advertisement

Author: Indika Abayarathne

MSc in IT [University of Colombo] Solutions Architect | Consultant Technologies: Power Platform | Dynamics CE | Azure

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: