I have a DataGridView which one of its columns is a DataGridViewCheckBoxColumn. When I check a cell of this column, I validate if two other cells in the same row (but in different columns) are 0. If both are 0, then I want to cancel the new value entered for the checkbox column (consume the event and ignore the change on it). That is: if you check it and other two columns are both 0 (in the same row), then I want to keep uncheck the checkbox cell.
I do this by handling the CellValidating event. Inside it I check if these two cells (in the same row)are both 0 and if so and user has checked the checkbox cell (also in the same row) then I do e.Cancel = true, in order to keep unchecked the checkbox cell and ignore the check done by user.
But it is not working, I have seen that e.RowIndex in CellValidating event does not correspond to the row index on which I have just clicked but e.ColumnIndex is correct…
In summary, what I want is that when user checks the checkbox column of a row, if the other two columns values are both set to 0 for the same row, I want to keep the checkbox column unchanged (it remains with its old value, for example if it was unchecked and user checks it, it will remain unchecked). Otherwise if these two columns are not both set to 0, checkbox column will take it into account, for example, if it was unchecked and user checks it, it will change to checked.
Any ideas?
I would use a different event than
CellValidating. I would useCellBeginEditI believe
CellValidatinggets called on the whatever cell you were previously on and that is why the row index doesn’t make sense.