Could not Focus Cell In DataGridView
-
Hi Experts, After my validation my control goes to the next cell..I have written the code to focus the current cell.But could not get the same.... code is ---> Private Sub dgvstock_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgvstock.CellValidating Dim cell As DataGridViewCell = dgvstock.Item(e.ColumnIndex, e.RowIndex) Dim ret As Integer If cell.IsInEditMode Then Dim c As Control = dgvstock.EditingControl Select Case dgvstock.Columns(e.ColumnIndex).Name Case "Sr No.", "Amount" c.Text = CleanInputNumber(c.Text) If c.Text = "" Then ret = MessageBox.Show("Enter Only Numbers", "Invalid Input") If ret = 1 Then dgvstock.CurrentCell = dgvstock(e.ColumnIndex, e.RowIndex) //Here i want the focus on current cell End If End If End Select End If End Sub Private Function CleanInputAlphabet(ByVal str As String) As String Return System.Text.RegularExpressions.Regex.Replace(str, "[0-9\b\s-]", "") End Function
-
Hi Experts, After my validation my control goes to the next cell..I have written the code to focus the current cell.But could not get the same.... code is ---> Private Sub dgvstock_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgvstock.CellValidating Dim cell As DataGridViewCell = dgvstock.Item(e.ColumnIndex, e.RowIndex) Dim ret As Integer If cell.IsInEditMode Then Dim c As Control = dgvstock.EditingControl Select Case dgvstock.Columns(e.ColumnIndex).Name Case "Sr No.", "Amount" c.Text = CleanInputNumber(c.Text) If c.Text = "" Then ret = MessageBox.Show("Enter Only Numbers", "Invalid Input") If ret = 1 Then dgvstock.CurrentCell = dgvstock(e.ColumnIndex, e.RowIndex) //Here i want the focus on current cell End If End If End Select End If End Sub Private Function CleanInputAlphabet(ByVal str As String) As String Return System.Text.RegularExpressions.Regex.Replace(str, "[0-9\b\s-]", "") End Function
//This will written in the EditingControlShowing event of datagridview If TypeOf e.Control Is TextBox Then DirectCast(e.Control, TextBox).CharacterCasing = CharacterCasing.Upper End If
-
Hi Experts, After my validation my control goes to the next cell..I have written the code to focus the current cell.But could not get the same.... code is ---> Private Sub dgvstock_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgvstock.CellValidating Dim cell As DataGridViewCell = dgvstock.Item(e.ColumnIndex, e.RowIndex) Dim ret As Integer If cell.IsInEditMode Then Dim c As Control = dgvstock.EditingControl Select Case dgvstock.Columns(e.ColumnIndex).Name Case "Sr No.", "Amount" c.Text = CleanInputNumber(c.Text) If c.Text = "" Then ret = MessageBox.Show("Enter Only Numbers", "Invalid Input") If ret = 1 Then dgvstock.CurrentCell = dgvstock(e.ColumnIndex, e.RowIndex) //Here i want the focus on current cell End If End If End Select End If End Sub Private Function CleanInputAlphabet(ByVal str As String) As String Return System.Text.RegularExpressions.Regex.Replace(str, "[0-9\b\s-]", "") End Function
Datagridview1.CurrentCell = Datagridview1('Your column index', Datagridview1.NewRowIndex) Datagridview1.BeginEdit(True)