How to Validate DataGridView Input This example explains how to use DR.net to set validation criteria for a DataGridView control on a Visual Studio.NET form. Why You Should Use This To add extensible and customizable DataGridView validation that is easy to use and maintain. DR.net stores validation information in a Microsoft Access table, allowing you to implement n-tier best practices by performing validation in the presentation layer. DR.net validation is: Extensible: Add field validation to new fields as you create them. Unlike validation routines that are embedded in the code base, you are not locked into the original validation design. Add new validation checking as you go. Customizable: You decide what fields need validation and how that validation works. If you have a specific format for part numbers, unlocking codes, or any other type of specialty data, you can easilycreate detailed and accurate validation checks. Easy to use and maintain: You don t need to add any code to add validation. Simply add an entry to the validation table and leverage the validation routines in DR.net s reusable code. Modular: DR.net lets you separate input validation from database field validation. Control and evaluate user input, even when you don t need to store the information in a database. How to Validate DataGridView Input Page 1
Validation Sequence Dr.net performs the following tasks: Establishes a connection to the Microsoft Access Payments table, which is part of the parking-test.mdb database. DR.net handles the mechanics of the connection; you supply one argument the database s predefined nickname. Populates a DataGridView control with data from the Payments table. Checks input against predefined validation criteria stored in the _validations table within the parking-test.mdb database. Displays a customizable warning alert if your input does not conform to the validation criteria. In this example, the validation rules specify that the check_number field must not be blank. How to Validate DataGridView Input Page 2
Methods This example uses the following DR.net methods: get_connection_name(database_nickname_string) New DataGridViewer show_grid(datagridview_name, SQL_command) Example Code Imports SLM.connections.connections_admin Public Class Review_Payments Private Sub Review_Payments_Load(ByVal sender As _ System.Object, ByVal e As System.EventArgs)_ Handles MyBase.Load get_connection_name("parking") Dim dgv_payments As New DataGridView dgv_payments.dock = DockStyle.Fill Me.Controls.Add(dgv_Payments) Dim g As New GridViewer_Sandbox g.show_grid(dgv_payments, "select * from payments", "payments") End Sub End Class How it Works The get_connection_name method creates a connection to the parking_test.mdb database called parking. For an explanation of how it works, see How to Populate a Form. A DataGridView called dgv_payments is added to the Review_Payments form and the DR.net GridViewer_Sandbox object called g is created. The DR.net method show_grid populates the grid with the contents of the Payments table using and SQL SELECT command. DR.net methods in the GridViewer_Sandbox class execute the validation routines. When you enter data into a DataGridView, these routines check for rules in the _validations table. If rules exist, DR.net applies the ones associated with the form and DataGridView specified in the form code. In this example, the table contains the following validation entry. How to Validate DataGridView Input Page 3
The form column contains the name of the form containing the DataGridView to be validated. The form_object column contains the name of the DataGridView to be validated. The field column contains the name of the DataGridView field to be validated. The validation column contains the validation rule. The comment column contains the message text assigned to the validation failure alert box. Setup This example requires: A Windows Form called Review_Payments An entry in the _validations table of the parking_test.mdb database (as shown in the previous illustration) Options You can set the following validation rules. Validation String Accepted Values anything Any value accepted non_zero Numeric value > 0 non _blank Cannot be blank datetime time numeric Valid date or time Valid time Numeric; blanks accepted How to Validate DataGridView Input Page 4
In addition to single field validation, you can also validate fields in multiple DataGridViews or on multiple forms. You can also specify rules that compare input fields (Time_in<time_out, for example, where the Time_in entry must precede the time_out entry). How to Validate DataGridView Input Page 5