CellEndEdit之后的DataGridView SetFocus

XXX*_*XXX 3 vb.net desktop datagridview winforms

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

我希望焦点回到我编辑值的原始Cell.

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

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
Run Code Online (Sandbox Code Playgroud)

我不知道编辑后或输入键后的实际代码,焦点回到编辑的原始单元格中.

因为每次我点击ENTER键,它直接进入下一个Cell.

是什么代码将焦点重新定位回原始Cell编辑.

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

And*_*rea 7

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

Private flag_cell_edited As Boolean
Private currentRow As Integer
Private currentColumn As Integer
Run Code Online (Sandbox Code Playgroud)

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

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 
Run Code Online (Sandbox Code Playgroud)

然后,在SelectionChanged事件处理程序中设置DataGridViewCurrentCell使用属性到最后编辑的单元格currentRowcurrentColumn变量撤消默认的单元格焦点的变化:

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
Run Code Online (Sandbox Code Playgroud)