我使用了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我不知道在编辑之后或者在回车键之后焦点回到被编辑的原始单元格中时的真实代码。
因为每次我按回车键,它都会直接转到下一个单元格。
将焦点重新定位回编辑的原始单元格的代码是什么?
我知道EditingControlShowing方法,但我不认为我必须使用该方法来获得我想要的东西。
发布于 2013-07-07 21:54:28
试试这个:定义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和currentColumn变量将DataGridView的CurrentCell属性设置为最后编辑的单元格,以撤消默认的单元格焦点更改:
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发布于 2020-12-02 23:18:00
安德里亚的想法是使用DataGridViewCell来处理事情:
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 Subhttps://stackoverflow.com/questions/17511317
复制相似问题