现在我正在编写代码,当我更改单元格中的文本,然后当我按enter键或tab键时,当前单元格将停留在我修改的同一个单元格中。我在这里搜索了一下那个(Link),但它对我不起作用。我做错了什么?
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
发布于 2020-12-11 09:51:45
要禁用Tab键,可以将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
https://stackoverflow.com/questions/65248282
复制