Skip to content

Binding an enumeration to a dropdown list with words rather than an unbroken string – A slightly better version

March 22, 2012

Yesterday I had found a better solution for binding an enumeration to a dropdown list as supposed to two days ago. In terms of the code length its shorter, and its more flexible in terms of the binding the datatext field and datavalue fields more efficiently than writing each one for each enumeration.

Here I have the  DataTextField is assigned to “Value” and the DataValueField is assigned to “Key.” This is because we used the Dictionary type in the BindWithEnum function.

Dictionary<string, int> d2 = GetEnumForBind(typeof(ActiveFrom));

LoadActiveFromToPrimary();

.........

    public void LoadActiveFromToPrimary()
    {
        this.ddlControl.DataTextField = "Name";

        this.ddlControl.DataValueField = "Code";

        this.ddlControl.DataSource = Extensions.BindWithEnum<ActiveFrom>();

        this.ddlControl.DataBind();
    }

The bulk of the code uses the DescriptionAttribute to mark the enum with the new descriptive text as the association, the broken strings.

You set a static class which I called Extensions, and create an IEnumerable extending structs where the iteration code goes through each of the entries of the enumeration, and stores the descriptiveattribute as a string, and returns this as a list.

Please note that I had to write List in the code below when it should be List<object>, apparently this doesn’t output correctly on WordPress.

<pre>

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;

public enum ActiveFrom : int
{
[Description(“Now”)]
DateOfPurchase = 0,
[Description(“Tomorrow”)]
OnLogin = 1,
[Description(“Day After Tomorrow”)]
OnActivation = 2
}

public static class Extensions
{
public static IEnumerable BindWithEnum() where TEnum : struct
{
Type enumType = typeof(TEnum);

Type descriptionAttributeType = typeof(DescriptionAttribute);

List list = new List();

foreach (int value in Enum.GetValues(enumType))
{
MemberInfo enumObj = enumType.GetMember(Enum.ToObject(enumType, value).ToString()).SingleOrDefault();

object attr = enumObj.GetCustomAttributes(descriptionAttributeType, false).SingleOrDefault();

if (attr == null) continue;

string description = ((DescriptionAttribute)attr).Description;

list.Add(new { Code = value, Name = description });
}

return list;
}
}

</pre>

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: