Udostępnij za pośrednictwem


How to: Validate Data During Row Changes

Validating data is the process of checking the values being entered into your applications data. It is a good practice to check these values before sending updates to the underlying data store. This is because it 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.

You can validate data when the values in a data row change by responding to the RowChanging event. When raised, this event passes an event argument (e.Row) that contains the values being proposed for each column in the current row. Based on the contents of each column in e.Row, you can:

You can also perform validation during individual column changes by using the ColumnChanging event. For more information, see How to: Validate Data During Column Changes.

Validating Data in the RowChanging Event

You can write code to verify that each column you want to validate contains data that meets the requirements of your application. If the proposed value is unacceptable, set the column to indicate that it contains an error. The following examples set a column error when the Quantity column is 0 or less. The row-changing event handlers should resemble the following examples.

To validate data when a row changes (Visual Basic)

  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 title bar of the table you want to validate. This action automatically creates the RowChanging event handler of the DataTable in the dataset's partial-class file.

    Tip

    Double-click to the left of the table name to create the row-changing event handler. If you double-click the table name, you can edit the table name.

    Private Sub Order_DetailsDataTable_Order_DetailsRowChanging(ByVal sender As System.Object, _
        ByVal e As Order_DetailsRowChangeEvent) Handles Me.Order_DetailsRowChanging
    
        If CType(e.Row.Quantity, Short) <= 0 Then
            e.Row.SetColumnError("Quantity", "Quantity must be greater than 0")
        Else
            e.Row.SetColumnError("Quantity", "")
        End If 
    End Sub
    

To validate data when a row changes (C#)

  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 title bar of the table you want to validate. This action creates a partial-class file for the DataTable.

    Note

    The Dataset Designer does not automatically create an event handler for the RowChanging event. You have to create a method to handle the RowChanging event and execute code to hook up the event in the table's initialization method.

  3. Copy the following code into the partial class:

        public override void EndInit()
        {
            base.EndInit();
            Order_DetailsRowChanging += TestRowChangeEvent;
        }
    
        public void TestRowChangeEvent(object sender, Order_DetailsRowChangeEvent e)
        {
            if ((short)e.Row.Quantity <= 0)
            {
                e.Row.SetColumnError("Quantity", "Quantity must be greater than 0");
            }
            else
            {
                e.Row.SetColumnError("Quantity", "");
            }
        }
    

See Also

Tasks

How to: Connect to Data in a Database

How to: Validate Data During Column Changes

Walkthrough: Displaying Data on a Form in a Windows Application

Concepts

What's New in Data

TableAdapter Overview

Dataset Designer

Data Sources Overview

Reference

Data Sources Window

Events (C# Programming Guide)

Other Resources

Data Walkthroughs

Validating Data