473,395 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Binding enum values alphabetically to a drop down list

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.E ventType),
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.Lo g.EventType))
Dim objDataRow As DataRow = objDataTable.NewRow
objDataRow("EventTypeID") =
CType([Enum].Parse(GetType(ManagePointClassLibrary.Utils.Log.E ventType),
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
Nov 17 '05 #1
2 9868
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
jo***********@surfcontrol.com
"Mark Heimonen" <ma***@adiaim.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.E ventType),
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.Lo g.EventType))
Dim objDataRow As DataRow = objDataTable.NewRow
objDataRow("EventTypeID") =
CType([Enum].Parse(GetType(ManagePointClassLibrary.Utils.Log.E ventType),
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

Nov 17 '05 #2
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" <jo***********@surfcontrol.com> wrote in message
news:uR**************@TK2MSFTNGP12.phx.gbl...
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
jo***********@surfcontrol.com
"Mark Heimonen" <ma***@adiaim.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.E ventType),
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.Lo g.EventType))
Dim objDataRow As DataRow = objDataTable.NewRow
objDataRow("EventTypeID") =
CType([Enum].Parse(GetType(ManagePointClassLibrary.Utils.Log.E ventType),
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


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: msnews.microsoft.com | last post by:
How do I get Intellisense to open a dropdown list box for a method's parameters when the parameter is an ENUM? public class MyClass { public enum IDkind { PersonID, EntityID, PlaceID
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
13
by: Adam Blair | last post by:
Is it possible to bind a switch statement to an Enum such that a compile-time error is raised if not all values within the Enum are handled in the switch statement? I realise you can use default:...
5
by: Vigneshwar Pilli via DotNetMonster.com | last post by:
string connectionString1 = "server=(local); user=sa;password=sa; database=sonic"; System.Data.SqlClient.SqlConnection dbConnection1 = new System.Data.SqlClient.SqlConnection(connectionString1);...
0
by: dutone | last post by:
I have searched high and low for a solution to this and I dont think its possible.... but I hope not. What I have is a list of items with various values that can be changed via a drop down. Each...
7
by: Nathan | last post by:
I'd like to use an enumeration as a datasource for a drop-down box. Is there a way to do this?
2
by: bmayer | last post by:
I am using a detail view and binding it to a sql data source (at some point in the future it will be an object data source). I have seen the article "Working with Data is ASP.NET 2.0 ::...
2
by: Jim Gregg | last post by:
Hello all, I am faced with some logic that I am unsure how to handle. Imagine that I am running a WMI query and I am outputting the data into a dynamically created ASP table control. Here is my...
2
by: =?Utf-8?B?VmljdG9yaW91czE=?= | last post by:
When the editcommand is execured for my datagrid the dropdown list appears for the bound item status. However, the item selected by default does not match the unedited data. How do I make the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.