How to Bind Enum Types to the DropDownList?

if you have DropDownList object called ddl you can do it as below

ddl.DataSource = Enum.GetNames(typeof(EmployeeType));
ddl.DataBind();

if you want the Enum value Back on Selection ....

 EmployeeType empType = (EmployeeType)Enum.Parse(typeof(EmployeeType), ddl.SelectedValue);

you can use lambda expression

        ddl.DataSource = Enum.GetNames(typeof(EmployeeType)).
        Select(o => new {Text = o, Value = (byte)(Enum.Parse(typeof(EmployeeType),o))});
        ddl.DataTextField = "Text";
        ddl.DataValueField = "Value";
        ddl.DataBind();

or Linq

        ddl.DataSource = from Filters n in Enum.GetValues(typeof(EmployeeType))
                select new { Text = n, Value = Convert.ToByte(n) };
        ddl.DataTextField = "Text";
        ddl.DataValueField = "Value";
        ddl.DataBind();

Tags:

C#

Asp.Net

Enums