首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CellEndEdit之后的DataGridView SetFocus

CellEndEdit之后的DataGridView SetFocus
EN

Stack Overflow用户
提问于 2013-07-07 18:27:19
回答 2查看 12K关注 0票数 3

我使用了CellEndEdit事件,在编辑单元格值之后,我按Enter键,然后单元格焦点下移。

我希望焦点回到我编辑值的原始单元格。

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

代码语言:javascript
复制
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方法,但我不认为我必须使用该方法来获得我想要的东西。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-07 21:54:28

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

代码语言:javascript
复制
Private flag_cell_edited As Boolean
Private currentRow As Integer
Private currentColumn As Integer

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

代码语言:javascript
复制
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事件处理程序中,使用currentRowcurrentColumn变量将DataGridViewCurrentCell属性设置为最后编辑的单元格,以撤消默认的单元格焦点更改:

代码语言:javascript
复制
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
票数 7
EN

Stack Overflow用户

发布于 2020-12-02 23:18:00

安德里亚的想法是使用DataGridViewCell来处理事情:

代码语言:javascript
复制
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 Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17511317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档