Binding to an enumeration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nathan

    #1

    Binding to an enumeration

    I'd like to use an enumeration as a datasource for a drop-down box. Is there
    a way to do this?
  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Binding to an enumeration

    Nathan,
    Have you tried something like:

    Public Enum SomeEnum
    Value1 = 1
    Value2 = 2
    End Enum

    Dim list As Array = [Enum].GetValues(GetT ype(SomeEnum))

    Then bind the above list variable?

    Some people bind to the names, via [Enum].GetNames, as opposed to the
    values. I prefer using the values as above In Windows Forms as the
    ComboBox.Select edValue is the enum value rather then a string... In Web
    Forms I don't see an advantage to using the values, but then again I have
    not bound to enums in web forms...

    --
    Hope this helps
    Jay B. Harlow [MVP - Outlook]
    ..NET Application Architect, Enthusiast, & Evangelist
    T.S. Bradley - http://www.tsbradley.net


    "Nathan" <Nathan@discuss ions.microsoft. com> wrote in message
    news:CD812C76-D8B2-49AE-8194-6D5AD2095068@mi crosoft.com...
    | I'd like to use an enumeration as a datasource for a drop-down box. Is
    there
    | a way to do this?


    Comment

    • David Lee Conley

      #3
      Re: Binding to an enumeration

      Nathan,

      What I've pasted below is from the VS2005 Help documentation. It doesn't
      specify for which version of .NET it's for, so I can't guarantee it'll work
      for all versions.

      Dave
      _______________ _______________ _______________ ____________
      The following code example demonstrates how to bind a collection of objects
      to a DataGridView control so that each object displays as a separate row.
      This example also illustrates how to display a property with an enumeration
      type in a DataGridViewCom boBoxColumn so that the combo box drop-down list
      contains the enumeration values.

      This example requires:

      a.. References to the System and System.Windows. Forms assemblies.

      Imports System.Windows. Forms
      Imports System.Collecti ons.Generic

      Public Enum Title
      King
      Sir
      End Enum

      Public Class EnumsAndComboBo x
      Inherits Form

      Private flow As New FlowLayoutPanel ()
      Private WithEvents checkForChange As Button = New Button()
      Private knights As List(Of Knight)
      Private dataGridView1 As New DataGridView()

      Public Sub New()
      MyBase.New()
      SetupForm()
      SetupGrid()
      End Sub

      Private Sub SetupForm()
      AutoSize = True
      End Sub

      Private Sub SetupGrid()
      knights = New List(Of Knight)
      knights.Add(New Knight(Title.Ki ng, "Uther", True))
      knights.Add(New Knight(Title.Ki ng, "Arthur", True))
      knights.Add(New Knight(Title.Si r, "Mordred", False))
      knights.Add(New Knight(Title.Si r, "Gawain", True))
      knights.Add(New Knight(Title.Si r, "Galahad", True))

      ' Initialize the DataGridView.
      dataGridView1.A utoGenerateColu mns = False
      dataGridView1.A utoSize = True
      dataGridView1.D ataSource = knights

      dataGridView1.C olumns.Add(Crea teComboBoxWithE nums())

      ' Initialize and add a text box column.
      Dim column As DataGridViewCol umn = _
      New DataGridViewTex tBoxColumn()
      column.DataProp ertyName = "Name"
      column.Name = "Knight"
      dataGridView1.C olumns.Add(colu mn)

      ' Initialize and add a check box column.
      column = New DataGridViewChe ckBoxColumn()
      column.DataProp ertyName = "GoodGuy"
      column.Name = "Good"
      dataGridView1.C olumns.Add(colu mn)

      ' Initialize the form.
      Controls.Add(da taGridView1)
      Me.AutoSize = True
      Me.Text = "DataGridVi ew object binding demo"
      End Sub

      Private Function CreateComboBoxW ithEnums() As DataGridViewCom boBoxColumn
      Dim combo As New DataGridViewCom boBoxColumn()
      combo.DataSourc e = [Enum].GetValues(GetT ype(Title))
      combo.DataPrope rtyName = "Title"
      combo.Name = "Title"
      Return combo
      End Function

      #Region "business object"
      Private Class Knight
      Private hisName As String
      Private good As Boolean
      Private hisTitle As Title

      Public Sub New(ByVal title As Title, ByVal name As String, _
      ByVal good As Boolean)

      hisTitle = title
      hisName = name
      Me.good = good
      End Sub

      Public Property Name() As String
      Get
      Return hisName
      End Get

      Set(ByVal Value As String)
      hisName = Value
      End Set
      End Property

      Public Property GoodGuy() As Boolean
      Get
      Return good
      End Get
      Set(ByVal Value As Boolean)
      good = Value
      End Set
      End Property

      Public Property Title() As Title
      Get
      Return hisTitle
      End Get
      Set(ByVal Value As Title)
      hisTitle = Value
      End Set
      End Property
      End Class
      #End Region

      Public Shared Sub Main()
      Application.Run (New EnumsAndComboBo x())
      End Sub

      End Class

      "Nathan" <Nathan@discuss ions.microsoft. com> wrote in message
      news:CD812C76-D8B2-49AE-8194-6D5AD2095068@mi crosoft.com...[color=blue]
      > I'd like to use an enumeration as a datasource for a drop-down box. Is
      > there
      > a way to do this?[/color]


      Comment

      • Mythran

        #4
        Re: Binding to an enumeration


        "Nathan" <Nathan@discuss ions.microsoft. com> wrote in message
        news:CD812C76-D8B2-49AE-8194-6D5AD2095068@mi crosoft.com...[color=blue]
        > I'd like to use an enumeration as a datasource for a drop-down box. Is
        > there
        > a way to do this?[/color]

        What we have done is created a method that takes a drop-down listbox
        control, enumeration type, and default value. For each enum value, we added
        a Description attribute that describes the value. Then instead of
        displaying the enumeration names/values, we display (using reflection) the
        values of the DescriptionAttr ibute for each enumeration value. This way,
        each item displayed in the drop down is displayed in a more descriptive
        way...

        HTH,
        Mythran

        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Binding to an enumeration

          "Nathan" <Nathan@discuss ions.microsoft. com> schrieb:[color=blue]
          > I'd like to use an enumeration as a datasource for a drop-down box. Is
          > there
          > a way to do this?[/color]

          See:

          <URL:http://groups.google.d e/group/microsoft.publi c.dotnet.langua ges.vb/msg/6ded56f06760df6 9>

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://classicvb.org/petition/>

          Comment

          • Dennis

            #6
            RE: Binding to an enumeration

            The following code binds an enum to a combo-box and then retrieves the Enum
            that is selected in the combo-box:

            dim mySelectedEnum as myEnum

            'Display the Enum names in the combo box
            myComboBox.Data Source = [Enum].GetNames(GetTy pe(myEnum))

            'Get the selected Enum
            mySelectedEnum = CType([Enum].Parse(GetType( myEnum), _
            myComboBox.Sele ctedItem.ToStri ng), myEnum)

            Hope this helps.
            --
            Dennis in Houston


            "Nathan" wrote:
            [color=blue]
            > I'd like to use an enumeration as a datasource for a drop-down box. Is there
            > a way to do this?[/color]

            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: Binding to an enumeration

              Dennis,
              The following code "simplifies " this concept:

              | dim mySelectedEnum as myEnum
              |
              | 'Display the Enum names in the combo box
              | myComboBox.Data Source = [Enum].GetValues(GetT ype(myEnum))
              |
              | 'Get the selected Enum
              | mySelectedEnum = DirectCast(myCo mboBox.Selected Item, myEnum)

              Notice the actual bind is the same amount of code, however the get selected
              value is significantly simplified.

              --
              Hope this helps
              Jay B. Harlow [MVP - Outlook]
              ..NET Application Architect, Enthusiast, & Evangelist
              T.S. Bradley - http://www.tsbradley.net


              "Dennis" <Dennis@discuss ions.microsoft. com> wrote in message
              news:547F594D-2C20-477F-A3AB-486F84AE2003@mi crosoft.com...
              | The following code binds an enum to a combo-box and then retrieves the
              Enum
              | that is selected in the combo-box:
              |
              | dim mySelectedEnum as myEnum
              |
              | 'Display the Enum names in the combo box
              | myComboBox.Data Source = [Enum].GetNames(GetTy pe(myEnum))
              |
              | 'Get the selected Enum
              | mySelectedEnum = CType([Enum].Parse(GetType( myEnum), _
              | myComboBox.Sele ctedItem.ToStri ng), myEnum)
              |
              | Hope this helps.
              | --
              | Dennis in Houston
              |
              |
              | "Nathan" wrote:
              |
              | > I'd like to use an enumeration as a datasource for a drop-down box. Is
              there
              | > a way to do this?


              Comment

              • Dennis

                #8
                Re: Binding to an enumeration

                Thanks for shortened tip. I was really trying to write it such that it was
                general for comboboxes that were only strings that would convert a string
                entered into a Enum.
                --
                Dennis in Houston


                "Jay B. Harlow [MVP - Outlook]" wrote:
                [color=blue]
                > Dennis,
                > The following code "simplifies " this concept:
                >
                > | dim mySelectedEnum as myEnum
                > |
                > | 'Display the Enum names in the combo box
                > | myComboBox.Data Source = [Enum].GetValues(GetT ype(myEnum))
                > |
                > | 'Get the selected Enum
                > | mySelectedEnum = DirectCast(myCo mboBox.Selected Item, myEnum)
                >
                > Notice the actual bind is the same amount of code, however the get selected
                > value is significantly simplified.
                >
                > --
                > Hope this helps
                > Jay B. Harlow [MVP - Outlook]
                > ..NET Application Architect, Enthusiast, & Evangelist
                > T.S. Bradley - http://www.tsbradley.net
                >
                >
                > "Dennis" <Dennis@discuss ions.microsoft. com> wrote in message
                > news:547F594D-2C20-477F-A3AB-486F84AE2003@mi crosoft.com...
                > | The following code binds an enum to a combo-box and then retrieves the
                > Enum
                > | that is selected in the combo-box:
                > |
                > | dim mySelectedEnum as myEnum
                > |
                > | 'Display the Enum names in the combo box
                > | myComboBox.Data Source = [Enum].GetNames(GetTy pe(myEnum))
                > |
                > | 'Get the selected Enum
                > | mySelectedEnum = CType([Enum].Parse(GetType( myEnum), _
                > | myComboBox.Sele ctedItem.ToStri ng), myEnum)
                > |
                > | Hope this helps.
                > | --
                > | Dennis in Houston
                > |
                > |
                > | "Nathan" wrote:
                > |
                > | > I'd like to use an enumeration as a datasource for a drop-down box. Is
                > there
                > | > a way to do this?
                >
                >
                >[/color]

                Comment

                Working...