vb.net CellEndEdit 后的 DataGridView SetFocus
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17511317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGridView SetFocus after CellEndEdit
提问by XXXXXXXXXXXXXX
I used the CellEndEditevent, after editing the cell value I press the Enter Key, then the cell focus moves down.
我使用了该CellEndEdit事件,在编辑单元格值后按 Enter 键,然后单元格焦点向下移动。
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
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.
我不知道真正的代码是在编辑之后还是在 ENTER 键之后焦点又回到了被编辑的原始单元格中。
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.
将焦点重新定位回原始单元格编辑的代码是什么。
I know the EditingControlShowingmethod but I don't think I have to used that method to get what I wanted too.
我知道该EditingControlShowing方法,但我认为我不必也使用该方法来获得我想要的东西。
回答by Andrea
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:
试试这个:定义 3 个变量。一个用于记忆是否进行了编辑操作,另外两个用于存储最后编辑单元格的行和列索引:
Private flag_cell_edited As Boolean
Private currentRow As Integer
Private currentColumn As Integer
When an edit operation occurs you store coordinates of edited cell and set your flag to true inside CellEndEditevent handler:
当发生编辑操作时,您存储编辑单元格的坐标并将您的标志设置为 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
Then in SelectionChangedevent handler you set DataGridView's CurrentCellproperty to the last edited cell using currentRowand currentColumnvariables to undo default cell focus change:
然后在SelectionChanged事件处理程序中,您使用和变量将DataGridView的CurrentCell属性设置为最后编辑的单元格以撤消默认单元格焦点更改: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

