1

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?

2 Answers 2

1

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();
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.