How to Bind Enum Types to the DropDownList?

If I have the following enum

public enum EmployeeType
{
    Manager = 1,
    TeamLeader,
    Senior,
    Junior
}

and I have DropDownList and I want to bind this EmployeeType enum to the DropDownList, is there any way to do that?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

if you have DropDownList object called ddl you can do it as below

ddl.DataSource = Enum.GetNames(typeof(EmployeeType));
ddl.DataBind();

if you want the Enum value Back on Selection ….

 EmployeeType empType = (EmployeeType)Enum.Parse(typeof(EmployeeType), ddl.SelectedValue);

Method 2

you can use lambda expression

        ddl.DataSource = Enum.GetNames(typeof(EmployeeType)).
        Select(o => new {Text = o, Value = (byte)(Enum.Parse(typeof(EmployeeType),o))});
        ddl.DataTextField = "Text";
        ddl.DataValueField = "Value";
        ddl.DataBind();

or Linq

        ddl.DataSource = from Filters n in Enum.GetValues(typeof(EmployeeType))
                select new { Text = n, Value = Convert.ToByte(n) };
        ddl.DataTextField = "Text";
        ddl.DataValueField = "Value";
        ddl.DataBind();

Method 3

Here is another approach:

Array itemNames = System.Enum.GetNames(typeof(EmployeeType));
foreach (String name in itemNames)
{
    //get the enum item value
    Int32 value = (Int32)Enum.Parse(typeof(EmployeeType), name);
    ListItem listItem = new ListItem(name, value.ToString());
    ddlEnumBind.Items.Add(listItem);
}

i used this link to do it:

http://www.codeproject.com/Tips/303564/Binding-DropDownList-Using-List-Collection-Enum-an

Method 4

I wrote a helper function to give me a dictionary that I can bind:

public static Dictionary<int, string> GetDictionaryFromEnum<T>()
{

    string[] names = Enum.GetNames(typeof(T));

    Array keysTemp = Enum.GetValues(typeof(T));
    dynamic keys = keysTemp.Cast<int>();

    dynamic dictionary = keys.Zip(names, (k, v) => new {
        Key = k,
        Value = v
    }).ToDictionary(x => x.Key, x => x.Value);

    return dictionary;
}

Method 5

Here is my solution:

public static class DataBindingExtensions
{
    public static string GetDescription(this Enum source)
    {
        var str = source.ToString();
        var fi = source.GetType().GetField(str);
        var desc = fi.GetCustomAttribute(typeof(DescriptionAttribute), false) as DescriptionAttribute;
        return desc == null ? str : desc.Description; 
    }

    public static T GetValue<T>(this ComboBox comboBox)
        where T : struct, IComparable, IFormattable, IConvertible
    {
        return (T)comboBox.SelectedValue;
    }

    public static ComboBox BindTo<T>(this ComboBox comboBox) 
        where T : struct, IComparable, IFormattable, IConvertible
    {
        var list = Array.ConvertAll((T[])Enum.GetValues(typeof(T)), value => new { desp = ((Enum)(object)value).GetDescription(), value });
        comboBox.DataSource = list;
        comboBox.DisplayMember = "desp";
        comboBox.ValueMember = "value";
        return comboBox;
    }

    // C# 7.0 or highest
    public static ComboBox BindTo<T>(this ComboBox comboBox)
        where T : Enum
    {
        var list = Array.ConvertAll((T[])Enum.GetValues(typeof(T)), value => new { desp = value.GetDescription(), value });
        comboBox.DataSource = list;
        comboBox.DisplayMember = "desp";
        comboBox.ValueMember = "value";
        return comboBox;
    }
}

My enum type:

public enum Mode
{
    [Description("Mode A")]
    A,
    [Description("Mode B")]
    B
}

Seems like:

var cb = new ComboBox();
cb.DropDownStyle = ComboBoxStyle.DropDownList;
cb.BindTo<BackupMode>();

Method 6

Even simpler:

 ddl.DataSource = Enum.GetValues(typeof(EmployeeType));

Then to go back:

EmployeeType etSelected = (EmployeeType)ddl.SelectedValue;


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x