Enums in DropDownList

I was talking to someone who had to do this today, and couldn't find any code to do it. Here's some old code I had lying around, maybe it's of interest to others.

Binding an enum type:

control.DataSource = Enum.GetNames( typeof(YourEnumType) );

Getting an enum value back from the string value:

YourEnumType myEnum = (YourEnumType)Enum.Parse( typeof(YourEnumType), control.SelectedValue);

If anyone has a simplified or better way to do this, please share.

Published Wednesday, July 23, 2003 11:43 PM by Tim Marman

Comments

# re: Strongly-Type a DropDownList with Enumerated Types

Thursday, March 03, 2005 7:59 AM by TrackBack

Leave a Comment

(required) 
(required) 
(optional)
(required)