Share via


How to: Validate Input with the Windows Forms DataGrid Control

Note

The DataGridView control replaces and adds functionality to the DataGrid control; however, the DataGrid control is retained for both backward compatibility and future use, if you choose. For more information, see Differences Between the Windows Forms DataGridView and DataGrid Controls.

There are two types of input validation available for the Windows Forms DataGrid control. If the user attempts to enter a value that is of an unacceptable data type for the cell, for example a string into an integer, the new invalid value is replaced with the old value. This kind of input validation is done automatically and cannot be customized.

The other type of input validation can be used to reject any unacceptable data, for example a 0 value in a field that must be greater than or equal to 1, or an inappropriate string. This is done in the dataset by writing an event handler for the ColumnChanging or RowChanging event. The example below uses the ColumnChanging event because the unacceptable value is disallowed for the "Product" column in particular. You might use the RowChanging event for checking that the value of an "End Date" column is later than the "Start Date" column in the same row.

To validate user input

  1. Write code to handle the ColumnChanging event for the appropriate table. When inappropriate input is detected, call the SetColumnError method of the DataRow object.

    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. Connect the event handler to the event.

    Place the following code within either the form's Load event or its constructor.

    ' 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));
    

See Also

Reference

DataGrid

ColumnChanging

SetColumnError

Other Resources

DataGrid Control (Windows Forms)