This article is for those who want to bind the bindable controls(Drop down...etc) in Web/Window Form to the Enum data type.
Sometimes, we want to bind the Enums to the bindable controls instead of fetching the data from the database and then bind it to the bindable controls. Here I explain how to bind the Enums that you have defined in your Common Classes/Utility Classes Code.
Imagine you have an enum defined in your code like so:
public enum Departments
Every Enum type derives from System.Enum. There are two static methods that help bind data to a binable control (and retrieve the value). These are Enum.GetNames and Enum.Parse. Using GetNames,you are able to bind to your binable control as follows:
Below i am writing the example how to bind the enum to dropdown list.
Dropdown list id:DDLDeparments
DDLDeparments.DataSource = System.Enum.GetNames(typeof(Departments));
Now if you want the Enum value Back on Selection ....
private void DDLDeparments_SelectedIndexChanged(object sender, System.EventArgs e)
Departments selecteddep = (Departments)Enum.Parse(DDLDeparments.SelectedValue);