DataGridView有错误吗?

3

如果 DataGridView 中存在任何验证错误(使用 CellValidating 事件使用单元格的 ErrorText 属性设置),我希望防止用户保存所做的更改。

我正在寻找(但无法看到)类似于 myDataGridView.HasErrors() 的方法?

3个回答

1

在验证行的同时,立即执行它。使用arik发布的MSDN示例...

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;
    }
}

这个方法无法处理多列,对吧?例如,假设第一个单元格未通过验证 - 提交按钮将被禁用。然后下一个单元格通过验证 - 即使之前的单元格未通过验证,提交按钮也将启用...? - Rezzie
为什么提交按钮会在没有显式指示的情况下重新启用?上面的代码并不是你要使用的确切代码。它旨在说明一个观点(即在发生故障时,您可以使任何内容无效)。 - Ocelot20
您发布的代码足以在任何单元格未通过验证时禁用按钮,但是我应该在哪里重新启用它呢? - Rezzie
没有看到你的代码和你想要实现什么,我的唯一答案是...在任何有意义的地方。我的意思是,如果有一个.HasErrors方法,你会从哪里调用它呢? - Ocelot20
在CellValidation事件中?this.SubmitButton.Enabled = !dataGridView1.HasErrors() - Rezzie
1
DataGridView没有实现HasErrors方法,这让OP感到非常失望。 - Kharlos Dominguez

1

在 MSDN 中有一个关于如何验证 DataGridView 的示例,请看一下...

DataGridView

希望对您有所帮助


1
抱歉,可能我在最初的问题表述上不够清晰。我已经很好地验证了单元格,并在适当的位置设置了ErrorText。我只是想知道是否有任何单元格未通过验证,以便我可以防止用户单击“保存”按钮。 - Rezzie

0

我想现在这个问题已经解决了,但是我会添加我的解决方案来帮助其他人解决同样的问题,即如何处理多字段验证:

我创建了一些公共变量来存储输入是否有效:

public bool validation1_valid = true;
public bool validation2_valid = true;

然后,在 DataGrid 的 _CellValueChanged 方法中:
private void gridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        string cellHeader = gridView.Columns[e.ColumnIndex].HeaderText.ToString();

       //if cell is one that needs validating
       if (cellHeader == "Something" || cellHeader == "Something Else")
       {
           //if it isn't null/empty
           if (gridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null
               && gridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() != "")
           {
               //check if it's valid (here using REGEX to check if it's a number)                  
               if (System.Text.RegularExpressions.Regex.IsMatch(gridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(), @"^(?:^\d+$|)$"))
               {
                   //set the relevant boolean if the value to true if the cell it passes validation
                   switch (cellHeader)
                   {
                       case "Something":
                           validation1_valid= true;
                           break;
                       case "Something Else":
                           validation2_valid= true;
                           break;
                   }

                   //Add an error text
                   gridView.Columns[e.ColumnIndex].DefaultCellStyle.ForeColor = Color.Black;
                   gridView.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = String.Empty;
                   gridView.Rows[e.RowIndex].ErrorText = String.Empty;

                   //DON'T disable the button here - it will get changed every time the validation is called
               }
               else //doesn't pass validation
               {
                   //set boolean to false
                   switch (cellHeader)
                   {
                       case "Something":
                           validation1_valid = false;
                           break;
                       case "Something Else":
                           validation2_valid = false;
                           break;
                   }

                   gridView.Columns[e.ColumnIndex].DefaultCellStyle.ForeColor = Color.Red;
                   gridView.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Value must be numeric.";
                }
           }
           else //null or empty - I'm allowing these, so set error to "" and booleans to true
           {
               switch (cellHeader)
                   {
                       case "Something":
                           validation1_valid = true;
                           break;
                       case "Something Else":
                           validation2_valid = true;
                           break;
                   }
               gridView.Columns[e.ColumnIndex].DefaultCellStyle.ForeColor = Color.Black;
               gridView.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = String.Empty;
               gridView.Rows[e.RowIndex].ErrorText = String.Empty;
           }
       }

       //if there is an invalid field somewhere
       if (validation1_valid == false || validation2_valid == false)
       {
           //set the button to false, and add an error to the row
           btnSave.Enabled = false;
           gridView.Rows[e.RowIndex].ErrorText = "There are validation errors.";
       }
       //else if they're all vaild
       else if (validation1_valid == true && validation2_valid == true)
       {
           //set the button to true, and remove the error from the row
           btnSave.Enabled = true;
           gridView.Rows[e.RowIndex].ErrorText = String.Empty;
       }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接