Home All Groups Group Topic Archive Search About

Binding to an enumeration

Author
1 May 2006 11:35 AM
Nathan
I'd like to use an enumeration as a datasource for a drop-down box.  Is there
a way to do this?

Author
1 May 2006 12:09 PM
Jay B. Harlow [MVP - Outlook]
Nathan,
Have you tried something like:

    Public Enum SomeEnum
        Value1 = 1
        Value2 = 2
    End Enum

        Dim list As Array = [Enum].GetValues(GetType(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.SelectedValue 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


Show quoteHide quote
"Nathan" <Nat***@discussions.microsoft.com> wrote in message
news:CD812C76-D8B2-49AE-8194-6D5AD2095068@microsoft.com...
| I'd like to use an enumeration as a datasource for a drop-down box.  Is
there
| a way to do this?
Author
1 May 2006 12:12 PM
David Lee Conley
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 DataGridViewComboBoxColumn 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.Collections.Generic

Public Enum Title
    King
    Sir
End Enum

Public Class EnumsAndComboBox
    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.King, "Uther", True))
        knights.Add(New Knight(Title.King, "Arthur", True))
        knights.Add(New Knight(Title.Sir, "Mordred", False))
        knights.Add(New Knight(Title.Sir, "Gawain", True))
        knights.Add(New Knight(Title.Sir, "Galahad", True))

        ' Initialize the DataGridView.
        dataGridView1.AutoGenerateColumns = False
        dataGridView1.AutoSize = True
        dataGridView1.DataSource = knights

        dataGridView1.Columns.Add(CreateComboBoxWithEnums())

        ' Initialize and add a text box column.
        Dim column As DataGridViewColumn = _
            New DataGridViewTextBoxColumn()
        column.DataPropertyName = "Name"
        column.Name = "Knight"
        dataGridView1.Columns.Add(column)

        ' Initialize and add a check box column.
        column = New DataGridViewCheckBoxColumn()
        column.DataPropertyName = "GoodGuy"
        column.Name = "Good"
        dataGridView1.Columns.Add(column)

        ' Initialize the form.
        Controls.Add(dataGridView1)
        Me.AutoSize = True
        Me.Text = "DataGridView object binding demo"
    End Sub

    Private Function CreateComboBoxWithEnums() As DataGridViewComboBoxColumn
        Dim combo As New DataGridViewComboBoxColumn()
        combo.DataSource = [Enum].GetValues(GetType(Title))
        combo.DataPropertyName = "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 EnumsAndComboBox())
    End Sub

End Class

Show quoteHide quote
"Nathan" <Nat***@discussions.microsoft.com> wrote in message
news:CD812C76-D8B2-49AE-8194-6D5AD2095068@microsoft.com...
> I'd like to use an enumeration as a datasource for a drop-down box.  Is
> there
> a way to do this?
Author
1 May 2006 5:25 PM
Mythran
"Nathan" <Nat***@discussions.microsoft.com> wrote in message
news:CD812C76-D8B2-49AE-8194-6D5AD2095068@microsoft.com...
> I'd like to use an enumeration as a datasource for a drop-down box.  Is
> there
> a way to do this?

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 DescriptionAttribute for each enumeration value.  This way,
each item displayed in the drop down is displayed in a more descriptive
way...

HTH,
Mythran
Author
1 May 2006 5:30 PM
Herfried K. Wagner [MVP]
"Nathan" <Nat***@discussions.microsoft.com> schrieb:
> I'd like to use an enumeration as a datasource for a drop-down box.  Is
> there
> a way to do this?

See:

<URL:http://groups.google.de/group/microsoft.public.dotnet.languages.vb/msg/6ded56f06760df69>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
2 May 2006 12:36 AM
Dennis
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.DataSource = [Enum].GetNames(GetType(myEnum))

'Get the selected Enum
mySelectedEnum = CType([Enum].Parse(GetType(myEnum), _
myComboBox.SelectedItem.ToString), myEnum)

Hope this helps.
--
Dennis in Houston


Show quoteHide quote
"Nathan" wrote:

> I'd like to use an enumeration as a datasource for a drop-down box.  Is there
> a way to do this?
Author
2 May 2006 3:11 AM
Jay B. Harlow [MVP - Outlook]
Dennis,
The following code "simplifies" this concept:

| dim mySelectedEnum as myEnum
|
| 'Display the Enum names in the combo box
| myComboBox.DataSource = [Enum].GetValues(GetType(myEnum))
|
| 'Get the selected Enum
| mySelectedEnum = DirectCast(myComboBox.SelectedItem, 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


Show quoteHide quote
"Dennis" <Den***@discussions.microsoft.com> wrote in message
news:547F594D-2C20-477F-A3AB-486F84AE2003@microsoft.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.DataSource = [Enum].GetNames(GetType(myEnum))
|
| 'Get the selected Enum
| mySelectedEnum = CType([Enum].Parse(GetType(myEnum), _
| myComboBox.SelectedItem.ToString), 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?
Author
2 May 2006 11:31 PM
Dennis
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


Show quoteHide quote
"Jay B. Harlow [MVP - Outlook]" wrote:

> Dennis,
> The following code "simplifies" this concept:
>
> | dim mySelectedEnum as myEnum
> |
> | 'Display the Enum names in the combo box
> | myComboBox.DataSource = [Enum].GetValues(GetType(myEnum))
> |
> | 'Get the selected Enum
> | mySelectedEnum = DirectCast(myComboBox.SelectedItem, 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" <Den***@discussions.microsoft.com> wrote in message
> news:547F594D-2C20-477F-A3AB-486F84AE2003@microsoft.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.DataSource = [Enum].GetNames(GetType(myEnum))
> |
> | 'Get the selected Enum
> | mySelectedEnum = CType([Enum].Parse(GetType(myEnum), _
> | myComboBox.SelectedItem.ToString), 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?
>
>
>