如何将枚举类型绑定到DropDownList?

时间:2010-06-23 02:52:54

标签: c# asp.net enums

如果我有以下枚举

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

我有DropDownList,我想将这个EmployeeType枚举绑定到DropDownList,有没有办法做到这一点?

6 个答案:

答案 0 :(得分:69)

如果您有名为ddl的DropDownList对象,则可以按照下面的方式执行

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

如果您希望Enum值返回选择....

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

答案 1 :(得分:12)

你可以使用lambda表达式

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

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

答案 2 :(得分:5)

这是另一种方法:

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

我使用此链接来执行此操作:

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

答案 3 :(得分:1)

我写了一个帮助函数来给我一个我可以绑定的字典:

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

答案 4 :(得分:1)

这是我的解决方法:

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

我的枚举类型:

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

似乎是:

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

答案 5 :(得分:0)

更简单:

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

然后返回:

EmployeeType etSelected = (EmployeeType)ddl.SelectedValue;