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.Cell Template End Get Set(By Val value As Data Grid View Cell) ' Ensure that the cell used for the template is a Masked Edit Cell If Not (value Is Nothing) And Not value. Is Assignable From( _ Get Type(Data Grid View Masked Edit Cell)) Then Throw New Invalid Cast Exception("Must be a Data Grid View Masked Edit Cell") End If My Base. It inherits from the Masked Text Box control (to get the user interface we want) and implements the Editing Control interface so that the control works inside the Data Grid View. Editing Control Row Index Get Return row Index Num End Get Set(By Val value As Integer) row Index Num = value End Set End Property Public Function Editing Control Wants Input Key(By Val key As Keys, By Val _ data Grid View Wants Input Key As Boolean) As Boolean Implements _ IData Grid View Editing Control.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.The remaining overrides are to adjust to the fact that the Masked Text Box control supports multiple "validating types". Editing Control Formatted Value Get Return Me.value Is Changed. End Sub Public Read Only Property Reposition Editing Control On Value Change() As Boolean _ Implements IData Grid View Editing Control.Public Class Data Grid View Masked Edit Cell Inherits Data Grid View Text Box Cell Dim p Column As Data Grid View Masked Edit Column Public Sub New() End Sub Public Overrides Sub Initialize Editing Control(By Val row Index As Integer, By Val _ initial Formatted Value As Object, By Val data Grid View Cell Style As Data Grid View Cell Style) My Base. To String End Get Set(By Val value As Object) If Type Of value Is [String] Then Me. To String End If End Set End Property Public Function Get Editing Control Formatted Value(By Val context As _ Data Grid View Data Error Contexts) As Object Implements _ IData Grid View Editing Control. Text End Function Public Sub Apply Cell Style To Editing Control(By Val data Grid View Cell Style As _ Data Grid View Cell Style) Implements _ IData Grid View Editing Control. Reposition Editing Control On Value Change Get Return False End Get End Property Public Property Editing Control Data Grid View() As Data Grid View Implements _ IData Grid View Editing Control. 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.
Data Property Name = "Category ID" ' the matching column from the "base" table dgvc.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.' ' 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.