我使用了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
方法,但我认为我不必使用该方法来获得我想要的东西。
最佳答案
试试这个:定义 3 个变量。 一个用于记住是否进行了编辑操作,另外两个用于存储最后编辑的单元格的行和列索引:
Private flag_cell_edited As Boolean
Private currentRow As Integer
Private currentColumn As Integer
发生编辑操作时,您存储已编辑单元格的坐标,并在 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
然后在 SelectionChanged
事件处理程序中,使用 currentRow
将 DataGridView
的 CurrentCell
属性设置为最后编辑的单元格和 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
关于vb.net - CellEndEdit 之后的 DataGridView SetFocus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17511317/