[英]How to force datagridviewcell to end edit when row header is clicked
I am trying to force the DataGridViewCell
to exit out of the edit mode when a user clicks the row header that's in the same row as the cell being edited. 当用户单击与正在编辑的单元格位于同一行的行标题时,我试图强制DataGridViewCell
退出编辑模式。 For the record, editmode is set to EditOnEnter. 对于记录,editmode设置为EditOnEnter。
So I write the following event accordingly: 所以我相应地写了以下事件:
private void dGV_common_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
dGV_common.EndEdit();
}
The above code didn't force the cell to end the edit mode. 上面的代码没有强制单元格结束编辑模式。 While the below code forces the cell to exit from editmode: 以下代码强制单元格从editmode退出:
private void dGV_common_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
dGV_common.EndEdit();
dGV_common.CurrentCell = null;
}
It also deselects the entire row, which is not the desired behavior when a user clicks on the RowHeader
. 它还取消选择整行,这在用户单击RowHeader
时不是所需的行为。
So, my work around has been the following: 所以,我的工作如下:
private void dGV_customer_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
dGV_customer.EndEdit();
dGV_customer.CurrentCell = null;
dGV_customer.Rows[e.RowIndex].Selected = true;
}
Which works well when you select a single row header, but fails when you try to select multiple row headers by holding shift. 当您选择单个行标题时效果很好,但是当您尝试通过保持shift来选择多个行标题时会失败。
How can I properly handle this situation? 我该如何妥善处理这种情况?
i ran into exactly the same problem this week! 本周我遇到了完全相同的问题! it appears this is a pretty well documented bug in the datagridview. 看来这是datagridview中一个记录很好的bug。 i'm unsure if it's been fixed in any later versions. 我不确定它是否已在任何更新版本中修复。 checking for a row header when the grid is clicked and changing the editmode seems to work though: 单击网格时检查行标题并更改editmode似乎工作:
private void dataGridView_MouseClick( object sender, MouseEventArgs e ) {
DataGridView dgv = (DataGridView)sender;
if (dgv.HitTest(e.X, e.Y).Type == DataGridViewHitTestType.RowHeader) {
dgv.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
dgv.EndEdit();
} else {
dgv.EditMode = DataGridViewEditMode.EditOnEnter;
}
}
however this is still an irritating work around if you use many datagridviews throughout your application, so let me know if you discover a better solution. 但是,如果您在整个应用程序中使用许多datagridviews,这仍然是一个令人恼火的工作,所以如果您发现更好的解决方案,请告诉我。
EDIT: this question seems to have a similar solution 编辑: 这个问题似乎有类似的解决方案
I found a easy fix to disable editmode: 我找到了一个简单的解决方法来禁用editmode:
SendKeys.Send("{TAB}");
SendKeys.Send("+{TAB}");
It simply presses tab, and then shift+tab 它只需按Tab键,然后移动+ tab
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.