Re: Binding enum values alphabetically to a drop down list

Discussion in 'ASP .Net' started by John Saunders, Aug 3, 2003.

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



    "Mark Heimonen" <> wrote in message
    news:%...
    > 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
    >
    >
    John Saunders, Aug 3, 2003
    #1
    1. Advertising

  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" <> wrote in message
    news:...
    > 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
    >
    >
    >
    > "Mark Heimonen" <> wrote in message
    > news:%...
    > > 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 Heimonen, Aug 4, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SirPoonga
    Replies:
    2
    Views:
    741
    Ben Strackany
    Jan 7, 2005
  2. weiwei
    Replies:
    0
    Views:
    1,001
    weiwei
    Jan 5, 2007
  3. msimmons
    Replies:
    0
    Views:
    456
    msimmons
    Jul 16, 2009
  4. Replies:
    5
    Views:
    252
  5. Replies:
    3
    Views:
    275
Loading...

Share This Page