How to stop user from entering an invalid value on a DataGridView
Posted on 2007-03-19
I have a DataGridView with 7 Columns. I have set all columns to readonly = true except the last column. The user is supposed to enter inventory counts in the last column of the dgv. I want to validate each entry as being numeric as soon as the user enters a value.
I added code to the 'CellLeave' function and I can successfully trap the error - but how do I force the cursor back to the cell in question (I would have called this 'setfocus' in VB6).
Here is some code:
Private Sub dgvInventoryCount_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvInventoryCount.CellLeave
If e.ColumnIndex = 7 Then
Dim CellValue As String = dgvInventoryCount.CurrentCell.Value.ToString
If IsNumeric(CellValue) = False Then
MessageBox.Show("Invalid Entry - must be a number", "Invalid Count Quantity")
'*** WANT TO KEEP FOCUS ON THE CURRENT CELL HERE ***
As always, I'm open to attacking this from a different direction if my fundamental logic is flawed.