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?