Using an Enum as a DataSource

June 10, 2010

I tend to use Enumurations quite alot in my code – They are a very handy way of restricting the values of data whizzing around your application.

Today I encountered a scenario where I needed to allow the user to select from a bunch of enum entries presented in a drop down list.

This is actually a very simple task, heres how I did it:

DropDownListFoo.DataSource = Enum.GetNames(typeof(MyClass.EnumName));

Enum.GetNames() takes an enum and returns a String array containing the values within the enum.

The key here is that an array is a valid DataSource, where an an enum is not as it does not have any suitable interfaces.


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

%d bloggers like this: