Get enumeration items as a list in C#

Sometime we need to get enumeration items as a list to bind it to some data controls like drop downs.

Here is how we can do that. Suppose we have following enumeration:

 enum UserStatus
    {
        PendingVerification = 1,
        PendingAdminApproval = 2,
        Active = 3,
        BlockedByAdmin = 4
    }

We want to bind a dropdown on manage users page with these items. As you know dropdown have two properties for each item, one to handle display value and the other to handle backend value.

Using the current definition of enumeration we can use the name of enumeration item as display property and underlying integer value as backend value but it will not look good as there will be no space between words like PendingVerification. It will be more readable if it is Pending Verification.

So let’s add Description attribute to the enumeration items. Updated enumeration will look like this:

    enum UserStatus
    {
        [Description("Pending Verification")]
        PendingVerification = 1,
        [Description("Pending Admin Approval")]
        PendingAdminApproval = 2,
        [Description("Active")]
        Active = 3,
        [Description("Blocked by Admin")]
        BlockedByAdmin = 4
    }

Note: You need to add the following using statement at the top of file:

using System.ComponentModel;

Once it is done, create a class using the following code:

  public class Enum<T> where T : struct, IConvertible
    {
       
        public static ListItemCollection GetListItems()
        {
            ListItemCollection items = new ListItemCollection();


            Type type = typeof (T);

            if (type.IsEnum)
            {
                Array values = System.Enum.GetValues(type);

                foreach (int val in values)
                {

                    var memInfo = type.GetMember(type.GetEnumName(val));
                    var descriptionAttributes = memInfo[0].GetCustomAttributes(typeof (DescriptionAttribute), false);
                    ListItem item = new ListItem
                    {
                        Text = val.ToString(),
                        Value = val.ToString()
                    };
                    if (descriptionAttributes.Length > 0)
                    {

                        item.Text = ((DescriptionAttribute) descriptionAttributes[0]).Description;
                    }
                    items.Add(item);


                }

            }
            return items;
        }
    }

Now you can bind a dropdown with the items in this enumeration using the following code:

        var userStatus = Enum<UserStatus>.GetListItems();

        DropDownList1.DataSource = userStatus;
        DropDownList1.DataTextField = "Text";
        DropDownList1.DataValueField = "Value";
        DropDownList1.DataBind();

It will show the dropdown as below:

Note: You need to add using statement for the utility class if it is somewhere else in the namespace to be able to use the GetListItems() method.

Tip: You can bind a combo box in Windows Forms application using the code below:

  cmbUserStatus.DataSource = Enum<UserStatus>.GetListItems();
                cmbUserStatus.DisplayMember = "Text";
                cmbUserStatus.ValueMember = "Value";

I hope it will help you to bind data controls with items of an enumeration without creating a look up table in database.

Comments
Share it