The example below shows how you could replace an existing automatically-generated "Units In Stock" column with a new custom column that supports the Masked Edit feature. Columns("Units In Stock")) ' remove the existing column Data Grid View1. Remove At(index) ' create a new custom column Dim dgv Masked Edit As New Data Grid View Masked Edit Column dgv Masked Edit.Dim index As Integer ' find the location of the column index = Data Grid View1. Mask = "####" ' this mask will allow only numbers (max of 4) dgv Masked Edit.' ' 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).
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.Cell Template = value End Set End Property ' ' New properties required by the Masked Text Box control ' Public Property Mask() As String Get Return p Mask End Get Set(By Val value As String) p Mask = value End Set End Property Public Property Prompt Char() As Char Get Return p Prompt Char End Get Set(By Val value As Char) p Prompt Char = value End Set End Property Public Property Validating Type() As Type Get Return p Validating Type End Get Set(By Val value As Type) p Validating Type = value End Set End Property End Class The next step is the Masked Edit Cell class. Most of the code required to implement the methods and properties is fairly straight forward. Editing Control Wants Input Key Return True End Function Public Sub Prepare Editing Control For Edit(By Val select All As Boolean) Implements _ IData Grid View Editing Control.It overrides the Initialize Editing Control method to allow the Mask, Prompt Char, and Validating Type properties from the column class to be passed onto the Cell class. Class Masked Edit Editing Control Inherits Masked Text Box Implements IData Grid View Editing Control Private data Grid View Control As Data Grid View Private value Is Changed As Boolean = False Private row Index Num As Integer Public Sub New() End Sub Public Property Editing Control Formatted Value() As Object Implements _ IData Grid View Editing Control. Prepare Editing Control For Edit ' No preparation needs to be done.Value Member = "Category ID" ' the numeric value to look up in the "child" table dgvc.
Display Member = "Category Name" ' the column value you want displayed dgvc. Get Data() ' the child table ' some more tweaking dgvc. Automatic ' insert the new column at the same location Data Grid View1. Insert(index, dgvc)) Now the user can select the user-friendly Category strings and also is prevented from selecting a value that's not a valid Category ID If the 5 built-in column types is not sufficient, it is fairly easy to add a custom column type yourself.
New(New Data Grid View Masked Edit Cell()) End Sub Public Overrides Property Cell Template() As Data Grid View Cell Get Return My Base. Return Get Type(Masked Edit Editing Control) End Get End Property Public Overrides Read Only Property Value Type() As Type Get ' Return the type of the value that Masked Edit Editing Control contains. Validating Type End Get End Property Public Overrides Read Only Property Default New Row Value() As Object Get Return "" End Get End Property End Class Lastly, is the Editing Control. Back Color End Sub Public Property Editing Control Row Index() As Integer Implements _ IData Grid View Editing Control.