Bind Enum to ComboBox in GridView

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brien King

    #1

    Bind Enum to ComboBox in GridView

    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
    brien@classic-soft.com


  • Ken Tucker [MVP]

    #2
    Re: Bind Enum to ComboBox in GridView

    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



    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.GetN ames(GetType(Kn ownColor))

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

    Dim dgvEnum As New DataGridViewCom boBoxColumn
    With dgvEnum
    .Width = 150
    .DataSource = lstEnum
    .ValueMember = "EnumValue"
    .DisplayMember = "StringValu e"
    .HeaderText = "Color"
    End With

    DataGridView1.C olumns.Add(dgvE num)

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

    "Brien King" <brien@classi c-soft.com> wrote in message
    news:eZcl9ZUMGH A.1532@TK2MSFTN GP12.phx.gbl...[color=blue]
    >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
    > brien@classic-soft.com
    >
    >[/color]


    Comment

    Working...