enum and dropdownlist

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

he yall,
does anyone know the best way to populate a dropdownlist from an enum list?

thanks,
rodchar
 
if (!IsPostBack)
{
ListItem li;

foreach (myEnum myValue in Enum.GetValues(typeof(myEnum)))
{
li = new ListItem(Enum.GetName(typeof(myEnum), myValue).ToString(),
myValue.ToString());
myDropDownList.Items.Add(li);
}
}

Derek
 
Thank you for that, ok now that the dropdownlist has the enums when the user
selects an item how do i convert it back to the enum type

when i try to assign the selected value back to the type:

_varType = DropDownList1.SelectValue;

I get an error message that says cannot convert type 'string' to type 'enum'.
 
What exactly are you trying to accomplish. A short but working
example of what you have so far would help greatly.

Derek
 
_varType = Enum.Parse(typeof(myEnum),DropDownList1.SelectValue);

-- bruce (sqlwork.com)
 
Back
Top