user271077
user271077

Reputation: 1006

Check if a dataGridView has errorText set on any of it's cells

How can I know if a datagridview has errorText on any of it's cells. I have a Save button which I want to enable only if all cell values are valid meaning that none of the cells have errorText set

Upvotes: 7

Views: 5426

Answers (2)

Daniel A.
Daniel A.

Reputation: 33

Good solution in the accepted answer. I cannot comment, so here is my comment in a new answer:

Don't forget the ErrorText of the row and add this below the first "foreach":

if (row.ErrorText.Length > 0)
{
    hasErrorText = true;
    break;
}

Upvotes: 0

Jojo Sardez
Jojo Sardez

Reputation: 8568

Use this method on your code:

private bool HasErrorText()
    {
        bool hasErrorText = false;
        //replace this.dataGridView1 with the name of your datagridview control
        foreach (DataGridViewRow row in this.dataGridView1.Rows)
        {
            foreach (DataGridViewCell cell in row.Cells)
            {
                if (cell.ErrorText.Length > 0)
                {
                    hasErrorText = true;
                    break;
                }
            }
            if (hasErrorText)
                break;
        }

        return hasErrorText;
    }

Upvotes: 13

Related Questions