Data Validation Sample
This sample demonstrates the Dataset Designer and uses the PropertyChanged events to validate user input.
To get samples and instructions for installing them
Do one or more of the following:
On the Help menu, click Samples.
The Readme displays information about samples.
Visit the Visual Studio 2008 Samples Web site. The most recent versions of samples are available there.
Locate samples on the computer on which Visual Studio is installed. By default, samples and a Readme file are installed in drive:\Program Files\Microsoft Visual Studio 9.0\Samples\lcid. For Express editions of Visual Studio, all samples are located online.
For more information, see Visual Studio Samples.
This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.
To run this sample
This sample requires the Northwind sample database. For more information, see How to: Install and Troubleshoot Database Components for Samples.
This sample demonstrates the data validation features available in the Visual Studio designers. Two arbitrary and fictitious rules are used to validate the data. Code is added using the Dataset Designer. Double-clicking a column name creates the event for the column change. Double-clicking the field list creates the event for the row change. In those event handlers, code is added to validate the proposed values:
Private Sub EmployeeDataTable_BirthDateChanging( _ ByVal sender As EmployeeDataTable, _ ByVal e As BirthDateChangeEventArg) Handles Me.BirthDateChanging Dim minimumAgeDate As New DateTime(DateTime.Now().Year - 18, _ DateTime.Now().Month, DateTime.Now().Day) If e.ProposedValue > minimumAgeDate Then e.Row.SetColumnError(e.BirthDateColumn, _ "Employees must be at least 18 years of age.") Else e.Row.SetColumnError(e.BirthDateColumn, "") End If End Sub Private Sub EmployeesDataTable_EmployeesRowChanging( _ ByVal sender As System.Object, ByVal e As _ EmployeesRowChangeEvent) Handles Me.EmployeesRowChanging If (e.Row.BirthDate = e.Row.HireDate) Then e.Row.RowError = e.Row.RowError & _ "Birth date and hire date are the same." Else e.Row.ClearErrors() End If End Sub
When row and column errors are defined, the DataGridView control displays a warning icon. For row errors, the icon is displayed to the left of the row. For column errors, the icon is displayed in the cell.