vb.net - CellEndEdit 之后的 DataGridView SetFocus

标签 vb.net winforms datagridview desktop

我使用了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 事件处理程序中,使用 currentRowDataGridViewCurrentCell 属性设置为最后编辑的单元格和 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/

相关文章:

c# - 如何将 Visual Basic 中的代码转换为 C#

c# - 无法清除 Winforms-ListBox

c# - 水平滚动条在 DataGridView 上不可见

c# - 使用 sql 查询结果填充 datagridview

c# - 设计模式/想法根据触发器将执行的操作刷新网格中的行

mysql - VB.Net 和 MySQL(如何制作自动植入的安装程序)

vb.net - 为什么我必须在这里双投?

c# - 在外部单击时如何隐藏控件?

c++ - 如何将对 Winform 成员函数的引用传递给不同线程的不同类?

c# - 如何计算数据库中列的字符长度并将它们放入 datagridview 中显示的新列中