C# 如何在第一次单击时激活组合框 (Datagridview)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13005112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to activate combobox on first click (Datagridview)
提问by l46kok
In winforms, you need to click the combobox twice to properly activate it - the first time to focus it, the second time to actually get the dropdown list.
在 winforms 中,您需要单击组合框两次才能正确激活它 - 第一次是聚焦它,第二次是实际获取下拉列表。
How do I change this behavior so that it activates on the very first click?
如何更改此行为,使其在第一次单击时激活?
This is for DATAGRIDVIEW combobox.
这是用于 DATAGRIDVIEW 组合框。
采纳答案by Jeff Click
I realize this is an old question, but I figured I would give my solution to anyone out there that may need to be able to do this.
我意识到这是一个老问题,但我想我会将我的解决方案提供给可能需要能够做到这一点的任何人。
While I couldn't find any answers to do exactly this... I did find an answerto a different question that helped me.
虽然我找不到任何答案来做到这一点......我确实找到了一个对我有帮助的不同问题的答案。
This is my solution:
这是我的解决方案:
private void datagridview_CellEnter(object sender, DataGridViewCellEventArgs e)
{
bool validClick = (e.RowIndex != -1 && e.ColumnIndex != -1); //Make sure the clicked row/column is valid.
var datagridview = sender as DataGridView;
// Check to make sure the cell clicked is the cell containing the combobox
if(datagridview.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn && validClick)
{
datagridview.BeginEdit(true);
((ComboBox)datagridview.EditingControl).DroppedDown = true;
}
}
private void datagridview_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
datagridview.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
The above code must be tied into the CellEnter event of the datagridview.
上面的代码必须绑定到datagridview的CellEnter事件中。
I hope this helps!
我希望这有帮助!
edit:Added a column index check to prevent crashing when the entire row is selected.
编辑:添加了列索引检查以防止在选择整行时崩溃。
Thanks, Up All Nightfor the above edit
谢谢,整夜为上述编辑
edit2:Code is now to be tied to the CellEnter rather than the CellClick event.
edit2:代码现在将绑定到 CellEnter 而不是 CellClick 事件。
Thanks, HaraldDutchfor the above edit
感谢HaraldDutch的上述编辑
edit3:Any changes will committed immediately, this will save you from clicking in another cell in order to update the current combobox cell.
edit3:任何更改都将立即提交,这将使您无需单击另一个单元格以更新当前组合框单元格。
回答by Abdul Majid
Set the DropDownStyle property of your combo box to DropDownList...
将组合框的 DropDownStyle 属性设置为 DropDownList...
回答by KreepN
Set the following on your DataGridView:
在您的 DataGridView 上设置以下内容:
EditMode = EditOnEnter
This is probably the easiest solution and has been the workaround for many users here on SO when this question gets asked.
这可能是最简单的解决方案,并且一直是 SO 上许多用户在被问到这个问题时的解决方法。
EDIT :
编辑 :
Per heredo the following:
每这里做到以下几点:
Set the Editmode:
设置编辑模式:
EditMode = EditOnKeystrokeOrF2
Modify the EditingControlShowing event on the datagridview:
修改 datagridview 上的 EditingControlShowing 事件:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox ctl = e.Control as ComboBox;
ctl.Enter -= new EventHandler(ctl_Enter);
ctl.Enter += new EventHandler(ctl_Enter);
}
void ctl_Enter(object sender, EventArgs e)
{
(sender as ComboBox).DroppedDown = true;
}
This will get you your desired results. Let me know if that doesn't do it.
这将得到你想要的结果。如果那不行,请告诉我。
回答by jefferp
If you set the entire grid to EditOnEnter, you can get some pretty funky activity when you are on a text column. Here's my solution, which should be self explanatory. If you did not know the column names, you could just check the cell type on mousemove.
如果您将整个网格设置为 EditOnEnter,那么当您在文本列上时,您可以获得一些非常时髦的活动。这是我的解决方案,这应该是不言自明的。如果您不知道列名,您可以在 mousemove 上检查单元格类型。
Private Sub GridView_CellMouseMove(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles GridView.CellMouseMove
Select Case GridView.Columns(e.ColumnIndex).Name
Case "Ad_Edit", "Size_Caption", "Demo_Code"
GridView.EditMode = DataGridViewEditMode.EditOnEnter
Case Else
GridView.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2
End Select
End Sub
回答by Yudit
I changed only the EditModeproperty of the datagridviewto EditOnEnterand it's working perfectly.
我只更改EditMode了datagridviewto的属性,EditOnEnter它运行良好。
EditMode = EditOnEnter

