vb.net DataGridView在按enter键或tab键时如何保持在当前单元格中?

vb.net DataGridView在按enter键或tab键时如何保持在当前单元格中?,vb.net,datagridview,Vb.net,Datagridview,现在我正在编写代码,在我更改单元格中的文本后,当我按enter键或tab键时,当前的单元格将保留在我修改的单元格中。我在这里搜索过这个() 但它确实对我有用。我犯了什么错误 Public row1, col1 As Integer Public selectedPart1 As String Private Sub DataGridView1_KeyDown(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyDown

现在我正在编写代码,在我更改单元格中的文本后,当我按enter键或tab键时,当前的单元格将保留在我修改的单元格中。我在这里搜索过这个() 但它确实对我有用。我犯了什么错误

Public row1, col1 As Integer
Public selectedPart1 As String
Private Sub DataGridView1_KeyDown(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyDown

    row1 = DataGridView1.CurrentCell.RowIndex
    col1 = DataGridView1.CurrentCell.ColumnIndex    
    
    selectedPart1 = DataGridView1(0, row1).Value.ToString

    If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab Then
        MsgBox("ok")
        DataGridView1.CurrentCell = DataGridView1(col1, row1)
        e.SuppressKeyPress = True
    End If

End Sub

要禁用选项卡键,可以将
StandardTab
属性设置为True:

DataGridView1.StandardTab = True
要在编辑后禁用Enter键,请使用事件
CellEndEdit
SelectionChanged

Private currentRow, currentCell As Integer
Private resetRow As Boolean = False

Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
    If resetRow Then
        resetRow = False
        DataGridView1.CurrentCell = DataGridView1.Rows(currentRow).Cells(currentCell)
    End If
End Sub

Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
    resetRow = True
    currentRow = e.RowIndex
    currentCell = e.ColumnIndex
End Sub