在CellEndEdit之后的DataGridView SetFocus [英] DataGridView SetFocus after CellEndEdit

查看:185
本文介绍了在CellEndEdit之后的DataGridView SetFocus的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我想要重点回到原始单元格,在那里我编辑了这个值。



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

  Private Sub DataGridVie1_CellEndEdit(ByVal sender As System.Object,ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)处理DataGridVie1.CellEndEdit 
' ...
'....编辑代码在这里...输入和验证值...
'...
'...
'...在结束之前如果程序我把这个值
DataGridVie1.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected = True
DataGridVie1.CurrentCell = DataGridVie1.Rows(e.RowIndex) .Cells(e.ColumnIndex)
'DataGridVie1.BeginEdit(False)'''DID不适用于此,因为它会导致再次编辑。
结束如果

编辑后或输入后不知道真正的代码KEY的重点是在原来的编辑单元格。



因为每次我按ENTER键,它直接下到下一个单元格。



什么代码将焦点重新定位到原始的单元格编辑。



我知道 EditingControlShowing 方法,但我不认为我必须使用该方法来获得我想要的。

解决方案

尝试这样做:定义3个变量。
一个记住编辑操作是否存在,另一个2存储上次编辑的单元格的行和列索引:

  Private flag_cell_edited As Boolean 
Private currentRow As Integer
私有currentColumn As Integer

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



私有子DataGridView1_CellEndEdit(ByVal sender As Object,ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)处理DataGridView1.CellEndEdit
flag_cell_edited = True
currentColumn = e .ColumnIndex
currentRow = e.RowIndex
End Sub

然后在code> SelectionChanged 事件处理程序您设置 DataGridView CurrentCell 属性到最后编辑的单元格使用 currentRow currentColumn 故障单元格焦点更改:

  Private Sub DataGridView1_SelectionChanged(ByVal sender As Object,ByVal e As System.EventArgs)处理DataGridView1.SelectionChanged 
如果flag_cell_edited然后
DataGridView1.CurrentCell = DataGridView1(currentColumn,currentRow)
flag_cell_edited = False
End If
End Sub


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

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.

I know the EditingControlShowing method but I don't think I have to used that method to get what I wanted too.

解决方案

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

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 

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆