How to make datagridviewcell finish editing on click of row header

I am trying to force DataGridViewCell

exit from edit mode when the user clicks a row heading that is on the same row as the cell being edited. For recording, editmode is set to EditOnEnter.

So, I write the following event:

private void dGV_common_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    dGV_common.EndEdit();   
} 

      

The above code did not cause the cell to end edit mode. Although below code makes the cell exit 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 the user clicks on RowHeader

.

So my work was as follows:

private void dGV_customer_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    dGV_customer.EndEdit();
    dGV_customer.CurrentCell = null;
    dGV_customer.Rows[e.RowIndex].Selected = true;
}

      

Works well when you select a single row heading, but fails when you try to select multiple row headings while holding shift.

How can I properly handle this situation?

+5


source to share


2 answers


I ran into the same problem this week! seems to be a pretty well documented bug in datagridview. I'm not sure if it has been fixed in any later versions. checking the row header when the grid is clicked and changing the edit mode seems to work:

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 annoying if you use a lot of datagridviews throughout your application, so let me know if you find a better solution.

EDIT: This question seems to have a similar solution

+5


source


I found a simple fix to turn off edit mode:

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

      



He just clicks tab and then Shift + Tab

0


source







All Articles