Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView HasErrors?

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()?

like image 464
Rezzie Avatar asked Feb 12 '26 23:02

Rezzie


1 Answers

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;
    }
}
like image 99
Ocelot20 Avatar answered Feb 15 '26 11:02

Ocelot20



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!