C# 单击行标题时如何强制 datagridviewcell 结束编辑

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14431936/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:47:08  来源:igfitidea点击:

How to force datagridviewcell to end edit when row header is clicked

c#winformsdatagridview

提问by l46kok

I am trying to force the DataGridViewCellto exit out of the edit mode when a user clicks the row header that's in the same row as the cell being edited. For the record, editmode is set to EditOnEnter.

DataGridViewCell当用户单击与正在编辑的单元格位于同一行的行标题时,我试图强制退出编辑模式。对于记录,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:

上面的代码没有强制单元格结束编辑模式。虽然下面的代码强制单元格退出编辑模式:

    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?

我该如何正确处理这种情况?

采纳答案by splinterz

i ran into exactly the same problem this week! it appears this is a pretty well documented bug in the datagridview. 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:

这周我遇到了完全相同的问题!看来这是datagridview 中一个很好记录的错误。我不确定它是否已在任何更高版本中修复。单击网格时检查行标题并更改编辑模式似乎有效:

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

编辑:这个问题似乎有一个类似的解决方案

回答by Solarity

I found a easy fix to disable editmode:

我找到了一个简单的方法来禁用编辑模式:

SendKeys.Send("{TAB}");
SendKeys.Send("+{TAB}");

It simply presses tab, and then shift+tab

它只是按下tab,然后shift+tab