问题
I am using a DataGridView
, created using the designer with a few columns including a DataGridViewComboBoxColumn
column.
It is slightly irritating that I have to click on each cell twice or even three times to display the drop-list:
- If I click over the text part, it takes 3 clicks!
- If I click on the down arroow, only two clicks:
I assume it's due to the cell using the first click to get focus, but is there a way to address the issue so clicking on a cell displays the combobox right away? I note that using DataGridViewCheckBoxColumn
the same issue does not happen... clicking on a checkbox immediately toggles it regardless if that cell had focus.
回答1:
You can simply set EditMode property of your DataGridView
to EditOnEnter
.
It makes editing more easy. Almost single click, but if you want even when you click on content of combobox show dropdown for your ComboBoxColumn
immediately, you can handle CellClick event and then use EditingControl of your grid and cast it to DataGridViewComboBoxEditingControl and makes it to show dropdown.
private void categoryDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
//You can check for e.ColumnIndex to limit this to your specific column
var editingControl = this.categoryDataGridView.EditingControl as
DataGridViewComboBoxEditingControl;
if (editingControl != null)
editingControl.DroppedDown = true;
}
Be careful when using this trick, you may make drop downs annoying for users when they only want to navigate between cells without editing.
来源:https://stackoverflow.com/questions/32947475/datagridviewcomboboxcolumn-have-to-click-cell-twice-to-display-combo-box