Datagridview rowvalidating example
Note that only the Cell Validating event can keep the editor active,when the validation fails and it does not allow the user to exit the cell until a valid value is filled.However, pressing the Esc key in this situation will revert the initial value of the cell and it will close the editor. The Row Validating event has a slightly different behavior.If it is less than five characters, you set the Error Text property of that cell to some error message.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).
This all works fine when the user is editing and adding rows through UI interaction, but creates a problem when the Data Source is set.
You do this by using the Formatted Value property of the Data Grid View Cell Validating Event Args parameter.
This property returns the new value entered in the cell.
When the validation fails, the editor is closed, but the user cannot change the current row in the grid until a valid cell value is entered.
In this situation the editor is not kept active when the validation fails and pressing the Esc key does not revert the changes as the editor is already closed.
The Data Grid View control of Windows Forms is a versatile control that can help you create professional-looking data entry forms.