检查dataGridView是否在其任何单元格上设置了errorText

use*_*077 5 c# datagridview winforms

如何知道datagridview是否在其任何单元格上都有errorText.我有一个Save按钮,只有当所有单元格值都有效意味着没有任何单元格设置了errorText时,我才想启用它

Joj*_*dez 11

在您的代码上使用此方法:

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;
    }
Run Code Online (Sandbox Code Playgroud)