Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Supressed Click event when an error is encountered on the form
 

Supressed Click event when an error is encountered on the form

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.
lidiya  Monday, June 04, 2007 2:43 PM
Use the CellValidating event instead of CellEndEdit for checking for invalid data being entered into the cell. If you set c.cancel=true in the cell validating event you will prevent the user from being able to move from the cell. This will keep the form from closing.
Ken Tucker  Monday, June 04, 2007 6:58 PM
Handle the form's Closing event and set it's Cancel property to true.
timvw  Monday, June 04, 2007 6:55 PM
Use the CellValidating event instead of CellEndEdit for checking for invalid data being entered into the cell. If you set c.cancel=true in the cell validating event you will prevent the user from being able to move from the cell. This will keep the form from closing.
Ken Tucker  Monday, June 04, 2007 6:58 PM

You can use google to search for other answers

Custom Search

More Threads

• get changes using BindlingList<T>
• Error Provider creating error but i don't know why
• DataGridView
• Default values in BindingSource
• Question about DataGrid with Business Object as DataSource
• Need practical examples of row validation and error handling on DataGridView
• Creating a typed dataset of MS Project 2003 XML files
• Get Current Clicked Column Header?
• Navigator's Broke!
• SortGlyphDirection ignored for .DataSource = DataTabe or DataView