CellEndEdit 后的 DataGridView SetFocus [英] DataGridView SetFocus after CellEndEdit
问题描述
我使用了 CellEndEdit
事件,在编辑单元格值后按 Enter 键,然后单元格焦点向下移动.
I used the CellEndEdit
event, after editing the cell value I press the Enter Key, then the cell focus moves down.
我希望焦点回到我编辑值的原始单元格.
I want the focus to go back at the original Cell where I edited the value.
我用了很多方法,但都失败了.
I used many ways, but failed.
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 键后焦点回到被编辑的原始单元格时的真实代码.
I don't know the real code when after edit or after a ENTER KEY the focus is back in the original Cell that was edited.
因为每次我按回车键,它都会直接进入下一个单元格.
Because everytime I HIT the ENTER KEY, it directly go down to the next Cell.
将焦点重新定位回原始单元格编辑的代码是什么.
Whats the code to reposition the focus back to the original Cell edited.
我知道 EditingControlShowing
方法,但我不认为我也必须使用该方法来获得我想要的.
I know the EditingControlShowing
method but I don't think I have to used that method to get what I wanted too.
推荐答案
试试这个:定义 3 个变量.一个用于记忆是否进行了编辑操作,另外两个用于存储最后编辑单元格的行和列索引:
Try this: define 3 variables. One to memorize if an edit operation has been made, the other 2 to store row and column indexes of the last edited cell:
Private flag_cell_edited As Boolean
Private currentRow As Integer
Private currentColumn As Integer
当编辑操作发生时,你存储编辑单元格的坐标并在 CellEndEdit
事件处理程序中设置你的标志为 true:
When an edit operation occurs you store coordinates of edited cell and set your flag to true inside CellEndEdit
event handler:
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
变量来撤消默认的单元格焦点更改:
Then in SelectionChanged
event handler you set DataGridView
's CurrentCell
property to the last edited cell using currentRow
and currentColumn
variables to undo default cell focus change:
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
这篇关于CellEndEdit 后的 DataGridView SetFocus的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!