SimonKravis
SimonKravis

Reputation: 659

Cannot customise DataError report in DataGridView on invalid data input

I am using a DataGridView control connected to a database table via a BindingSource control. The table includes a date/time field. When an incorrectly formatted date is entered in the cell corresponding to the date/time field and the row committed, I see a screen captioned DataGridView Default Error handler, with an Exception report and call stack. A message at the bottom says "To replace this default dialog please handle the DataError event". If I add code to this event to do this, the program hangs when invalid data input and commit attempted, with the focus unable to be moved out of the date/time cell with the invalid data. I would like to be able to customise the error message in this case.

Upvotes: 0

Views: 154

Answers (1)

SimonKravis
SimonKravis

Reputation: 659

The prompt to find some code to show in the event revealed the solution. I needed to set e.Cancel to false. Code without this does not enter the event, which was why I did not list it

// Don't throw an exception when we're done.
e.ThrowException = false;

// Display an error message.
string txt = "Error with " +
    dgvPeople.Columns[e.ColumnIndex].HeaderText +
    "\n\n" + e.Exception.Message;
MessageBox.Show(txt, "Error",
    MessageBoxButtons.OK, MessageBoxIcon.Error);

// If this is true, then the user is trapped in this cell.
e.Cancel = false;

Upvotes: 0

Related Questions