问题描述
在有人将此标记为重复之前,请注意这与提出的问题不同 此处,这里和这里.
Before anyone marks this as duplicate, plz note that this is not the same as the questions asked here, here and here.
当您的 DataGridView
中有两个或多个 DataGridViewComboBoxColumn
并且其中一个的下拉列表当前打开时,单击不同列的下拉按钮不会打开那个下拉菜单.相反,您仍然必须单击两次.第一次点击会隐藏已经打开的下拉菜单,第二次点击实际上会打开您点击的下拉菜单.
When you have two or more DataGridViewComboBoxColumn
s 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.
注意EditOnEnter
模式为ON时需要点击两次;否则,您必须单击三下才能完成此操作.我也尝试过 ContentClick 事件,但没有任何收获.
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.
当我的网格中有多个 DataGridViewComboBoxColumn
时,如何使用一键操作?
So how can I use one-click operation in cases when I have more than one DataGridViewComboBoxColumn
in my grid?
以防万一有人想复制它,这是过程:
Just in case anyone wants to reproduce it, here is the process:
- 创建一个新的 WinForms C# 项目.
转到 Form1 的代码并将其粘贴到构造函数中
InitializeComponent
行之后:
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);
运行项目.单击第一个下拉列表,然后单击另一个下拉列表而不关闭第一个.需要点击 2 或 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.
推荐答案
这似乎对我有用:
将 EditMode
设置为 EditProgramatically
对CellMouseClick
事件进行编码:
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;
}
关闭一个掉落的组合框仍然会吃掉一个 moseclick,但这就是它应该的样子,imo.
Closing a dropped combobox will still eat one moseclick but that's it how it should be, imo.
这篇关于DataGridViewComboBoxColumn 不会在第一次单击时打开下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!