Binding enum values alphabetically to a drop down list

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default 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


    Mark Heimonen Guest

  2. Similar Questions and Discussions

    1. wsdl changes enum values with [Flags]
      If I declare a enum with the attribute like: public enum XXX {a=0,b=1,c=2} Then I use this enum in a web service, the generated wsdl code...
    2. Need help in binding data to datagrid by using drop down list
      Hi all, I am new in asp.net, i encounter some problems in using drop down list and datagrid. I have manage to bind the data into datagrid but i...
    3. Binding WS to the List Component
      I'm having some trouble with binding a web service to a list component and would appreciate some help... I've written a simple CFC to use as a...
    4. Dynamical populating a list that can be used as drop down list
      Hi, I have a solution in which a person can be a member of one or more groups. In this case the groups are those used in the protection schema of...
    5. note 33904 added to function.fdf-enum-values
      If you need something like this in a pre 4.3 version, here is a rough hack I was working on before I realized I didn't need this after all. ...
  3. #2

    Default Re: Binding enum values alphabetically to a drop down list

    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
    [email]john.saunders@surfcontrol.com[/email]


    "Mark Heimonen" <markh@adiaim.com> wrote in message
    news:%23Xx5hwEWDHA.2368@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
    >
    >

    John Saunders Guest

  4. #3

    Default Re: Binding enum values alphabetically to a drop down list

    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" <john.saunders@surfcontrol.com> wrote in message
    news:uRNyLEfWDHA.2352@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
    > [email]john.saunders@surfcontrol.com[/email]
    >
    >
    > "Mark Heimonen" <markh@adiaim.com> wrote in message
    > news:%23Xx5hwEWDHA.2368@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
    > >
    > >
    >
    >

    Mark Heimonen Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139