Click here to Skip to main content
Click here to Skip to main content

Binding the Enum to the Dropdown Listbox and Sorting it on Values

, 12 May 2008
Rate this:
Please Sign up or sign in to vote.
An article on Binding Enum to DropdownListbox with the SortedList that sorts entries by value instead of by key.

Introduction

This article is for those who want to bind the DropdownListbox control in Web/Window Form to the Enum data type.

Background

Sometimes, we want to bind the Enums to the Dropdownlistbox instead of fetching the data from the database and then bind it to the Dropdownlistbox. Here I explain how to bind the Enums that you have defined in your Common Classes/Utility Classes Code.

Using the Code

It is really simple. Let's have a look at the below code snippet. Following is my Enum which defines the PhoneNumberType:

Public Enum PhoneNumberType As Integer
    [Select] = -1
    Home = 0
    Mobile = 1
    Work = 2
End Enum

Now, to bind any enum like this to your DropdownListbox,  all you need to do is create any variable of any type like SortedList(Of TKeys,TValues), Dictionary, etc. and then assign that variable to DataSource property of your dropdownListbox. I have used SortedList for binding.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) _
	Handles MyBase.Load, Me.Load

            If (Not Me.IsPostBack) Then
                Me._presenter.OnViewInitialized()
                Dim phoneTypes As SortedList(Of Integer, String)
                phoneTypes = GetEnumDataSource(Of PhoneNumberType)()
                If Not (phoneTypes Is Nothing) Then
                    drplstPhoneType.DataSource = phoneTypes
                    drplstPhoneType.DataValueField = "Key"
                    drplstPhoneType.DataTextField = "Value"
                    drplstPhoneType.DataBind()
                End If
            End If
End Sub

Public Function GetEnumDataSource(Of T)() As SortedList(Of Integer, String)
            Dim myEnumType As Type = GetType(T)
            Dim returnCollection As SortedList(Of Integer, String) = _
				New  SortedList(Of Integer, String)
            Try
                If myEnumType.BaseType Is GetType([Enum]) Then
                    Dim enumNames() As String = [Enum].GetNames(myEnumType)
                    For i As Integer = 0 To (enumNames.Length - 1)
                        returnCollection.Add(Convert.ToInt32([Enum].Parse_
				(myEnumType, enumNames(i))), enumNames(i))
                    Next
                End If
            Catch ex As Exception
                Return Nothing
            End Try
            Return returnCollection
        End Function

Points of Interest

What I did here is that I filled the SortedList with my EnumData Type's values and text for the same. Now if you observe the code snippets thoroughly, I have solved another problem of sorting out the DropdownListbox with item's values.

Since SortedList is capable of sorting out the item by its "keys", what I did here is for "keys", I have used the values of Enum and for values, I have used the text of Enum. Previously, it was the other way around. I have filled the SortedList with Text of Enum as Keys and values of Enum as values of Sorted List. In this case SortedList will return the List sorted by its Keys. But since I want "Select" option to be at the top with -1 value, what I did is instead of defining SortedList(Of String, Integer), I have defined SortedList(Of Integer, String) and filled all the values of Enum as Keys of the SortedList and Text as Values of the SortedList.

History

  • 12th May, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Chirag Maisuriya
Team Leader Cambridge Solutions LTD
India India
No Biography provided

Comments and Discussions

 
GeneralMight apply. PinmemberJoe_Dert28-Jun-10 19:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 12 May 2008
Article Copyright 2008 by Chirag Maisuriya
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid