Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

[4713] Binding a Control to an Enum

Last post 08-07-2007 2:11 PM by Vphillips. 12 replies.
Page 1 of 1 (13 items)
Sort Posts: Previous Next
  • 01-01-1999 12:00 AM

    [4713] Binding a Control to an Enum

    This thread is for discussions of Binding a Control to an Enum.

    • Post Points: 45
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 07-31-2005 12:30 PM In reply to

    • zorgster
    • Not Ranked
    • Joined on 07-31-2005
    • New Member
    • Points 75

    Enum in VB.NET

    Is it posible to do something similar in a Windows Forms application?  I have tried the code in VB.NET and get an error that complex binding is not allowed... The DataSource must be an IList or IListSource...?

    • Post Points: 0
  • 08-04-2005 5:15 AM In reply to

    Nice Article

    Thanks man
    • Post Points: 0
  • 08-04-2005 7:06 AM In reply to

    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

    • Post Points: 10
  • 08-04-2005 7:34 AM In reply to

    • zorgster
    • Not Ranked
    • Joined on 07-31-2005
    • New Member
    • Points 75
    ah yes.. of course... thanks
    • Post Points: 0
  • 09-22-2005 6:57 PM In reply to

    • CoryFoy
    • Not Ranked
    • Joined on 09-22-2005
    • New Member
    • Points 5

    System.Enum

    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.
    • Post Points: 0
  • 10-21-2005 10:53 PM In reply to

    • mulmad
    • Not Ranked
    • Joined on 06-18-2003
    • New Member
    • Points 5
    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?
    • Post Points: 0
  • 01-10-2006 6:39 AM In reply to

    • inferano
    • Top 150 Contributor
    • Joined on 06-27-2005
    • Fanatic Member
    • Points 1,220

    How do u set the order in 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...

     
    And God said "Let there be Light." ... [Genesis 1:3]
    • Post Points: 10
  • 01-13-2006 7:30 AM In reply to

    Instead use an arraylist

    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


    • Post Points: 0
  • 12-06-2006 6:17 PM In reply to

    • ezztek
    • Not Ranked
    • Joined on 12-06-2006
    • New Member
    • Points 10

    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.

    • Post Points: 5
  • 12-07-2006 6:17 PM In reply to

    • ezztek
    • Not Ranked
    • Joined on 12-06-2006
    • New Member
    • Points 10

    Re: How do u set the order in dropdownList

    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 = 0

    names = System.Enum.GetNames(enumType)

    values = System.Enum.GetValues(enumType)

    Dim arrayOfEnum(names.Length - 1) As HW.ArraySort

    While x < names.Length

    arrayOfEnum(x) =

    New HW.ArraySort(CType(values.GetValue(x), Integer), names(x))

    x = x + 1

    End While

    Array.Sort(arrayOfEnum)

    Return arrayOfEnum

    End Function

     

    You will now be returning an ArrayList.  You will also need to add the following code in a module file:

    Option

    Explicit On

    Option

    Strict On

    Imports

    System.Collections

    Public

    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 Function

    End

    Class

    Now you are able to sort by KEY (display data) or Value either ascending or descending.

    Hopefully this solves your issue..

    • Post Points: 5
  • 02-22-2007 5:43 PM In reply to

    Re: [4713] Binding a Control to an Enum

    I am binding an ASP.net control using the above methods. For some reason each of the item in dropdownlist is showing System.Collections.DictionaryEntry as text and value, although I checked the key value pair in hash table and it has same values as enum. Makes me wonder what happens during databinding? How can I get the right sat of text and value in my dropdown list? Any idea?
    • Post Points: 5
  • 08-07-2007 2:11 PM In reply to

    • Vphillips
    • Not Ranked
    • Joined on 08-07-2007
    • United Kingdom
    • New Member
    • Points 5

    Re: [4713] Binding a Control to an Enum

    Why not just use:

    MyDropDownList.DataSource = Enum.GetNames(typeof(ContractType));

    • Post Points: 5
Page 1 of 1 (13 items)