7

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.

Community
  • 1
  • 1
dotNET
  • 33,414
  • 24
  • 162
  • 251
  • Can you give us some code ? – Julien698 Dec 31 '15 at 09:34
  • 1
    @Julien698: It doesn't require any special code. Add a `DataGridView` to your form. Add two `DataGridViewComboBoxColumns` to it and put some items into them. Set `EditMode` to `EditOnEnter` and press F5. – dotNET Dec 31 '15 at 09:53
  • 1
    @Julien698: I have added code just in case you'd like to produce the effect. – dotNET Jan 04 '16 at 17:58

2 Answers2

2

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.

TaW
  • 53,122
  • 8
  • 69
  • 111
1

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.

Alex X.
  • 453
  • 5
  • 11
  • Thanks for the input Alex. I checked out the MSDN discussion and that gave me a new line of action to dig; namely releasing the mouse capture manually. The basic problem as I discovered is that the dropdown list holds the mouse capture and at least one mouse click is required outside the list boundary to release that capture. After an hour of experimentation I finally gave it up. The problem is harder to solve than I initially thought it was. – dotNET Jan 05 '16 at 05:09
  • 1
    In the end if having to click on the grid twice to expand a drop down is really an issue, you could consider purchasing some 3-rd party controls by Telerik, Infragistics, SyncFusion, etc... or even something under the MIT license like SourceGrid... Cheers. – Alex X. Jan 05 '16 at 13:32