Egor Lavrentev
Egor Lavrentev

Reputation: 63

How to bind enum to DropDownList in ASP.NET with "pretty names"

I know how to bind enum values to DropDownList, but I want to use "pretty names" instead of enum values.

For example I describe enum:

    public enum ContainerStatus
    {
        [Display(Description = "Container processed")]
        Processed,
        [Display(Description = "Container ready to ship")]
        ReadyToShip,
        [Display(Description = "Container sent")]
        Sent
    }

I want instead of the enum values show DisplayAttribute values. Can you help me?

Upvotes: 1

Views: 2036

Answers (2)

Tran Nguyen
Tran Nguyen

Reputation: 1381

Try a generic implementation:

public static List<KeyValuePair<string, string>> EnumToList<T>() where T: Enum
    {
        var pInfos = typeof(T).GetFields();
        List<KeyValuePair<string, string>> displayList = new List<KeyValuePair<string, string>>();
        foreach (var pi in pInfos)
        {
            if (pi.FieldType == typeof(int)) continue;
            var attr = pi.GetCustomAttributes(typeof(DisplayAttribute), false);
            if (attr != null && attr.Length > 0)
            {
                var key = pi.Name;
                var value = (attr[0] as DisplayAttribute).Description;
                KeyValuePair<string, string> listItem = new KeyValuePair<string, string>(key, value);
                displayList.Add(listItem);
            }
            else
            {
                KeyValuePair<string, string> listItem = new KeyValuePair<string, string>(pi.Name, pi.Name);
                displayList.Add(listItem);
            }
        }
        return displayList;
    }

Data Binding method:

protected void Page_Load(object sender, EventArgs e)
{
     var dataSource = EnumToList<ContainerStatus>();
     dropDownList.DataSource = dataSource;
     dropDownList.DataValueField = "Key";
     dropDownList.DataTextField = "Value";
     dropDownList.DataBind();
}

Upvotes: 1

J&#250;lio Murta
J&#250;lio Murta

Reputation: 545

You will need to create a class to read the Display attribute.

The complete source is following.

public enum ContainerStatus
{
    [Display(Description = "Container processed")]
    Processed,
    [Display(Description = "Container ready to ship")]
    ReadyToShip,
    [Display(Description = "Container sent")]
    Sent
}

public static class EnumExtensions
{
    public static string Description(this Enum value)
    {
        var enumType = value.GetType();
        var field = enumType.GetField(value.ToString());
        var attributes = field.GetCustomAttributes(typeof(DisplayAttribute),
                                                   false);
        return attributes.Length == 0
            ? value.ToString()
            : ((DisplayAttribute)attributes[0]).Description;
    }
}

public partial class WebForm1 : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {       
        var values = Enum.GetValues(typeof(ContainerStatus)).Cast<ContainerStatus>();

        foreach (var v in values)
        {
            DropDownList1.Items.Add(v.Description());                
        }
    }
}

Upvotes: 0

Related Questions