Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridViewComboBoxColumn - Have to click cell twice to display combo box

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:

  1. If I click over the text part, it takes 3 clicks!

enter image description here enter image description here enter image description here

  1. If I click on the down arroow, only two clicks:

enter image description here enter image description here

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.

like image 657
Mr. Boy Avatar asked Oct 05 '15 11:10

Mr. Boy


1 Answers

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.

like image 163
Reza Aghaei Avatar answered Sep 19 '22 12:09

Reza Aghaei