Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridViewComboBoxColumn doesn't open the dropdown on first click

Before anyone marks this as duplicate, plz note that this is not the same as the questions asked here, here and here.

When you have two or more DataGridViewComboBoxColumns in your DataGridView and one of them has its dropdown currently open, clicking on the dropdown button of a different column does not open that dropdown. Instead you still have to click twice. First click is consumed in hiding the already open dropdown and the second click actually opens the dropdown on which you click.

Note that two clicks are required when EditOnEnter mode is ON; else you'll have to perform three clicks to get this done. I have tried ContentClick event too, without any gain.

So how can I use one-click operation in cases when I have more than one DataGridViewComboBoxColumn in my grid?

Update

Just in case anyone wants to reproduce it, here is the process:

  1. Create a new WinForms C# project.
  2. Go to Form1's code and paste this in the constructor after the InitializeComponent line:

    DataGridView dgv = new DataGridView();
    DataGridViewComboBoxColumn col1 = new DataGridViewComboBoxColumn();
    DataGridViewComboBoxColumn col2 = new DataGridViewComboBoxColumn();
    
    dgv.Columns.AddRange(new DataGridViewColumn[] { col1, col2 });
    dgv.Dock = DockStyle.Fill;
    dgv.EditMode = DataGridViewEditMode.EditOnEnter;
    
    col1.Items.AddRange(new object[] { "Cat", "Dog", "Elephant", "Lion" });
    col2.Items.AddRange(new object[] { "Duck", "Hen", "Crow", "Sparrow" });
    
    this.Controls.Add(dgv);
    
  3. Run the project. Click on the first drop-down, then click on the other dropdown without closing the first one. It will take 2 or 3 clicks (depending upon where you click in the second dropdown) to get the second list opened.

like image 610
dotNET Avatar asked Dec 31 '15 09:12

dotNET


2 Answers

There's nothing wrong with your code. This is the designed behavior for the .NET DataGridView control.

If you click on the button (the pointing down arrow) on the right side of the drop down control rather then on the text area, the drop down list will show right away - 1 click.

If you click on the text area of the drop down control, if will get the focus first and then show the drop down list - 2 clicks.

If at the moment another drop down list is expanded then it would take an extra click to collapse it - that would be 3 clicks.

There are a couple of workarounds to improve this behavior. Take a look at this MSDN discussion. IMHO these kinds of alterations are not worth it but it's certainly up to you to decide what suits best to your project.

Best Regards.

like image 69
Alex X. Avatar answered Nov 12 '22 14:11

Alex X.


This seems to work for me:

Set the EditMode to EditProgramatically

Code the CellMouseClick event:

private void dgv_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    // maybe do a column type check before..!?
    dgv.BeginEdit(false);
    var ec = dgv.EditingControl as DataGridViewComboBoxEditingControl;
    if (ec != null && ec.Width - e.X < SystemInformation.VerticalScrollBarWidth ) 
       ec.DroppedDown = true;
}

Closing a dropped combobox will still eat one moseclick but that's it how it should be, imo.

like image 1
TaW Avatar answered Nov 12 '22 14:11

TaW