Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / Visual Basic

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

Rate me:
Please Sign up or sign in to vote.
3.00/5 (3 votes)
12 May 2008CPOL2 min read 39.3K   18   1
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:

VB.NET
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.

VB.NET
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)


Written By
Team Leader Cambridge Solutions LTD
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMight apply. Pin
Joe_Dert28-Jun-10 19:52
Joe_Dert28-Jun-10 19:52 

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

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