12

I am new to C# and I have a question,

I have a a enum something like

   public enum
        {
    [Description("1,2,3")]
    123,
    [Description("3,4,5")]
    345,
    [Description("6,7,8 ")]
    678,
       }

Now I want the enum descriptions to bind to a dropdownlist.. can some one help me..

thanks in advance!

PS: I am sorry if I am not clear..Let me know if I need to be more specific

helpme
  • 570
  • 1
  • 7
  • 26

3 Answers3

10
public static class EnumExtensionMethods
{
    public static string GetDescription(this Enum enumValue)
    {
        object[] attr = enumValue.GetType().GetField(enumValue.ToString())
            .GetCustomAttributes(typeof (DescriptionAttribute), false);

        return attr.Length > 0 
           ? ((DescriptionAttribute) attr[0]).Description 
           : enumValue.ToString();            
    }

    public static T ParseEnum<T>(this string stringVal)
    {
        return (T) Enum.Parse(typeof (T), stringVal);
    }
}

//Usage with an ASP.NET DropDownList
foreach(MyEnum value in Enum.GetValues<MyEnum>())
   myDDL.Items.Add(New ListItem(value.GetDescription(), value.ToString())
...
var selectedEnumValue = myDDL.SelectedItem.Value.ParseEnum<MyEnum>()

//Usage with a WinForms ComboBox
foreach(MyEnum value in Enum.GetValues<MyEnum>())
   myComboBox.Items.Add(new KeyValuePair<string, MyEnum>(value.GetDescription(), value));

myComboBox.DisplayMember = "Key";
myComboBox.ValueMember = "Value";
...
var selectedEnumValue = myComboBox.SelectedItem.Value;

These two extension methods have been invaluable to me for going on 5 years and two different jobs, for exactly your stated need.

KeithS
  • 70,210
  • 21
  • 112
  • 164
4

This is how you would write it:

public enum Test
{
  [Description("1,2,3")]
  a = 123,
  [Description("3,4,5")]
  b = 345,
  [Description("6,7,8")]
  c = 678
}

//Get attributes from the enum
    var items = 
       typeof(Test).GetEnumNames()
        .Select (x => typeof(Test).GetMember(x)[0].GetCustomAttributes(
           typeof(DescriptionAttribute), false))
        .SelectMany(x => 
           x.Select (y => new ListItem(((DescriptionAttribute)y).Description)))

//Add items to ddl
    foreach(var item in items)
       ddl.Items.Add(item);
Magnus
  • 45,362
  • 8
  • 80
  • 118
  • i tried this but i cannot see the GetEnumNames() and .GetCustomAttributes() am i missing an assembly?? – helpme May 08 '12 at 20:06
  • [GetEnumNames()](http://msdn.microsoft.com/en-us/library/system.type.getenumnames.aspx) is a member of the `Type` class – Magnus May 08 '12 at 20:58
  • This will only takes enum item with Description attribute and will not take which doesn't has that. It would be great if the code house that, so all items will be available in the dropdownlist. – quasar Dec 17 '19 at 05:00
0

You could build a wrapper class that looks for the DescriptionAttribute on each member and displays that. Then bind to the wrapper instance. Something like this:

Get the Enum<T> value Description

Community
  • 1
  • 1
D Stanley
  • 149,601
  • 11
  • 178
  • 240