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 d开发者_如何学Goatagridview for my custom class enum property.
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.
精彩评论