How to Bind Enum Types to the DropDownList?

后端 未结 6 1153
终归单人心
终归单人心 2020-12-24 11:29

If I have the following enum

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

and I have DropDownList

相关标签:
6条回答
  • 2020-12-24 12:02

    Even simpler:

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

    Then to go back:

    EmployeeType etSelected = (EmployeeType)ddl.SelectedValue;
    
    0 讨论(0)
  • 2020-12-24 12:05

    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;
    }
    
    0 讨论(0)
  • 2020-12-24 12:09

    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();
    
    0 讨论(0)
  • 2020-12-24 12:14

    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

    0 讨论(0)
  • 2020-12-24 12:19

    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>();
    
    0 讨论(0)
  • 2020-12-24 12:25

    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);
    
    0 讨论(0)
提交回复
热议问题