Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. For the record, editmode is set to 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.

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.

How can I properly handle this situation?

like image 345
l46kok Avatar asked Jan 21 '13 03:01

l46kok


1 Answers

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:

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.

EDIT: this question seems to have a similar solution

like image 122
splinterz Avatar answered Nov 03 '22 19:11

splinterz