Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...