public static void DataBind(ListControl ctrl, Type enumType, EnumDropdownBindingType dropdownBindingType, string firstRecordText) { List <EnumItemData> itemList = GetEnumData(enumType).Values.ToList(); switch (dropdownBindingType) { case EnumDropdownBindingType.Exact: //Do nothing break; case EnumDropdownBindingType.ClearFirstRecord: itemList[0].DisplayValue = firstRecordText; break; case EnumDropdownBindingType.AddEmptyFirstRecord: int firstValue = -1; EnumItemData item = new EnumItemData() { ID = firstValue, DisplayValue = firstRecordText, Name = null }; itemList.Insert(0, item); break; case EnumDropdownBindingType.RemoveFirstRecord: itemList.RemoveAt(0); break; } ctrl.DataTextField = "DisplayValue"; ctrl.DataValueField = "ID"; ctrl.DataSource = itemList; ctrl.DataBind(); }
public static Dictionary <int, EnumItemData> GetEnumData(Type enumType) { Dictionary <int, EnumItemData> returnList = new Dictionary <int, EnumItemData>(); Array enumValues = Enum.GetValues(enumType); foreach (object value in enumValues) { int intValue = (int)value; string name = Enum.GetName(enumType, value); string displayValue = EnumDisplayValueAttribute.GetDisplayValue(value); EnumItemData item = new EnumItemData() { ID = intValue, Name = name, DisplayValue = displayValue }; returnList.Add(intValue, item); } return(returnList); }