3

我使用了CellEndEdit事件,在编辑单元格值后按 Enter 键,然后单元格焦点向下移动。

我希望焦点回到我编辑值的原始单元格。

我用了很多方法,但都失败了。

Private Sub DataGridVie1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridVie1.CellEndEdit
   '...
   '....editing codes here...to input and validate values...
   '...
   '...
   '...before the End If of the procedure I put this values
   DataGridVie1.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected = True
   DataGridVie1.CurrentCell = DataGridVie1.Rows(e.RowIndex).Cells(e.ColumnIndex)
   'DataGridVie1.BeginEdit(False) '''DID NOT apply this because it cause to edit again.
End If

当编辑后或输入键后,我不知道真正的代码,焦点又回到了被编辑的原始单元格中。

因为每次我按 ENTER 键时,它都会直接进入下一个单元格。

将焦点重新定位回已编辑的原始单元格的代码是什么。

我知道这种EditingControlShowing方法,但我认为我也不必使用那种方法来获得我想要的东西。

4

2 回答 2

7

试试这个:定义 3 个变量。一个用于记忆是否进行了编辑操作,另一个用于存储最后一个编辑单元格的行和列索引:

Private flag_cell_edited As Boolean
Private currentRow As Integer
Private currentColumn As Integer

当发生编辑操作时,您存储已编辑单元格的坐标并将您的标志设置为 true 在CellEndEdit事件处理程序中:

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
End Sub 

然后在SelectionChanged事件处理程序中,您将DataGridView'sCurrentCell属性设置为最后编辑的单元格,currentRow并使用currentColumn变量来撤消默认单元格焦点更改:

Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
    If flag_cell_edited Then
        DataGridView1.CurrentCell = DataGridView1(currentColumn, currentRow)
        flag_cell_edited = False
    End If
End Sub
于 2013-07-07T13:54:28.447 回答
0

Andrea 的想法,但使用 aDataGridViewCell来处理事情:

Private LastCell As DataGridViewCell = Nothing

Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
    LastCell = DataGridView1.CurrentCell
End Sub 

Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
    If LastCell IsNot Nothing Then
        DataGridView.CurrentCell = LastCell
        LastCell = Nothing
    End If
End Sub
于 2020-12-02T15:17:59.643 回答