2010-08-22 147 views
3

我希望防止用戶保存對DataGridView所做的更改(如果它有任何驗證錯誤)(使用CellValidating事件使用單元格的ErrorText屬性進行設置)。DataGridView有錯誤?

我在找(但不能看到)一種方法,如myDataGridView.HasErrors()

在MSDN

回答

1

有關於如何驗證在DataGridView 看看一個例子...

DataGridView

我希望它幫

+0

對不起,也許我原來的問題還不清楚。我已經驗證了單元格的正確性,並在適當的地方設置了ErrorText。我只想知道是否有任何單元格驗證失敗,因此我可以阻止用戶單擊「保存」按鈕。 – Rezzie 2010-08-22 13:53:22

1

只要做到這一點,在您要驗證的同時行。使用張貼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; 
    } 
} 
+0

雖然這並不能處理多列,是嗎?例如,假設第一個單元驗證失敗 - 提交按鈕被禁用。然後下一個單元格通過驗證 - 提交按鈕將被啓用,即使之前的單元格失敗...? – Rezzie 2010-08-22 19:28:28

+0

爲什麼提交按鈕會重新啓用而沒有明確指示這樣做?上面的代碼並不是您使用的確切代碼。這是爲了說明一個觀點(即當發生故障時,你可以使任何你想要的都無效)。 – Ocelot20 2010-08-23 13:02:34

+0

您發佈的代碼足以在任何單元格驗證失敗時禁用按鈕,但是如何(以及在​​哪裏)重新啓用它? – Rezzie 2010-08-23 14:18:12

0

我想,這個問題已經解決了,現在,但我會加入我的解決方案,以幫助其他面臨關於與多個字段驗證應對同一個問題:

我創造了一些pubilc變量來存儲whetehr輸入是否有效:

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