我在具有4列的表单中使用了DataGridView功能:项目代码,描述,数量,价格。我想在项目代码插入单元格时,焦点应移动到同一行的第3个单元格,即第3列(数量)。直到这里工作正常,因为我提到了这个链接。
DataGridView SetFocus after CellEndEdit
现在的问题是它被卡在第3个单元格上,当我按下回车键时,它不会移动到下一行第一列(项目代码)。我的代码是
Private flag_cell_edited As Boolean
Private currentRow As Integer
Private currentColumn As Integer
Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
flag_cell_edited = True
currentColumn = e.ColumnIndex
currentRow = e.RowIndex
MsgBox(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
End Sub
Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
If flag_cell_edited Then
DataGridView1.CurrentCell = DataGridView1.Rows(currentRow).Cells(2)
flag_cell_edited = False
End If
End Sub
我使用MsgBox查看插入到单元格中的数据,以便稍后我可以将其存储到数据库中。
答案 0 :(得分:0)
当您在第三个单元格上按Enter键时,您将获得CellEndEdit事件,就像您在第一个单元格中结束编辑一样。只有在第一个单元格上时才需要触发第三个单元格上的移动
Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
if e.ColumnIndex = 0 Then
flag_cell_edited = True
currentColumn = e.ColumnIndex
currentRow = e.RowIndex
Else
flag_cell_edited = False
End If
End Sub
Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
If flag_cell_edited Then
DataGridView1.CurrentCell = DataGridView1.Rows(currentRow).Cells(0)
flag_cell_edited = False
End If
End Sub