How to make a ComboBox of a DataGridViewComboBoxColumn accept user new items?
Using a DataGridViewComboBoxColumn, the goal is to make the ComboBoxes accept user's new items and also the possibility of choosing items that are already present.
I'm aware of using EditingControlShowing event of the DataGridView to change the DropDownStyle of the DataGridViewComboBoxEditingControl at run time to allow this, but I'm wondering if this can be done at a lower level.
What I'm doing now is extending DataGridViewComboBoxColumn, DataGridViewComboBoxCell and DataGridViewComboBoxEditingControl, hoping to change the EditingControl's DropDownStyle in the moment I instantiate it. So far, no luck.
The debugger shows the right assignment is being executed, but nonetheless, the DropDownStyle is popping at the EditingControlShowing (using the event for debugging purposes) as DropBoxList, not DropBox, which is the intent.
Here follow the classes:
Public Class DataGridViewComboBoxColumnALT
Inherits System.Windows.Forms.DataGridViewComboBoxColumn
Public Sub New()
开发者_如何学JAVAMe.CellTemplate = New DataGridViewComboBoxCellALT
End Sub
End Class
Public Class DataGridViewComboBoxCellALT
Inherits System.Windows.Forms.DataGridViewComboBoxCell
Public Overrides ReadOnly Property EditType() As Type
Get
Return GetType(DataGridViewComboBoxEditingControlALT)
End Get
End Property
End Class
Public Class DataGridViewComboBoxEditingControlALT
Inherits System.Windows.Forms.DataGridViewComboBoxEditingControl
Implements System.Windows.Forms.IDataGridViewEditingControl
Public Sub New()
MyBase.New()
Me.DropDownStyle = ComboBoxStyle.DropBox
End Sub
End Class
You need to set DropDownStyle of DataGridView.EditingControl @ InitializeEditingControl method of Inherted cell class
class DataGridViewComboBoxCellExtended : DataGridViewComboBoxCell
{
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
((DataGridViewComboBoxEditingControlExtended)DataGridView.EditingControl).DropDownStyle = ComboBoxStyle.DropDown;
}
public override Type EditType
{
get
{
return typeof(DataGridViewComboBoxEditingControlExtended);
}
}
I argued with the DataGridViewComboBox for ages. It's awful. I eventually used the old trick of creating a (non-datagridview) ComboBox at runtime and floating it over the grid.
精彩评论