|
I have a form with a DataGridView where one of the columns is editable. The column accepts only integers. I handle the DataGridView.DataError event in case the user enters a non-integer. Code looks like this:
void myDGV_DataError(object sender, DataGridViewDataErrorEventArgs e) { myDGV.CancelEdit(); e.ThrowException = false;
errorProvider1.SetError(myControl, "User-Friendly Error Text Here"); }
For my purposes, only integers between 1 -> 9999 are valid, so in DataGridView.CellEndEdit I make sure the entered value fits. I save the last good value, so that I can revert to it if the user enters an invalid one. This seems to work great. There is only one issue. If the user modifies the value in the grid to a '-1' and clicks on 'OK' button to save the changes, the value is reverted back to its original value, saved, and form gets closed. The problem is that the user never gets to see that there was a problem with the input, because the form is closed. However if the user enters 'D' into the grid and clicks on 'OK' button, the form stays open and Click event is never executed.
I'm assuming CLR is responsible for this behavior. I would like to keep the form open in both scenarios and would like to find out if there is a way to tap into whatever CLR is using to cancel the Click event if an error occurs.
I know I can check if there were any errors on the form in my OK_Click event handler, but I am hoping there is a better solution. Thanks in advance.
|