Monday, August 31, 2009

Fill dropdownlist from enum

as we all know that enum is very useful and sometimes we need to show values of enum on dropdownlist. by following way we can show data of enum in dropdownlist
enum CustomColors
{
BLACK = -1,
RED = 7,
GREEN = 14,
UNKNOWN = -13
}

protected void BtnFillddl_Click(object sender, EventArgs e)
{
EnumToListBox(typeof(DayOfWeek), DropDownList1);

EnumToListBox(typeof(CustomColors), ListBox1);
}
static public void EnumToListBox(Type EnumType, ListControl TheListBox)
{
Array Values = System.Enum.GetValues(EnumType);

foreach (int Value in Values)
{
string Display = Enum.GetName(EnumType, Value);
ListItem Item = new ListItem(Display, Value.ToString());
TheListBox.Items.Add(Item);
}
}

Share This!


No comments:

Powered By Blogger · Designed By Seo Blogger Templates