Binding enum values alphabetically to a drop down list

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
 
J

John Saunders

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.
 
M

Mark Heimonen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top