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?
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
' 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]
"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...
"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]
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?
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