473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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?
May 1 '06 #1
7 4428
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
"Nathan" <Na****@discussions.microsoft.com> wrote in message
news:CD**********************************@microsof t.com...
| I'd like to use an enumeration as a datasource for a drop-down box. Is
there
| a way to do this?
May 1 '06 #2
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

"Nathan" <Na****@discussions.microsoft.com> wrote in message
news:CD**********************************@microsof t.com...
I'd like to use an enumeration as a datasource for a drop-down box. Is
there
a way to do this?

May 1 '06 #3

"Nathan" <Na****@discussions.microsoft.com> wrote in message
news:CD**********************************@microsof t.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

May 1 '06 #4
"Nathan" <Na****@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/>

May 1 '06 #5
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?

May 2 '06 #6
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" <De****@discussions.microsoft.com> wrote in message
news:54**********************************@microsof t.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?
May 2 '06 #7
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:
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" <De****@discussions.microsoft.com> wrote in message
news:54**********************************@microsof t.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?

May 2 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Justin Wright | last post by:
I know that I can set up an enumeration as follows ( just typed in quick so may have syntax errors ): <xsd:simpleType name="colors"> <xsd:restriction base="xsd:string"> <xsd:enumeration...
1
by: Andy Wells | last post by:
I'm using VB.NET and I have an application that binds a schema to the main form's controls, and the user has the ability to load an XML file through the schema and into the bound form. My...
13
by: Adam Blair | last post by:
Is it possible to bind a switch statement to an Enum such that a compile-time error is raised if not all values within the Enum are handled in the switch statement? I realise you can use default:...
4
by: Marshal | last post by:
Sure... IEnumerable was inconvenient suggesting a separate class to service the enumeration, IEnumerator, and multiple operations: Current, MoveNext, Reset. (I'll warp the definition of "operation"...
2
by: mark | last post by:
I understand that writing programs with option strict on is the best way to obtain stable applications. I have also found the applications to run much faster. Option strict on disallows late...
0
by: mancha28 | last post by:
Hi, I have create Word object by late binding to open a Word document and save it under another name. This new saved document should be saved as mht-file. Following code I impelemented: ...
0
by: Steven Bolard | last post by:
Hello, I am trying to port my .net 1.1 application to 2.0. I am using vs2005. I am trying to get my webservices to run and although i can compile them and and get wsdl and service descriptions...
5
by: Nathan | last post by:
I'd like to use an enumeration as a datasource for a drop-down box. Is there a way to do this?
2
by: Charlie | last post by:
Hi: How would I bind a list to an enumeration so that enumeration value becomes list values and enumeration constants becomes list text? Do you loop through enum and add manually or is there a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.