Search This Blog

Thursday, August 27, 2009

Bind DropdownList by an Enum

i have an enum like using System;using System.Collections.Generic;using System.Text; namespace ConfigurationManager{ public static class ConfigurationEnumerators
{
public enum EventTypeValues
{
Red= 1,
Green= 2,
Yellow= 3,
White= 4,
Blue = 5,
Violate = 0
}

}
}
This is a color type enum.Now i am going to populate a dropdown list by this Color Enumarator ddlColorType.Items.Insert(0, "------Select------");
foreach (string str in Enum.GetNames(typeof(ConfigurationEnumerators.EventTypeValues)))

{
ddlCarType.Items.Insert((int)Enum.Parse(typeof( ConfigurationEnumerators.EventTypeValues), str ),str.ToString());
}

The dropdownlist is populated by the enum with the string at its TextField and the int value at its Index fieldNow it is possible to collect the selected dropdownlist value by ddlColorType.SelectedIndex property

2 comments:

Supriyo said...

yes..i got it what i wanted..thank you.

Subhamay Sur said...

thank you