I want to bind the value and the name of the enum to the dropdownlist. I've been looking around about how to do this, and found some examples, but it's not working for me for some reason:
Here are some methods I wrote to bind enum's to dropdownlists
/// <summary>
/// Pass in a DropDownList and typeof(YourEnum) as parameters to auto-bind a dropdownlist to an enum
/// </summary>
/// <param name="ddl">ddlMyDropDownList</param>
/// <param name="enumType">typeof(MyEnum)</param>
/// <param name="retainSelected">true/false indicating if the currently selected item should remain selected</param>
public static void BindEnum2DropDownList(DropDownList ddl, Type enumType, bool retainSelected)
{
// retain the currently selected item if possible
string currentlySelectedValue = string.Empty;
if(retainSelected)
currentlySelectedValue = ddl.SelectedValue;
ddl.DataSource = WilsToolbox.EnumHelper.GetListItemsFromEnum(enumType);
ddl.DataBind();
// in the event that there was a selected item, keep it.
if (currentlySelectedValue != string.Empty && retainSelected == true)
{
ddl.ClearSelection();
ddl.Items.FindByText(currentlySelectedValue).Selected = true;
}
}
public static ListItemCollection GetListItemsFromEnum(Type enumType)
{
// container to be returned
ListItemCollection items = new ListItemCollection();
// break down the enumerator items into key/value pairs
string[] names = Enum.GetNames(enumType);
Array values = Enum.GetValues(enumType);
// piece together the key/pairs into the listitem collection
for (int i = 0; i <= names.Length - 1; i++)
{
items.Add(new ListItem(names[i], values.GetValue(i).ToString()));
}
// return it
return items;
}
Please click 'Mark as Answer' if reply assisted you.
"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams
Why not use like this to be able pass every listcontrol :
public static void BindToEnum(Type enumType, ListControl lc)
{
// get the names from the enumeration
string[] names = Enum.GetNames(enumType);
// get the values from the enumeration
Array values = Enum.GetValues(enumType);
// turn it into a hash table
Hashtable ht = new Hashtable();
for (int i = 0; i < names.Length; i++)
// note the cast to integer here is important
// otherwise we'll just get the enum string back again
ht.Add(names[i], (int)values.GetValue(i));
// return the dictionary to be bound to
lc.DataSource = ht;
lc.DataTextField = "Key";
lc.DataValueField = "Value";
lc.DataBind();
}
Apples
Member
75 Points
452 Posts
Bind dropdownlist to enum
Jun 02, 2008 03:24 PM|LINK
I want to bind the value and the name of the enum to the dropdownlist. I've been looking around about how to do this, and found some examples, but it's not working for me for some reason:
public enum Salutations { Mr = 1, Ms = 2, Mrs = 3 } string[] names = Enum.GetNames(typeof(Salutations)); Array values = Enum.GetValues(typeof(Salutations)); for(int i=0; inew ListItem(names[i], values[i].ToString()); ddlSalutations1.Items.Add(item); }The problem is trying to access the value in values. I can't do it for an Array. How can I modify this?
pixelsyndica...
Star
7826 Points
1344 Posts
Re: Bind dropdownlist to enum
Jun 02, 2008 03:44 PM|LINK
Here are some methods I wrote to bind enum's to dropdownlists
/// <summary> /// Pass in a DropDownList and typeof(YourEnum) as parameters to auto-bind a dropdownlist to an enum /// </summary> /// <param name="ddl">ddlMyDropDownList</param> /// <param name="enumType">typeof(MyEnum)</param> /// <param name="retainSelected">true/false indicating if the currently selected item should remain selected</param> public static void BindEnum2DropDownList(DropDownList ddl, Type enumType, bool retainSelected) { // retain the currently selected item if possible string currentlySelectedValue = string.Empty; if(retainSelected) currentlySelectedValue = ddl.SelectedValue; ddl.DataSource = WilsToolbox.EnumHelper.GetListItemsFromEnum(enumType); ddl.DataBind(); // in the event that there was a selected item, keep it. if (currentlySelectedValue != string.Empty && retainSelected == true) { ddl.ClearSelection(); ddl.Items.FindByText(currentlySelectedValue).Selected = true; } } public static ListItemCollection GetListItemsFromEnum(Type enumType) { // container to be returned ListItemCollection items = new ListItemCollection(); // break down the enumerator items into key/value pairs string[] names = Enum.GetNames(enumType); Array values = Enum.GetValues(enumType); // piece together the key/pairs into the listitem collection for (int i = 0; i <= names.Length - 1; i++) { items.Add(new ListItem(names[i], values.GetValue(i).ToString())); } // return it return items; }"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams
ecbruck
All-Star
98783 Points
9691 Posts
Moderator
Re: Bind dropdownlist to enum
Jun 02, 2008 03:57 PM|LINK
Here's an example:
ASPX
CODE-BEHIND
Microsoft MVP - ASP.NET
gbogea
Contributor
4019 Points
576 Posts
Re: Bind dropdownlist to enum
Jun 02, 2008 04:04 PM|LINK
You can create a method to help you convert your enum into a hashtable and then bind it to the ddl:
protected void Page_Load(object sender, EventArgs e) { Hashtable ht = GetEnumForBind(typeof(Salutations)); DropDownList1.DataSource = ht; DropDownList1.DataTextField = "value"; DropDownList1.DataValueField = "key"; DropDownList1.DataBind(); } public Hashtable GetEnumForBind(Type enumeration) { string[] names = Enum.GetNames(enumeration); Array values = Enum.GetValues(enumeration); Hashtable ht = new Hashtable(); for (int i = 0; i < names.Length; i++) { ht.Add(Convert.ToInt32(values.GetValue(i)).ToString(), names[i]); } return ht; }-----------------
Please 'Mark as Answer' the post(s) that helped you
Apples
Member
75 Points
452 Posts
Re: Bind dropdownlist to enum
Jun 02, 2008 06:13 PM|LINK
That worked great, thank you.
mattaniah
Member
5 Points
10 Posts
Re: Bind dropdownlist to enum
Jul 10, 2008 01:34 PM|LINK
Damn you are good. Thanks Alot.
LastPhoenix
Member
15 Points
16 Posts
Re: Bind dropdownlist to enum
Dec 02, 2009 08:38 AM|LINK
Why not use like this to be able pass every listcontrol :
public static void BindToEnum(Type enumType, ListControl lc) { // get the names from the enumeration string[] names = Enum.GetNames(enumType); // get the values from the enumeration Array values = Enum.GetValues(enumType); // turn it into a hash table Hashtable ht = new Hashtable(); for (int i = 0; i < names.Length; i++) // note the cast to integer here is important // otherwise we'll just get the enum string back again ht.Add(names[i], (int)values.GetValue(i)); // return the dictionary to be bound to lc.DataSource = ht; lc.DataTextField = "Key"; lc.DataValueField = "Value"; lc.DataBind(); }And use just as simple as :
Anithaol
Member
5 Points
4 Posts
Re: Bind dropdownlist to enum
Feb 09, 2010 03:55 PM|LINK
Hi,
That was a Good Example to bind to any ListControl
neilinfl
Member
2 Points
3 Posts
Re: Bind dropdownlist to enum
Oct 16, 2010 07:46 PM|LINK
I like it! But what is I wanted to decorate my enumeration with a system.componantmodel.description and use the description as the value:
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">public enum Divisions : int</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> [Description("Restaurant")]</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> Restaurant= 0,</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> [Description("Residential")]</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> Residential = 1,</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> [Description("Commercial")]</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> Commercial = 2</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div>public enum Divisions : int
{
[Description("Fruit & Vegetables")]
Vegetables= 0,
[Description("Beef")]
Beef = 1,
[Description("Poultry")]
Poultry= 2
}
So the key would be 0 and the value would be Fruit and Vegetables