Export (0) Print
Expand All

How to: Validate Data During Row Changes 

Validating data is the process of checking the values being entered into your applications 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.

NoteNote

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:

Validation can also be performed during individual column changes 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, then 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 look similar to 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.

    NoteTip

    Double-click to the left of the table name to create the row-changing event handler. Double-clicking directly on the table name enables editing of 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.

    NoteNote

    The Dataset Designer does not automatically create an event handler for the RowChanging event. You need 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

Community Additions

ADD
Show:
© 2014 Microsoft