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.

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. For more information, see How to: Extend the Functionality of a Dataset1.

You can validate data when the value in a data column changes by responding to the ColumnChanging2 event. When raised, this event passes an event argument (ProposedValue3) 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 RowChanging7 event. For more information, see How to: Validate Data During Row Changes8.

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 Designer9.

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

    NoteNote

    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", "");
                }
            }
        }
    
Links Table
1http://msdn.microsoft.com/en-us/library/ms171896.aspx
2http://msdn.microsoft.com/en-us/library/system.data.datatable.columnchanging.aspx
3http://msdn.microsoft.com/en-us/library/system.data.datacolumnchangeeventargs.proposedvalue.aspx
4http://msdn.microsoft.com/en-us/library/system.data.datarow.setcolumnerror.aspx
5http://msdn.microsoft.com/en-us/library/system.windows.forms.errorprovider.aspx
6http://msdn.microsoft.com/en-us/library/95ysxkwy.aspx
7http://msdn.microsoft.com/en-us/library/system.data.datatable.rowchanging.aspx
8http://msdn.microsoft.com/en-us/library/1120xds5.aspx
9http://msdn.microsoft.com/en-us/library/7973zb70.aspx
10http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx
11http://msdn.microsoft.com/en-us/library/wwh8ka92.aspx
12http://msdn.microsoft.com/en-us/library/7ehy30d4.aspx
13http://msdn.microsoft.com/en-us/library/f6xht7x2.aspx
14http://msdn.microsoft.com/en-us/library/fxk9yw1t.aspx
15http://msdn.microsoft.com/en-us/library/6ckyxa83.aspx
16http://msdn.microsoft.com/en-us/library/bz9tthwx.aspx
17http://msdn.microsoft.com/en-us/library/314t4see.aspx
18http://msdn.microsoft.com/en-us/library/8s2t4x15.aspx
19http://msdn.microsoft.com/en-us/library/t3b36awf.aspx
Community Content Add
Annotations FAQ
Not for 'Website' projects
This does not work in a web-site project (no project file), as opposed to web-application projects