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.