Bind enum to ddl

Sometime we need to bind the value and the name of the enum to the dropdownlist. here is the code that will help us to achieve this.

here I have created a method to convert enum into hashtable :

    public Hashtable ConvertEnumToHashTable(Type myenum)
        string[] names = Enum.GetNames(myenum);
        Array values = Enum.GetValues(myenum);
        Hashtable ht = new Hashtable();
        for (int i = 0; i < names.Length; i++)
            ht.Add(Convert.ToInt32(values.GetValue(i)).ToString(), names[i]);
        return ht;

Now here is code that shows you how to use this function :

    protected void Page_Load(object sender, EventArgs e)
        Hashtable ht = ConvertEnumToHashTable(typeof(myenum));
        myDDL.DataSource = ht;
        myDDL.DataTextField = "value";
        myDDL.DataValueField = "key";



Hope this code will help !!!

Jay Ganesh


One thought on “Bind enum to ddl

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