Search code examples
.netwinformsdatagridviewfocuscell

auto move focus to next cell datagridview


I have a datagridview(DGV) in whic i want the first cell of the row to be serial number, so i put up this code.

  Private Sub dgvStudents_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVStudents.RowEnter
        Dim RowIndex As Integer = e.RowIndex
        Debug.Print(RowIndex.ToString)
        With DGVStudents
            .Rows(e.RowIndex).Cells(0).Value = e.RowIndex + 1
            If .Rows(e.RowIndex).Cells(0).Selected = True Then
                .Rows(e.RowIndex).Cells(0).Selected = False
                .Rows(e.RowIndex).Cells(1).Selected = True
            End If
        End With
        DGVStudents.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
    End Sub

The main aim is on entering a new row, number the first cell and move focus to the next. putting it in edit mode, but i can seem to get it to work. any help?


Solution

  • I believe you are actually in the edit mode of the first cell, Maybe you could push this into the CellEnter event and in case its the first cell use SendKeys.Send("{TAB}") to move you to the next cell. Also if the value is not going to be changed by the user set the first column as ReadOnly too.

    You can also try by setting the CurrentCell property of the DataGridView something of this sort

    datagridview1.CurrentCell = Rows(e.RowIndex).Cells(1);