2
votes

Here is my working code of pressing Enter key to move to another cell like 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.

Thank you for your help

1
Sorry but I am not able to replicate your problem. I get it moving from column to column by pressing enter just once in any case, if the cell is not being edited. If the cell is being edited, I have to press enter twice (first time to accept the changes and second time to move a different column), independently upon the current column.varocarbas
Its okay now sir, I have made a working code for this problem :)Matthew
I am happy to read that. But what was the exact problem? As said, this code works fine at my end. If you had to perform any modification, please, update your code, explain what was wrong and your exact conditions (or write an answer with the solution). Otherwise you might want to delete this question as far as wouldn't be useful to anyone (no clear description neither of the problem nor of the solution).varocarbas
Ah ok sir tnx for reminding me, The main problem here is that when I in the last row, I have to press enter twice after editing a cell, and I will also put my solution for this problem.Matthew
Yes. But bear in mind that if you are editing a cell (in the last row or in the first one or in any row), you would have always to press enter twice (with this approach): first time to store the value in the cell and second time to perform the moving to a different column. Thanks for updating the question, it might be helpful to others.varocarbas

1 Answers

2
votes

This is my edited code in the CellandEdit and 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.

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.

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. 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.

And also I change the properties of my DGV of the EditMode to:EditonKeystrokeOrF2

This code is perfectly working for me, I do not change code in the keydown event.