How to bind an Enum with its value to the DropDownList in ASP.NET?

Posted by SheoNarayan under Error and Solution on 6/10/2011 | Points: 10 | Views : 20057 | Status : [Administrator] | Replies : 3

You must have faced this type of scenario in your day to day life where you have to bind the enums into the DropDownList or ListBox control. A solution is given to this link http://stackoverflow.com/questions/61953/how-do-you-bind-an-enum-to-a-dropdownlist-control-in-asp-net however it deosn't bind the dropDownList items value. It just binds the text and value of the item as the enum items name (at least thats what I got).

I have improved this code little further and below code brings the enum name as the item text and enum value as item value in the DropDownList.

Array itemValues = System.Enum.GetValues(typeof(NewsCategory));
Array itemNames = System.Enum.GetNames(typeof(NewsCategory));

for (int i = 0; i <= itemNames.Length - 1; i++)
{
var value = (int)(NewsCategory)Enum.Parse(typeof(NewsCategory), itemValues.GetValue(i).ToString(), true);
ListItem item = new ListItem(itemNames.GetValue(i).ToString(), value.ToString());
dropCategory.Items.Add(item);
}


In the above code, notice the value variable that I have retrieved from the enum by parsing it and the same is being set as the value for the ListItem.

If you have any better code than this, do respond to this thread with your code.

Hope this helps.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com



Responses

Posted by: Ndebata on: 6/12/2011 [Member] Starter | Points: 25

Up
0
Down
Dear Sir,
One way you can create listitems like this.

foreach (var item in typeof(MyEnum).GetFields())
{
if (item.FieldType == typeof(MyEnum))
DropDownList1.Items.Add(new ListItem { Text = item.Name, Value = item.GetRawConstantValue().ToString() });
}

Also one more thing as I could see you are trying to store the values in int, it will not work for long enum such as

public enum MyEnum:long
{
First=244444444444,
Second=8,
Third=5
}

please advice me if I am wrong.

Thanks and Regards,
Narayan



SheoNarayan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhu.b.rokkam on: 8/3/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi You can also do this way ..

public enum Colors

{
Red = 1,
Orange = 2,
Green = 3,
Black = 4,
None = 0
}

and to add this to a dropdown you can do as
foreach(int val in Enum.GetValues(typeof(Colors)))
{
ddlcolors.Items.Add(new ListItem(Enum.GetName(typeof(Colors), val)))
}




Thanks and Regards
Madhu

SheoNarayan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 8/4/2011 [Member] Bronze | Points: 25

Up
0
Down
Hello sir,
here is the code Hope this will helps you,

cmbEnum.Items.Clear();
Dictionary<int, string> DicEnum = new Dictionary<int, String>();

for int i=0;i<Enum.GetNames(typeof(MyEnum)).Length;i++)
{
DicEnum.Add(Enum.GetValues(typeof(MyEnum))[i],Enum.GetNames(typeof(MyEnum))[i]);
}

if (DicEnum.Count > 0)
{
cmbEnum.DataSource = DicEnum;
cmbEnum.DataTextField = "Value";
cmbEnum.DataValueField = "Key";
cmbEnum.Databind();
}

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

SheoNarayan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response