Binding enum values alphabetically to a drop down list

  • Thread starter Thread starter Mark Heimonen
  • Start date Start date
M

Mark Heimonen

I haven't worked with enum types that much, and I wrote the following code
to bind a list of enums to a dropdownlist. Using
CType([Enum].Parse(GetType(ManagePointClassLibrary.Utils.Log.EventType),
strEventType), Integer).ToString is horrible to read, and probably not that
efficient either. Does anybody know a more elegant approach to this
problem?

Dim strEventType As String
For Each strEventType In
[Enum].GetNames(GetType(ManagePointClassLibrary.Utils.Log.EventType))
Dim objDataRow As DataRow = objDataTable.NewRow
objDataRow("EventTypeID") =
CType([Enum].Parse(GetType(ManagePointClassLibrary.Utils.Log.EventType),
strEventType), Integer).ToString
objDataRow("EventType") = strEventType
objDataTable.Rows.Add(objDataRow)
objDataRow.AcceptChanges()
Next
objDataView = New DataView()
objDataView = objDataTable.DefaultView
objDataView.Sort = "EventType"
eventtype.DataSource = objDataView
eventtype.DataTextField = "EventType"
eventtype.DataValueField = "EventTypeID"
eventtype.DataBind()

Thanks,

Mark Heimonen
 
Mark,

You can't do much better than this because the ASP.NET DropDownList can only
handle string values (as opposed to the Windows Forms DropDownList, which
can handle arbitrary objects).

About the only thing you can do is to ignore the integer value entirely. Let
the DropDownList use EventType for both the DataTextField and
DataValueField. When you need a value (perhaps in the SelectedIndexChanged
event), _then_ use Enum.Parse to get the value as your Enum type.
 
Thanks,

Yeah, I think ignoring the integer value entirely is a good step up
performance-wise.

Mark Heimonen
Developer
Adia Information Management Corporation

John Saunders said:
Mark,

You can't do much better than this because the ASP.NET DropDownList can only
handle string values (as opposed to the Windows Forms DropDownList, which
can handle arbitrary objects).

About the only thing you can do is to ignore the integer value entirely. Let
the DropDownList use EventType for both the DataTextField and
DataValueField. When you need a value (perhaps in the SelectedIndexChanged
event), _then_ use Enum.Parse to get the value as your Enum type.
--
John Saunders
Internet Engineer
(e-mail address removed)


Mark Heimonen said:
I haven't worked with enum types that much, and I wrote the following code
to bind a list of enums to a dropdownlist. Using
CType([Enum].Parse(GetType(ManagePointClassLibrary.Utils.Log.EventType),
strEventType), Integer).ToString is horrible to read, and probably not that
efficient either. Does anybody know a more elegant approach to this
problem?

Dim strEventType As String
For Each strEventType In
[Enum].GetNames(GetType(ManagePointClassLibrary.Utils.Log.EventType))
Dim objDataRow As DataRow = objDataTable.NewRow
objDataRow("EventTypeID") =
CType([Enum].Parse(GetType(ManagePointClassLibrary.Utils.Log.EventType),
strEventType), Integer).ToString
objDataRow("EventType") = strEventType
objDataTable.Rows.Add(objDataRow)
objDataRow.AcceptChanges()
Next
objDataView = New DataView()
objDataView = objDataTable.DefaultView
objDataView.Sort = "EventType"
eventtype.DataSource = objDataView
eventtype.DataTextField = "EventType"
eventtype.DataValueField = "EventTypeID"
eventtype.DataBind()

Thanks,

Mark Heimonen
 
Back
Top