![]() |
Desktop Development »
Combo & List Boxes »
Combo and List boxes
Intermediate
License: The Code Project Open License (CPOL)
Binding the Enum to the Dropdown Listbox and Sorting it on ValuesBy Chirag MaisuriyaAn article on Binding Enum to DropdownListbox with the SortedList that sorts entries by value instead of by key. |
VB (VB 7.x, VB 8.0, VB 9.0, VB 6), .NET (.NET 2.0, .NET 3.0, .NET 3.5), WebForms
|
|
Advanced Search |
|
|
|
||||||||||||||||
This article is for those who want to bind the DropdownListbox control in Web/Window Form to the Enum data type.
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.
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
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.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 12 May 2008 Editor: Deeksha Shenoy |
Copyright 2008 by Chirag Maisuriya Everything else Copyright © CodeProject, 1999-2009 Web10 | Advertise on the Code Project |