Skip to main content
Active reading [<http://en.wikipedia.org/wiki/Language_Integrated_Query>].Active reading [<http://en.wikipedia.org/wiki/Language_Integrated_Query>]. Removed meta information (this belongs in comments).
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

A bit late to the party, but using LinqUsing LINQ this can be done elegantly:

public static void SetOptions<T>(this DropDownList dropDownList) { if (!typeof(T).IsEnum) { throw new ArgumentException("Type must be an enum type."); } dropDownList.Items.AddRange(Enum .GetValues(typeof(T)) .Cast<Enum>() .Select(x => new ListItem(x.ToString(), Convert.ToInt32(x).ToString())) .ToArray()); } 

A bit late to the party, but using Linq this can be done elegantly:

public static void SetOptions<T>(this DropDownList dropDownList) { if (!typeof(T).IsEnum) { throw new ArgumentException("Type must be an enum type."); } dropDownList.Items.AddRange(Enum .GetValues(typeof(T)) .Cast<Enum>() .Select(x => new ListItem(x.ToString(), Convert.ToInt32(x).ToString())) .ToArray()); } 

Using LINQ this can be done elegantly:

public static void SetOptions<T>(this DropDownList dropDownList) { if (!typeof(T).IsEnum) { throw new ArgumentException("Type must be an enum type."); } dropDownList.Items.AddRange(Enum .GetValues(typeof(T)) .Cast<Enum>() .Select(x => new ListItem(x.ToString(), Convert.ToInt32(x).ToString())) .ToArray()); } 
Source Link
matk
  • 1.5k
  • 2
  • 14
  • 25

A bit late to the party, but using Linq this can be done elegantly:

public static void SetOptions<T>(this DropDownList dropDownList) { if (!typeof(T).IsEnum) { throw new ArgumentException("Type must be an enum type."); } dropDownList.Items.AddRange(Enum .GetValues(typeof(T)) .Cast<Enum>() .Select(x => new ListItem(x.ToString(), Convert.ToInt32(x).ToString())) .ToArray()); }