473,569 Members | 2,834 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4443
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" <Na****@discuss ions.microsoft. com> wrote in message
news:CD******** *************** ***********@mic rosoft.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 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" <Na****@discuss ions.microsoft. com> wrote in message
news:CD******** *************** ***********@mic rosoft.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****@discuss ions.microsoft. com> wrote in message
news:CD******** *************** ***********@mic rosoft.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 DescriptionAttr ibute 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****@discuss ions.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.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/>

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

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

May 2 '06 #8

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

Similar topics

1
12623
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 value="red"/> <xsd:enumeration value="yellow"/> <xsd:enumeration value="blue"/> </xsd:simpleType>
1
2030
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 problem is this: the schema contains an element called "TRENDED" and it has an attribute called "type" whose value can be "previous" or "current". This...
13
3029
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: to catch unhandled cases, but of course this is only at run-time. Example: public enum MyEnum { one, two, three, four }
4
5648
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" for a second if you don't mind). However, it existed within intuitive language semantics, whereas the new "yield" keyword, while highly...
2
1718
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 binding, so if I have an array with class level scope and I am operating on that array at procedure level I fabricate a "dummy array" and copy the...
0
3526
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: Parameters = new object; Parameters = "fileName.mht"; Parameters = WdSaveFormat.wdFormatWebArchive; Parameters = false;
0
2317
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 through internet explorer when hitting the ..asmx url, i cannot generate a proxy class to use in my winforms assembly. When i try to generate a...
5
3490
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
3620
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 way to databind like when using a table as list backing store? Thanks, Charlie
0
7693
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.