Skip to content

Binding an enumeration to a dropdown list with words rather than an unbroken string

March 16, 2012

If you ever wanted to bind an enumeration to a dropdown list with words rather than an unbroken string, for instnace you can only have as a key value for enumeration in your enumeration class strings like Bus, Car, Plane, but what if you wanted to have 20 Seater Bus, Four By Four Car, or Jet Fighter Plane as the text bound to your dropdown list from the keys in the enumeration. You cannot simple do this with the enumeration class alone, you need an extension class to add to your enum. Here’s how: –

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

    public enum EnumSample : int
        [StringValue("This is sample text")]
        SampleText1 = 0,
        [StringValue("This is a second sample text")]
        [StringValue("Finally the third text as sample")]

    public static class Extensions

 /// Will get the string value for a given enums value, this will /// only work if you assign the StringValue attribute to /// the items in your enum. /// 

////// public static string GetStringValue(this ActiveFrom value) { // Get the type Type type = value.GetType(); // Get fieldinfo for this type FieldInfo fieldInfo = type.GetField(value.ToString()); // Get the stringvalue attributes StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes( typeof(StringValueAttribute), false) as StringValueAttribute[]; // Return the first if there was a match. return attribs.Length > 0 ? attribs[0].StringValue : null; } }

You can see I added the Extensions class with the method GetStringValue(), this essentially gets the data type of the parameter being passed in, and is able using reflection to obtain the string value field passed in from the enumeration and store into StringValueAttribute[] attribs.

And then you simple call the Enum class and call the GetStringValue to work!

Before an old seperate class I created called StringValueAttribute was made redundant! So ignore the following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;

 /// This attribute is used to represent a string value /// for a value in an enum. /// 

public class StringValueAttribute : Attribute { #region Properties ///

/// Holds the stringvalue for a value in an enum. ///


public string StringValue { get; protected set; } #endregion #region Constructor ///

/// Constructor used to init a StringValue Attribute ///


///public StringValueAttribute(string value) { this.StringValue = value; } #endregion }

On the first piece of code which was a revision of the second piece with much tweaking and amending, I was able to bind a dropdown list with words of strings as the text field and the enumeration value as its text value, however it started from 0, for some reason I couldn’t set the start index as 1?
Here is the client code: –

            Dictionary d2 = GetEnumForBind(typeof(EnumSample));

            EnumSample sampletext1= ActiveFrom.SampleText1;
            EnumSample sampletext2= ActiveFrom.SampleText2;
            EnumSample sampletext3= ActiveFrom.SampleText3;

            ddlActiveFrom.Items.Add(new ListItem(sampletext1.GetStringValue(), d2["SampleText1"].ToString()));
            ddlActiveFrom.Items.Add(new ListItem(sampletext2.GetStringValue(), d2["SampleText2"].ToString()));
            ddlActiveFrom.Items.Add(new ListItem(sampletext3.GetStringValue(), d2["SampleText3"].ToString()));

            ddlActiveFrom.DataValueField = "value";


There you have it. Tip is to not to have a long variable name for the Enum nam, in this sample e.g. sampletext1 is fine but somethign really long meas you have to type a longer name for the key value for your enumeration.


From → Uncategorized

  1. Neat idea, but not very internationalizable. Maybe it would be easier to set an ID associated with the class instead of the members, and put it in a resource instead?

    • Do you mean to associate with an ID and store this in a database?
      Or to have fixed values in a resource file.

      My plan was to just store as an enumeration.

      Many thanks for the feedback, it does give options!

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: