If you want to prevent the user from leaving the current cell if they have set a wrong value, set e.Cancel = True on the CellValidating event. For example this code stops the user leaving the cell in the second column, if they didn't type a number:
Private Sub dataGridView1_CellValidating(sender as Object, e as DataGridViewCellValidatingEventArgs e) Handles dataGridView1.CellValidating
'set cancel to true if colindex is 1 and TryParse returned false (not a number)
e.Cancel = (e.ColumnIndex = 1 AndAlso Not Int32.TryParse(e.FormattedValue.ToString(), Nothing)
End Sub
To clear the input editor, you have to appreciate that you aren't editing the cell, you're typing into a textbox that is being drawn ON TOP of the place where the cell is. There is, conceptually, only one editor textbox per datagridview at any one time (and it puts it in whatever place is relevant for the current cell). The control that edits a value for the current cell is hence nothing to do with the cell, but is a feature of the grid itself, and is accessed by the dataGridView1.EditingControl. This returns a Control; it can be any kind of forms control (textbox, checkbox, datepicker etc) that edits the cell, hence why EditingControl returns a base class type of Control. Control has a .Text property and a .ResetText() method so we can clear the textbox without needing to cast it (but if we were using a date picker etc it might be that it needed casting before eg accessing its Value). This can reset the textbox:
Private Sub dataGridView1_CellValidating(sender as Object, e as DataGridViewCellValidatingEventArgs e) Handles dataGridView1.CellValidating
If e.ColumnIndex = 1 AndAlso Not Int32.TryParse(e.FormattedValue.ToString(), Nothing) Then
e.Cancel = true
dataGridView.EditingControl.ResetText()
End If
End Sub
CellValidatingevent handler. That's it, that's all. The whole point of that event is what you are trying to achieve. - jmcilhinney