Bind Enum to ComboBox in GridView

B

Brien King

I am binding to a collection of Business Objects in a GridView (Windows App,
..NET 2.0).

In my Business Object I have a Property that is an ENUM.

I want a Drop down box in the Grid that is Bound to the property that is of
the type of the ENUM. I want the combo box filled in with the list of ENUM
values.

Does the GridView support this?


Brien King
(e-mail address removed)
 
K

Ken Tucker [MVP]

Hi,

You can try something like this. You need to manually remove the
column from the gridview and add the column back in. You can see a
complete example here

http://www.vb-tips.com/default.aspx?ID=492bcbbc-f881-45c8-9b3a-62c7a184f472

Here is the code. I did not set the column the datagridview combo is bound
to. It will show the display member but keep the value member as its value.


Dim lstEnum As New List(Of enumBindTo)
Dim arColor() As String
arColor = KnownColor.GetNames(GetType(KnownColor))

For x As Int16 = 0 To arColor.GetUpperBound(0)
Dim cls As New enumBindTo
With cls
.EnumValue = x
.StringValue = arColor(x)
End With
Next

Dim dgvEnum As New DataGridViewComboBoxColumn
With dgvEnum
.Width = 150
.DataSource = lstEnum
.ValueMember = "EnumValue"
.DisplayMember = "StringValue"
.HeaderText = "Color"
End With

DataGridView1.Columns.Add(dgvEnum)

The class I used

Public Class enumBindTo

Private mintEnumValue As Integer
Public Property EnumValue() As Integer
Get
Return mintEnumValue
End Get
Set(ByVal value As Integer)
mintEnumValue = value
End Set
End Property

Private mstrValue As String
Public Property StringValue() As String
Get
Return mstrValue
End Get
Set(ByVal value As String)
mstrValue = value
End Set
End Property

End Class

Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top