Home > ASP.Net > Populate DropdownList from Enum

Populate DropdownList from Enum


Hi

try this example:

private enum Salutations { Mr = 1, Ms = 2, Mrs = 3 };

protected void Page_Load(object sender, EventArgs e)
    {
        Hashtable ht = GetEnumForBind(typeof(Salutations));
        DropDownList1.DataSource = ht;
        DropDownList1.DataTextField = "value";
        DropDownList1.DataValueField = "key";

        DropDownList1.DataBind();
       
    }

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

Hope it helps

Good Luck

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

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: