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

发布于 2024-10-02 07:16:14 字数 1209 浏览 2 评论 0原文

我将自定义类绑定到 Datagridview 并希望显示 Enum 属性之一的可编辑组合框。

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 中,我绑定列表(联系人),如下所示。

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

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

请让我知道如何在 datagridview 中获取自定义类枚举属性的可编辑组合框/下拉菜单。

I am binding custom class to Datagridview and want to show Editable combobox for one of the Enum property.

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

In Form1 i am binding List(Of Contact) like following.

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

Now,when i run the application in datagridview didn't create editable combobox for gender enum property of my custom class. I tried to create custom EnumConverter but didn't make the enum property to editable dropdown.

Please let me know how i'll get the editable combobox/dropdown in datagridview for my custom class enum property.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱*していゐ 2024-10-09 07:16:14

在 datagridview 的 EditingControlShowing 事件中,将该列组合框的下拉样式设置为 DropDown。下面是示例。

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

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

In the EditingControlShowing event of your datagridview, set the drop down style of that column comobo box to DropDown. Below is the sample.

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

This should make your gender combo box editable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文