Community discussion forum

Binding a Control to an Enum

This is a comment thread discussing Binding a Control to an Enum
  • 9 years ago

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

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 3 years ago

    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...?


  • 3 years ago

    Thanks man

  • 3 years ago

    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


  • 3 years ago

    ah yes.. of course... thanks

  • 3 years ago

    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.

  • 3 years ago

    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?

  • 2 years ago

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


     

  • 2 years ago

    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



  • 1 year ago

    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.

  • 1 year ago

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

  • 1 year ago

    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?

  • 1 year ago

    Why not just use:

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

Post a reply

Enter your message below

Sign in or Join us (it's free).