Highlight text (for edit) if validation fails in a DataGridView
Hi all,
In a DataGridView.
How do i highlight the entered text after the validation fails (e.Cancel = True) so user can just start typing and replacing the wrong text. To compare - In a textbox i would use:
textbox1.selectionstart=0
textbox1.selectionlength = len(textbox1.text)
I have searched but was unable to find any clue.
Louie
[379 byte] By [
LouieG] at [2008-1-10]
Hi all,
I got a solution myself.
Private Sub Grid_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles Grid.CellValidating
Select Case e.ColumnIndex
Case 0
If Grid.Rows(e.RowIndex).IsNewRow Then Return
If
Not
IsNumeric(e.FormattedValue) Then
e.Cancel =
True
errorMsg(e.FormattedValue & " is not a number") '<-- my own error message displayer
Grid.CancelEdit() '<- this is the statement that does the trick
End
If
End
Select
End
Sub
--
The invalid input is removed and the user is returned to an empty cell to just start typing again.
The user enters "qwerty" then the msg appears - when he/she press OK on the error message the cell is blank.
However if there was a valid value in the cell and the user enters an invalid string then the previous "correct" value is restored, which is great.
Example: the cell's value is "13" - the user get to the cell, he/she enters QWERTY , the error message pops up , the OK button is pressed , the 13 is back in the cell.
Louie