DataGridView HasErrors?

I want the user not to save the changes made to the DataGridView if it has any validation errors (set using the ErrorText property of the cell using the CellValidating event).

I'm looking for (but can't see) a method, for example myDataGridView.HasErrors()?

+3
source share
3 answers

in msdn there is an example of how to check DataGridView take a look ...

DataGridView

I hope this helps

+1
source

Just do it while checking the lines. Using the MSDN example published by arik ...

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 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;
    }
}
+1
source

, , , , , :

pubilc , :

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

Then in the DataGrid method _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 valid (here using REGEX to check if it 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;
       }
    }
0
source

Source: https://habr.com/ru/post/1760987/


All Articles