In your example, you use the Cell Validating event, as shown below: Here, you want to ensure that the length of the new Customer ID is at least five characters.You do this by using the Formatted Value property of the Data Grid View Cell Validating Event Args parameter.Hovering the mouse pointer on the icon displays the Error Text that you set earlier.
Displaying a Combo Box in Cell Frequently, besides displaying text in a cell you may want to display a drop-down list box to allow users to select from a list of pre-determined values.
In this case, you need to add a Combo Box to the cells in the desired column.
Notice how the Column Index and Row Index properties of Data Grid View Cell Validating Event Args parameter are used.
At run time, if the validation fails, the Data Grid View displays a red exclamation icon in that cell (see below).
Data Source = bindingsource '---add a combobox column to the Data Grid View control--- Data Grid View1. Add(combo Box Col) The previous code showed how you can bind a Binding Source control (containing a list of items to let the users choose) to a Data Grid View Combo Box Column control inside a Data Grid View control.
Add("Price", "Price") '---create a new bindingsource control--- Dim bindingsource As New Binding Source '---add the items into the control--- bindingsource. Header Text = "Types" '---data bind it--- combo Box Col.
Add("Type C") '---create a combobox column--- Dim combo Box Col As New Data Grid View Combo Box Column '---set the header--- combo Box Col.
Header Text = "Types" '---add items to it--- combo Box Col.
To perform any custom validation on the cell or row being edited, you can use the Cell Validating and Row Validation events, respectively.
If you want to validate the data cell by cell, the former event is useful where you want to validate the data for the entire row at once then the later event is handy.
There are two flavors, passive and supervising controller.