Friday, March 21, 2008

List out all enum values

I just found out the way to list out all the enum values. It's indeed very simple!

For example, I want to list out all the enum values of FileAccess.

string[] enumList = Enum.GetNames(typeof(FileAccess));
DropDownList1.DataSource = enumList;

I also found out how to convert the string representation value to the enum value too.

FileAccess fAccess = (FileAccess)Enum.Parse(typeof(FileAccess), DropDownList1.SelectedValue);

that's it. Isn't it simple?

No comments:

Post a Comment