I wish to prevent the user from saving changes made to a DataGridView if it has any validation errors (set using the ErrorText property of a cell using the CellValidating event).
I'm looking for (but cannot see) a method such as myDataGridView.HasErrors()?
Just do it at the same time you are validating the rows. Using the MSDN example arik posted...
private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
dataGridView1.Rows[e.RowIndex].ErrorText = "";
int newInteger;
if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; }
if (!int.TryParse(e.FormattedValue.ToString(),
out newInteger) || newInteger < 0)
{
e.Cancel = true;
dataGridView1.Rows[e.RowIndex].ErrorText = "the value must be a non-negative integer";
//If it's simple, do something like this here.
this.SubmitButton.Enabled = false;
//If not, set a private boolean variable scoped to your class that you can use elsewhere.
this.PassedValidation = false;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With