Freigeben über


How to: Validate Data During Column Changes

Validating data is the process of checking the values being entered into your application's data. Checking these values prior to sending updates to the underlying data store is a good practice that reduces the potential number of round trips between an application and the data store.

Note

The Dataset Designer creates a partial class where validation logic can be added to a dataset. The designer-generated dataset will not delete or change any code in the partial class. For more information, see How to: Extend the Functionality of a Dataset.

You can validate data when the value in a data column changes by responding to the ColumnChanging event. When raised, this event passes an event argument (ProposedValue) that contains the value being proposed for the current column. Based on the contents of e.ProposedValue, you can:

Validation can also be performed during the RowChanging event. For more information, see How to: Validate Data During Row Changes.

To validate data as column values change

  1. Open your dataset in the Dataset Designer. For more information, see How to: Open a Dataset in the Dataset Designer.

  2. Double-click the column you want to validate. This action creates the ColumnChanging event handler of the DataTable.

    Note

    The Dataset Designer does not automatically create an event handler for the C# event. The code needed to handle the event is included below.

  3. Add code to verify that e.ProposedValue contains data that meets the requirements of your application. If the proposed value is unacceptable, then set the column to indicate that it contains an error.

    The following code example sets a column error when the Quantity column is 0 or less. The column-changing event handler should look similar to the following:

    'Visual Basic
    Private Sub Order_DetailsDataTable_ColumnChanging(ByVal sender As System.Object, ByVal e As System.Data.DataColumnChangeEventArgs) _
        Handles Me.ColumnChanging
    
        If (e.Column.ColumnName = Me.QuantityColumn.ColumnName) Then
    
            If CType(e.ProposedValue, Short) <= 0 Then
            e.Row.SetColumnError(e.Column, "Quantity must be greater than 0")
        Else
            e.Row.SetColumnError(e.Column, "")
            End If
        End If
    End Sub
    
    // C#
    // Add this code to the DataTable 
    // partial class.
        public override void EndInit()
        {
            base.EndInit();
            ColumnChanging += SampleColumnChangingEvent;
        }
    
        public void SampleColumnChangingEvent(object sender, System.Data.DataColumnChangeEventArgs e)
        {
            if (e.Column.ColumnName == QuantityColumn.ColumnName)
            {
                if ((short)e.ProposedValue <= 0)
                {
                    e.Row.SetColumnError("Quantity", "Quantity must be greater than 0");
                }
                else
                {
                    e.Row.SetColumnError("Quantity", "");
                }
            }
        }
    

See Also

Tasks

Walkthrough: Displaying Data on a Windows Form

How to: Validate Data in the Windows Forms DataGridView Control

How to: Display Error Icons for Form Validation with the Windows Forms ErrorProvider Component

How to: Connect to Data in a Database

Reference

Data Sources Window

Concepts

TableAdapter Overview

Creating and Editing Typed Datasets

Data Sources Overview

Validating Data