It also implements the properties that will be required later on by the Masked Text Box control, namely the Mask, Prompt Char, and Validating Type properties.
Public Class Data Grid View Masked Edit Column Inherits Data Grid View Column Private p Prompt Char As Char = "_"c Private p Validating Type As Type = Get Type(String) Private p Mask As String = "" Public Sub New() My Base. Prompt Char End Sub Public Overrides Read Only Property Edit Type() As Type Get ' Return the type of the editing control that Masked Edit Editing Control uses.
The Data Grid View is a very powerful windows form control for providing a user interface to tabular data.
It has the initial "look and feel" of an Excel spreadsheet.
Data Property Name = "Category ID" ' the matching column from the "base" table dgvc.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.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.