Validate and focus DataGridView Cell

Validate and focus DataGridView Cell

We can use a lot of validations on DatagridView Cell.

Most of the people have some problem to focus the grid view Cell after some validation message

Ex. If any user put any string value in currency column then it show some validation message like “Enter Currency only” but after that the focus moves in another cell.

So we should use CellValidating event of DataGridView.

You can easily understand to see the following examples.

    Private Sub grdBilling_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles grdBilling.CellValidating


                If Not grdBilling.Rows(e.RowIndex).Cells(4).Value Is Nothing Then

                    If Not IsNumeric(grdBilling.Rows(e.RowIndex).Cells(4).Value) Then

                        MessageBox.Show("Enter Numeric value only", "", MessageBoxButtons.OK, MessageBoxIcon.Error)

                        e.Cancel = True


        End If

    End Sub


Here e.Cancel = True return the focus in the Validated cell