Library code snippets
Binding a Control to an Enum
Imagine you have an enum defined in your code like so:
enum ContractType
{
Permanent = 1,
Contract = 2,
Internship = 99
}
Suppose we have a DropDownList on an ASP.NET page from which we'd like the user to select the appropriate ContractType.
<asp:DropDownList runat="server" DataTextField="Key" DataValueField="Value" id="MyDropDownList">
Obviously, we can't do something as simple as
MyDropDownList.DataSource = ContractType;
as ContractType is a type, not an object. Instead, we can create a simple helper function that uses the Enum.GetValues and Enum.GetNames methods in order to create a Hashtable object (consisting of keys - Permanent, Contract, etc, and values - 1, 2 etc).
public static Hashtable BindToEnum(Type enumType)
{
// get the names from the enumeration
string[] names = Enum.GetNames(enumType);
// get the values from the enumeration
Array values = Enum.GetValues(enumType);
// turn it into a hash table
Hashtable ht = new Hashtable();
for (int i = 0; i < names.Length; i++)
// note the cast to integer here is important
// otherwise we'll just get the enum string back again
ht.Add(names[i], (int)values.GetValue(i));
// return the dictionary to be bound to
return ht;
}
You can then use this as follows:
MyDropDownList.DataSource = BindToEnum(typeof(ContractType));
and we get a drop down list bound to the appropriate names/values as defined by the enum (note that we specified DataTextField and DataValueField in the earlier DropDownList definition).
Related articles
Related discussion
-
Sheduling and sending mails asp.net
by mr_rajesh86 (0 replies)
-
asp.net add datarow to existing dataset table
by janetb (1 replies)
-
Buy cheap Xanax overnight. Cheap Xanax. Overnight delivery of Xanax in US no prescription needed. Cheapest Xanax.
by asleymar (0 replies)
-
Buy Soma online without a prescription. Soma drug no prescription. How to get Soma prescription. Soma cod accepted.
by asleymar (0 replies)
-
Cheap online order Fioricet. Cheap discount Fioricet. Offshore Fioricet online. How to buy Fioricet online without a prescription.
by asleymar (0 replies)
Related podcasts
-
StackOverflow uses ASP.NET MVC - Jeff Atwood and his technical team
Scott chats with Jeff Atwood of CodingHorror.com and most recently, StackOverflow.com. Jeff and Joel Spolsky and their technical team have created a new class of application using ASP.NET MVC. What works, what doesn't, and how did it all go down?
Events coming up
-
Mar
15
DevWeek 2010
London, United Kingdom
DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.
Why not just use:MyDropDownList.DataSource = Enum.GetNames(typeof(ContractType));In order to be able to sort you will have to implement an instance of the icomparer.
The first change is to the "BindToEnum" function:
Private
Function BindToEnum(ByVal enumType As Type) As ArrayList() Dim names As String(), values As Array Dim x As Integer = 0names = System.Enum.GetNames(enumType)
values = System.Enum.GetValues(enumType)
Dim arrayOfEnum(names.Length - 1) As HW.ArraySort While x < names.LengtharrayOfEnum(x) =
New HW.ArraySort(CType(values.GetValue(x), Integer), names(x))x = x + 1
End WhileArray.Sort(arrayOfEnum)
Return arrayOfEnum End FunctionYou will now be returning an ArrayList. You will also need to add the following code in a module file:
Option
Explicit OnOption
Strict OnImports
System.CollectionsPublic
Class ArraySort : Inherits System.Collections.ArrayList : Implements IComparable ' Nested classes to do secondary sorts Private Class sortByValueAscendingHelper : Implements IComparer Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements IComparer.Compare Dim c1 As ArraySort = CType(a, ArraySort) Dim c2 As ArraySort = CType(b, ArraySort) If (c1.Value > c2.Value) Then Return 1 End If If (c1.Value < c2.Value) Then Return -1 Else Return 0 End If End Function End Class Private Class sortByValueDescendingHelper : Implements IComparer Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements IComparer.Compare Dim c1 As ArraySort = CType(a, ArraySort) Dim c2 As ArraySort = CType(b, ArraySort) If (c1.Value < c2.Value) Then Return 1 End If If (c1.Value > c2.Value) Then Return -1 Else Return 0 End If End Function End Class Private Class sortKeyDescendingHelper : Implements IComparer Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements IComparer.Compare Dim c1 As ArraySort = CType(a, ArraySort) Dim c2 As ArraySort = CType(b, ArraySort) Return String.Compare(c2.mKey, c1.mKey) End Function End Class ' End nested classes. Private mValue As Integer Private mKey As String Public Sub New(ByVal Value As Integer, ByVal Key As String)mKey = Key
mValue = Value
End Sub Public Property Value() As Integer Get Return mValue End Get Set(ByVal Value As Integer)mValue = Value
End Set End Property Public Property Key() As String Get Return mKey End Get Set(ByVal Value As String)mKey = Value
End Set End Property Function CompareTo(ByVal obj As Object) As Integer Implements IComparable.CompareTo Dim c As ArraySort = CType(obj, ArraySort) Return String.Compare(Me.mKey, c.mKey) End Function Public Shared Function sortByValueAscending() As IComparer Return CType(New sortByValueAscendingHelper, IComparer) End Function Public Shared Function sortByValueDescending() As IComparer Return CType(New sortByValueDescendingHelper, IComparer) End Function Public Shared Function sortByKeyDescending() As IComparer Return CType(New sortKeyDescendingHelper, IComparer) End FunctionEnd
ClassNow you are able to sort by KEY (display data) or Value either ascending or descending.
Hopefully this solves your issue..
Good Post!!
Just wanted to make an additional comment.
By changing the following lines:
Dim names As String() = ContractType.GetNames(enumType)
Dim values As Array = ContractType.GetValues(enumType)
TO:
Dim names As String() = System.Enum.GetNames(enumType)
Dim values As Array = System.Enum.GetValues(enumType)
You make this process reusable.
Using Hashtable it is not possible.
Instead of HashTable use an ArrayList
ht=new ArrayList();
And change the code like this.
ht.Insert((int)values.GetValue(i),names);
No need to specify the DataTextField and DataValueField of the Dropdownlist
When we bind the Hashtable to the DropDown the list get the values in a random order?
How do i resolve this .
Suppose my list contains days,then
I want my days to be displayed in the order for eg. Sunday - Saturday...
ehh. I'm trying to make the public shared sub general by passing in the enum as a parameter. I'm having trouble. Is this not possible?
If you just want the names, and don't need the values, you could just do:
list.DataSource = System.Enum.GetNames(typeof(MyEnum));
There's probably a trick to using GetNames and GetValues to quickly get what you need without having to loop over the enum.
ah yes.. of course... thanks
yes
u will get that error if u r using windows form in windows form
u can use a datatable instead
Private Enum ContractType
Permanent = 1
Contract = 2
Internship = 99
End Enum
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.DataSource = BindToEnum(GetType(ContractType))
ComboBox1.DisplayMember = "Key"
ComboBox1.ValueMember = "Value"
End Sub
Public Shared Function BindToEnum(ByVal enumType As Type) As DataTable
Dim names As String() = ContractType.GetNames(enumType)
Dim values As Array = ContractType.GetValues(enumType)
Dim dt As New DataTable
dt.Columns.Add("Key", GetType(String))
dt.Columns.Add("Value", GetType(Integer))
Dim i As Integer = 0
While i < names.Length
Dim dr As DataRow = dt.NewRow
dr("Key") = names(i)
dr("Value") = CType(values.GetValue(i), Integer)
dt.Rows.Add(dr)
i = i + 1
End While
Return dt
End Function
Thanks man
This thread is for discussions of Binding a Control to an Enum.