简体   繁体   English

ENTER键作为TAB在vb.net中的datagridview问题中

[英]ENTER key as TAB in datagridview issue in vb.net

Here is my working code of pressing Enter key to move to another cell like TAB: 这是我按Enter键移动到另一个单元格(如TAB)的工作代码:

Private Sub dvFromAlloc_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dvFromAlloc.KeyDown
    If e.KeyCode = Keys.Enter Then

        Dim colm As Integer = dvFromAlloc.ColumnCount - 1
        Dim row As Integer = dvFromAlloc.RowCount - 1
        Dim currCell As DataGridViewCell = dvFromAlloc.CurrentCell

        If currCell.ColumnIndex = colm Then
            If currCell.RowIndex < row Then
                'gets the next row and the first selected index
                dvFromAlloc.CurrentCell = dvFromAlloc.Item(0, currCell.RowIndex + 1)
            End If
        Else
            'move in next col in the current row
            dvFromAlloc.CurrentCell = dvFromAlloc.Item(currCell.ColumnIndex + 1,  currCell.RowIndex)
        End If

        e.Handled = True
    End If

End Sub

Private Sub dvFromAlloc_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dvFromAlloc.CellEndEdit
    isEdited = True
    iColumnindex = e.ColumnIndex
    irowindex = e.RowIndex
End Sub

Private Sub dvFromAlloc_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dvFromAlloc.SelectionChanged
    If isEdited Then
        isEdited = False
        dvFromAlloc.CurrentCell = dvFromAlloc.Item(iColumnindex + 1, irowindex)
    End If
End Sub

The main problem here is that when I'm in the last row, I have to press enter twice after editing a cell before it moves to another cell, but if I'm in the other rows, I will only press enter once after editing the cell and it will move to the next cell. 这里的主要问题是,当我在最后一行中时,必须在编辑一个单元格后按两次Enter键,然后再将其移至另一个单元格,但是如果我在其他行中,则仅在编辑后按一次Enter键单元格,它将移动到下一个单元格。

Thank you for your help 谢谢您的帮助

This is my edited code in the CellandEdit and SelectionChanged: 这是我在CellandEdit和SelectionChanged中编辑的代码:

Private Sub dvFromAlloc_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dvFromAlloc.CellEndEdit
    isEdited = True
    iColumnindex = e.ColumnIndex
    irowindex = e.RowIndex

    If dvFromAlloc.CurrentRow.Index = dvFromAlloc.RowCount - 1 Then
        If dvFromAlloc.CurrentCell.ColumnIndex < dvFromAlloc.ColumnCount - 1 Then
            dvFromAlloc.CurrentCell = dvFromAlloc.Item(iColumnindex + 1, irowindex)
        End If
        isEdited = False
    End If

End Sub

In the code above(CellEndEdit), I have a first If statement: If the current row index is equals to the row count(that means I am checking if I am in the last rows) then I will execute the second If statement. 在上面的代码(CellEndEdit)中,我有第一个If语句:如果当前行索引等于行数(这意味着我正在检查我是否在最后一行中),那么我将执行第二个If语句。

The second If statement: if the current column index is less than the column count, then I will move the current cell to the next cell. 第二条If语句:如果当前列索引小于列数,则将当前单元格移至下一个单元格。

Private Sub dvFromAlloc_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dvFromAlloc.SelectionChanged

    If isEdited Then
        If dvFromAlloc.CurrentCell.ColumnIndex < dvFromAlloc.ColumnCount - 1 Then
            dvFromAlloc.CurrentCell = dvFromAlloc.Item(iColumnindex + 1, irowindex)
        Else
            dvFromAlloc.CurrentCell = dvFromAlloc.Item(1, irowindex + 1)
        End If
        isEdited = False

    End If

In the code above(SelectionChanged), I have put an If else statement: If the current cell column index is less than the column count, then I will move to the next cell until I reach the last column. 在上面的代码中(SelectionChanged),我放置了一个If else语句:如果当前单元格的列索引小于列数,那么我将移至下一个单元格,直到到达最后一列。 Elseif I am in the last column, then I set the current cell to the column index 1 and increment the row index to 1 so that it will move to the next row. Elseif如果我在最后一列中,那么我将当前单元格设置为列索引1,并将行索引递增到1,以便它将移至下一行。

And also I change the properties of my DGV of the EditMode to:EditonKeystrokeOrF2 我还将EditMode的DGV的属性更改为:EditonKeystrokeOrF2

This code is perfectly working for me, I do not change code in the keydown event. 这段代码对我来说是完美的,我不会在keydown事件中更改代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM