在DataGridView中显示自定义类Enum属性的可编辑下拉列表

时间:2010-11-15 10:26:59

标签: .net datagridview enums combobox

我将自定义类绑定到Datagridview,并希望为其中一个Enum属性显示Editable combobox。

Public Class Contact

    Public Enum GenderTypes
        Male
        Female
    End Enum

    Private _Firstname As String
    Private _Lastname As String
    Private _Gender As GenderTypes

    Public Property FirstName() As String
        Get
            Return Me._Firstname
        End Get
        Set(ByVal value As String)
            Me._Firstname = value
        End Set
    End Property

    Public Property LastName() As String
        Get
            Return Me._Lastname
        End Get
        Set(ByVal value As String)
            Me._Lastname = value
        End Set
    End Property

    Public Property Gender() As GenderTypes
        Get
            Return Me._Gender
        End Get
        Set(ByVal value As GenderTypes)
            Me._Gender = value
        End Set
    End Property

End Class

在Form1中我绑定了List(Of Contact),如下所示。

Dim mContacts As List(Of Contact) = New List(Of Contact)
dgContacts.DataSource = mContacts

现在,当我在datagridview中运行应用程序时,没有为我的自定义类的性别枚举属性创建可编辑的组合框。我尝试创建自定义EnumConverter,但没有将枚举属性设置为可编辑的下拉列表。

请告诉我如何在datagridview中为我的自定义类枚举属性获取可编辑的组合框/下拉列表。

1 个答案:

答案 0 :(得分:1)

在datagridview的EditingControlShowing事件中,将该列comobo框的下拉样式设置为DropDown。以下是样本。

if (MyGridView.CurrentCell.ColumnIndex.Equals(GenderColumn.Index) && (e.Control is ComboBox))
  {
    var genderCombox = e.Control as ComboBox;
    genderCombox.DropDownStyle = ComboBoxStyle.DropDown;
  }

这会使您的性别组合框可编辑。