I am setting "e.Cancel = true" in _CellValidating event when user inputs invalid value in my DatagridView Cell.
It seems that e.Cancel also prevent user from Closing the form or hitting X button, How can I add exception to that?(Allow user to close the form even e.Cancel is set to true)
I just rigged up a test project based on this scenario, and it seems that the DataGridView
's CellValidating
event is called before the Form
's Closing
event; this means that you have no way of knowing, at the time the cell is validated, whether the user has tried to close the form.
Strictly speaking, the correct sequence of events is for the user to either enter valid data in the cell (or cancel the edit by pressing the escape key) before the form will be allowed to close. However, if you want to allow the form to be closed regardless, you can handle the Closing
event for the form:
protected override void OnClosing(CancelEventArgs e) {
e.Cancel = false;
base.OnClosing(e);
}
This is bad practice, but it will give you the behaviour that you've asked for.