Skip to content

Instantly share code, notes, and snippets.

@findel
Created February 1, 2012 12:13
Show Gist options
  • Select an option

  • Save findel/1716775 to your computer and use it in GitHub Desktop.

Select an option

Save findel/1716775 to your computer and use it in GitHub Desktop.
Looping through enumerator to bind to DropDownList
// My enumerator
public enum MyKind
{
None = 0,
MyFirstValue = 1,
MySecondValue = 2,
MyThirdValue = 3
}
// My extention methods for the enumerator
public static class MyKindExtensions
{
public static string GetName(this MyKind source)
{
string name = "";
switch(source)
{
case MyKind.MyFirstValue:
name = "The First Value";
break;
case MyKind.MySecondValue:
name = "The Second Value";
break;
case MyKind.MyThirdValue:
name = "The Third Value";
break;
case MyKind.None:
default:
name = "Value not available";
break;
}
return name;
}
public static string GetValueString(this MyKind source)
{
return ((int)source).ToString(CultureInfo.InvariantCulture);
}
}
// Code to use in the *.aspx.cs file
// Drop down for lead times.
foreach(MyKind kind in Enum.GetValues(typeof(MyKind)))
{
DropDownBla.Items.Add(new ListItem(kind.GetName(), kind.GetValueString()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment