开发者

How to Bind Enum Types to the DropDownList?

If I have the following enu开发者_运维技巧m

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?


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);


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();


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


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;
}


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>();


Even simpler:

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

Then to go back:

EmployeeType etSelected = (EmployeeType)ddl.SelectedValue;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜