Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
C#
public static void LoadHemCombo(ComboBox cbo,Enum myEnum)
       {
           cbo.DataSource = Enum.GetValues(typeof(myEnum))
               .Cast<Enum>()
               .Select(value => new
               {
                   (Attribute.GetCustomAttribute(value.GetType().GetField(value.ToString()), typeof(DescriptionAttribute)) as DescriptionAttribute).Description,
                   value
               })
               .OrderBy(item => item.value)
               .ToList();
           cbo.DisplayMember = "Description";
           cbo.ValueMember = "value";
       }


this function works without enum parameter. I found the function at
http://codereview.stackexchange.com/questions/39163/loading-a-combobox-with-an-enum-and-binding-to-it[^]
Posted
Comments
Richard Deeming 13-Mar-15 12:18pm    
Maybe if you tried copying the code from the solution, rather than making random changes to it without understanding what it does?

The code in the solution doesn't have an Enum parameter, so why does yours?

1 solution

This:
C#
typeof(myEnum)
doesn't work because myEnum is your enum variable, not your enum type.

Instead you have to write the name of your enum-definition there - the equivalent to what is HemEnum in the source you linked:
C#
public enum HemEnum
{
    [Description("none")]
    HemNone = -1,
    [Description("sewn")]
    Hemsew = 0,
    [Description("welded")]
    HemWeld = 1,
    [Description("double folded")]
    Hemdoublefold = 2
}

So, if your enum type is named "Abcdef" you have to write:
C#
public static void LoadHemCombo(ComboBox cbo)
{
    cbo.DataSource = Enum.GetValues(typeof(Abcdef))
    // rest identical

I excluded the myEnum parameter of your method there because I don't know what it is supposed to be good for - it's not needed/used in the method.

edit after comment:

To use this with several different Enum-Types, make it a generic method:

C#
public static void LoadCombo<TEnum>(ComboBox cbo)
       {
           cbo.DataSource = Enum.GetValues(typeof(TEnum))
               .Cast<Enum>()
               .Select(value => new
               {
(Attribute.GetCustomAttribute(value.GetType().GetField(value.ToString()), typeof(DescriptionAttribute)) as DescriptionAttribute).Description,
                   value
               })
               .OrderBy(item => item.value)
               .ToList();
           cbo.DisplayMember = "Description";
           cbo.ValueMember = "value";
       }


Example use:
C#
public enum YourEnumType1
{
  [Description("foo")]
  abc,
  [Description("bar")]
  def,
  [Description("baz")]
  ghi
}

// wherever you want to initialize the combobox that should show YourEnumType1:

LoadCombo<YourEnumType1>(cbo);
 
Share this answer
 
v6
Comments
amagitech 13-Mar-15 12:31pm    
Yes I used it it's work. But I will use 40 different enum. So am I create 40 different method foreach enum
[no name] 13-Mar-15 12:33pm    
No, don't do that :-) There's a better way. I'll update my answer, please check in a few minutes.
[no name] 13-Mar-15 13:01pm    
Updated the answer. One addition, to make it clear: You use that generic form of the method for all of your 40 enum types, you just write the name of the specific enum type between the angled braces.
Please accept the answer if it works for you :-)
amagitech 14-Mar-15 3:30am    
Thank you. It's work.I am sorry for my delayed reply.
[no name] 14-Mar-15 3:58am    
No worries :-) You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900