DataGridView RowValidating fires after selecting another control. Any workaround?
Using the RowValidating events works fine.
But if i click somewhere in the datagridview without changing the row the event is always fireed after selecting another contorl.
Sample-1 (correct):
Selecting a Row -> Selecting a Textbox -> Selecting antoher Row |->RowValidating raised
Sample-2 (incorrect):
Selecting a Row -> Clicking the Columnheader -> Selecting a Textbox -> Selecting antoher Row |->RowValidating raised |->RowValidating raised
Sample-3 (incorrect):
Selecting a Row -> Clicking empty Area -> Selecting a Textbox -> Selecting antoher Row |->RowValidating raised |->RowValidating raised
Is there any way to only get RowInvalidating before a row is realy changeing?
I already tried to override SetSelectedRowCore
and was able the cancel the selection change, but the datagridview sends EndEdit()
to the Binding before SetSele开发者_运维技巧ctedRowCore
is fired.
I've found a little dirty workaround:
private void dataGridView_RowValidating(object sender, DataGridViewCellCancelEventArgs e) {
Point pos = dataGridView.PointToClient(Control.MousePosition);
DataGridView.HitTestInfo hi = dataGridView.HitTest(pos.X, pos.Y);
if (hi.Type == DataGridViewHitTestType.Cell && hi.RowIndex != e.RowIndex) {
e.Cancel = !AllowChangeCurrent(); //Check if changing selection is allowed
}
}
First I get the Current mouse position in DataGridView.
Afterward I check if a cell if clicked. (False -> Finished)
At least I check if the clicked cell/row is not the current (False -> Finished | True -> Validate)
精彩评论