Posts Tagged ‘Enum’

Create Drop Down List Options From Enum

Thursday, April 2nd, 2009

To view the values of an enumeration’s type (enum) to a DropdownList (or other controls with a DataSource property available) is sufficient to bind the following code:

Here’s an example with its own enum (of course also works with system types)

?View Code CSHARP
public enum CountryList
{
Germany = 1,
India = 2,
Japan = 3,
UAE = 4,
USA = 5
}
 
DropdownList1.DataSource = System.Enum.GetValues(typeof(CountryList));

Now, whenever you want to get to The Value of Even enum () method of the enum is useful to get under the VALUE of enum sees below snippet will helpful to
you.

?View Code CSHARP
int intCountry = Convert.ToInt32(System.Enum.Parse(typeof(CountryList), DropdownList1.SelectedItem.ToString());

Read More…

Happy Programming!!