共用方式為


HOW TO:使用 Windows Form DataGrid 控制項驗證輸入

注意事項注意事項

DataGridView 控制項會取代 DataGrid 控制項並加入其他功能,不過您也可以選擇保留 DataGrid 控制項,以提供回溯相容性及未來使用。 如需詳細資訊,請參閱 Windows Form DataGridView 和 DataGrid 控制項之間的差異

對 Windows Form DataGrid 控制項來說,可用的輸入驗證有兩種。 如果使用者嘗試輸入的值是屬於儲存格無法接受的資料型別,例如要整數卻輸入字串,這個新的無效值就會被舊值取代。 這種輸入驗證是自動執行且無法自訂的。

另一種輸入驗證可用來拒絕任何無法接受的資料,例如在必須大於或等於 1 的欄位中輸入的零值或是不適當的字串。 您可以在資料集中撰寫 ColumnChangingRowChanging 事件的事件處理常式來進行驗證。 以下範例將使用 ColumnChanging 事件,因為 "Product" 資料行尤其不允許無法接受的值。 您可使用 RowChanging 事件來檢查同一資料列中的 "End Date" 資料行的值是否晚於 "Start Date" 資料行。

若要驗證使用者輸入

  1. 寫入程式碼來為適當資料表處理 ColumnChanging 事件。 當偵測到不適當的輸入時,呼叫 DataRow 物件的 SetColumnError 方法。

    Private Sub Customers_ColumnChanging(ByVal sender As Object, _
    ByVal e As System.Data.DataColumnChangeEventArgs)
       ' Only check for errors in the Product column
       If (e.Column.ColumnName.Equals("Product")) Then
          ' Do not allow "Automobile" as a product.
          If CType(e.ProposedValue, String) = "Automobile" Then
             Dim badValue As Object = e.ProposedValue
             e.ProposedValue = "Bad Data"
             e.Row.RowError = "The Product column contians an error"
             e.Row.SetColumnError(e.Column, "Product cannot be " & _
             CType(badValue, String))
          End If
       End If
    End Sub
    
    //Handle column changing events on the Customers table
    private void Customers_ColumnChanging(object sender, System.Data.DataColumnChangeEventArgs e) {
    
       //Only check for errors in the Product column
       if (e.Column.ColumnName.Equals("Product")) {
    
          //Do not allow "Automobile" as a product
          if (e.ProposedValue.Equals("Automobile")) {
             object badValue = e.ProposedValue;
             e.ProposedValue = "Bad Data";
             e.Row.RowError = "The Product column contains an error";
             e.Row.SetColumnError(e.Column, "Product cannot be " + badValue);
          }
       }
    }
    
    //Handle column changing events on the Customers table
    private void Customers_ColumnChanging(System.Object sender, System.Data.DataColumnChangeEventArgs e) 
    {
       //Only check for errors in the Product column
       if ( e.get_Column().get_ColumnName().Equals("Product") ) 
       {
          //Do not allow "Automobile" as a product
          if ( e.get_ProposedValue().Equals("Automobile") ) 
          {
             System.Object badValue = e.get_ProposedValue();
             e.set_ProposedValue("Bad Data");
             e.get_Row().set_RowError("The Product column contains an error");
             e.get_Row().SetColumnError(e.get_Column(), "Product cannot be " + badValue);
          }
       }
    }
    
  2. 將事件處理常式連接到事件。

    將以下程式碼置於表單的 Load 事件或其建構函式中。

    ' Assumes the grid is bound to a dataset called customersDataSet1
    ' with a table called Customers.
    ' Put this code in the form's Load event or its constructor.
    AddHandler customersDataSet1.Tables("Customers").ColumnChanging, AddressOf Customers_ColumnChanging
    
    // Assumes the grid is bound to a dataset called customersDataSet1
    // with a table called Customers.
    // Put this code in the form's Load event or its constructor.
    customersDataSet1.Tables["Customers"].ColumnChanging += new DataColumnChangeEventHandler(this.Customers_ColumnChanging);
    
    // Assumes the grid is bound to a dataset called customersDataSet1
    // with a table called Customers
    // Put this code in the form's Load event or its constructor.
    customersDataSet1.get_Tables().get_Item("Customers").add_ColumnChanging(
       new DataColumnChangeEventHandler(this.Customers_ColumnChanging));
    

請參閱

參考

DataGrid

ColumnChanging

SetColumnError

其他資源

DataGrid 控制項 (Windows Form)