Validating Type = Get Type(Integer) dgv Masked Edit. Data Property Name = "Units In Stock" ' some more tweaking dgv Masked Edit. Automatic ' insert the new column at the same location Data Grid View1. Insert(index, dgv Masked Edit) Now the user will only be able to type numbers into this column.This technique provides an enhancement to the Data Grid View control that further affirms the notion that you should always attempt to prevent user input errors rather than just detect them.' ' Check to see if the user input meets all of the business rules before ' we move on to another row. Data Grid View Cell Cancel Event Args) Handles _ Data Grid View1. Value The error checking discussed in the previous section may not detect "data integrity errors" (those errors where the input violates a database foreign key, unique, or primary key constraint).' Private Sub Data Grid View1_Row Validating(By Val sender As Object, By Val e As _ System. Row Validating ' Assume that text input is complete when you attempt to navigate ' away from the row Data Grid View1. In this case we're just checking to ' see if the Unit Price is a positive number If Data Grid View1. Luckily, the Data Grid View Combo Box Column control was designed to help solve this problem.You can use a Data Grid View Combo Box Column to act a as "look up table" to show a user friendly version of the field, rather than just a numeric equivalent.Not only does this look better, it also prevents the user from selecting a value that would violate the foreign key constraint (meaning, you can't select a numeric value that doesn't exist in the related table) Take a look at the following example that replaces a numeric "Category ID" value with the associated user-friendly "Category" string from another table (called "Categories"). Columns("Category ID")) ' remove the existing column Data Grid View1. Remove At(index) ' create a new combobox column that we use as a "lookup" Dim dgvc As New Data Grid View Combo Box Column dgvc.If I erase my "Row Validating" and try again, the problem is solved.
When the Row Validating is fired, the values of my new row to be added are checked, if one of them is not okay, the message is shown to remind the user to input the correct values.This custom Data Grid View column control is contained in 3 classes...A Column control that inherits from Data Grid View Column, a Cell control that inherits from Data Grid View Text Box Cell, and an Editing Control that implements the IData Grid View Editing Control interface The Masked Edit Column class is fairly straight forward; it only overrides one property (to ensure that the correct cell type it used).You can bind a Data Table to the control so the user can easily add/delete/modify data in a database. Products) End Sub By default, the automatically generated columns may need some tweaking.Let's say that you have a Microsoft Access database that you plan to use in your program. For example, all columns are aligned to the left and have have no formatting.