Monday, January 27, 2014

Using a Dictionary as the DataSource for a DropDownList

It is possible to use a Dictionary or even Dictionary<string> as the DataSource for a DropDownList directly, without having to iterate through the the Dictionary's KeyValuePair collection and populate the DropDownList's Items collection manually.

The text member is "value" and the value member is "key" when using one of these types of objects to bind to a DropdownList.

Dictionary<byte> dicTable = new Dictionary<byte>();
dicTable.Add(1, "One");
dicTable.Add(2, "Two");
dicTable.Add(3, "Three");
dicTable.Add(4, "Four");
cmbDropDownList.DataSource = dicTable;
cmbDropDownList.DataTextField = "Value";
cmbDropDownList.DataValueField = "Key";
cmbDropDownList.DataBind();

Submit this story to DotNetKicks

No comments:

Post a Comment