Reputation: 1006
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
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
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