3

I wish to prevent the user from saving changes made to a DataGridView if it has any validation errors (set using the ErrorText property of a cell using the CellValidating event).

I'm looking for (but cannot see) a method such as myDataGridView.HasErrors()?

Rezzie
  • 4,763
  • 6
  • 26
  • 33

3 Answers3

1

in msdn there is an example on how to validate the DataGridView have a look...

DataGridView

i hope it helped

arik
  • 338
  • 1
  • 16
  • 1
    Sorry, maybe I wasn't clear in the original question. I am already validating the cells fine, and setting the ErrorText wherever appropriate. I just want to know if any cells have failed validation so that I can prevent the user from clicking a "Save" button. – Rezzie Aug 22 '10 at 13:53
1

Just do it at the same time you are validating the rows. Using the MSDN example arik posted...

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;
    }
}
Ocelot20
  • 10,510
  • 11
  • 55
  • 96
  • This doesn't cope with multiple columns though, does it? For example, say the first cell fails validation - the submit button is disabled. Then the next cell passes validation - the submit button will be enabled, even though a previous cell failed...? – Rezzie Aug 22 '10 at 19:28
  • Why would the submit button re-enable itself without being explicitly directed to do so? The code above isn't meant to be the exact code you use. It was meant to illustrate a point (that you can invalidate whatever you want as failure occurs). – Ocelot20 Aug 23 '10 at 13:02
  • The code you posted is sufficient for disabling the button whenever any cell fails validation, but how (& where) would I re-enable it? – Rezzie Aug 23 '10 at 14:18
  • Without seeing your code and what you're trying to accomplish, my only answer can be...wherever it makes sense to do so. I mean, if there was such a .HasErrors method, where would you call it from? – Ocelot20 Aug 23 '10 at 18:46
  • In the CellValidation event? `this.SubmitButton.Enabled = !dataGridView1.HasErrors()` – Rezzie Aug 23 '10 at 19:35
  • 1
    The DataGridView does not implement an HasErrors method, much to the OP's dismay. – Kharlos Dominguez Sep 02 '10 at 12:36
0

I imagine that the issue has been solved now, but I'll add my solution to help others facing the same issue regarding coping with multiple-field validation:

I created some pubilc variables to store whetehr the input is valid or not:

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

Then, in the DataGrid's _CellValueChanged method:

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;
       }
    }
Alex
  • 1,082
  • 3
  • 12
  • 23