vb.net 2010 when press enter in datagridview move to next column

I need when press enter  in datagridview move to right column and not to next line. Windows forms.
rflorencioAsked:
Who is Participating?
 
John (Yiannis) ToutountzoglouConnect With a Mentor Instructor Multiengine PilotCommented:
It is working i test it in my datagridview...
1.create a custom control and put this code :

Public Class Mydatagrid
    Inherits DataGridView

    Protected Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean
        Dim key As Keys = keyData And Keys.KeyCode

        If key = Keys.Enter Then
            MyBase.OnKeyDown(New KeyEventArgs(keyData))
            Return True
        Else
            Return MyBase.ProcessDialogKey(keyData)
        End If
    End Function

End Class


'Here is the differnce the ProcessDialogKey...
then drag the MyDatagrid Custom Control into Your Form (if it is bounded set again the datasource )
Then in your Form Put these Lines:
 Private Sub Mydatagrid1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Mydatagrid1.KeyDown
        If e.KeyCode = Keys.Enter Then
            Dim ri As Integer = Mydatagrid1.CurrentCell.RowIndex
            Dim ci As Integer = Mydatagrid1.CurrentCell.ColumnIndex
            e.SuppressKeyPress = True
            FindNextCell(Mydatagrid1, ri, ci + 1)  'checking from Next  
        End If
    End Sub

Sub FindNextCell(ByVal dgv As Mydatagrid, ByVal rowindex As Integer, ByVal columnindex As Integer)
        Dim found As Boolean = False

        While dgv.RowCount > rowindex
            While dgv.Columns.Count > columnindex
                If Not (dgv.Rows(rowindex).Cells(columnindex)).ReadOnly Then
                    dgv.CurrentCell = dgv.Rows(rowindex).Cells(columnindex)
                    Exit Sub
                Else
                    columnindex += 1
                End If
            End While
            rowindex += 1
            columnindex = 0
        End While
    End Sub

Open in new window

0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Hi ....
Try this
Private Sub MyDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyDataGridView.KeyDown
        If e.KeyCode = Keys.Return Then
            Dim numCols As Integer = MyDataGridView.ColumnCount
            Dim numRows As Integer = MyDataGridView.RowCount
            Dim currCell As DataGridViewCell = MyDataGridView.CurrentCell
            If currCell.ColumnIndex = numCols - 1 Then
                If currCell.RowIndex < numRows - 1 Then
                    MyDataGridView.CurrentCell = MyDataGridView.Item(0, currCell.RowIndex + 1)
                End If
            Else
                MyDataGridView.CurrentCell = MyDataGridView.Item(currCell.ColumnIndex, currCell.RowIndex + 1)
            End If
            e.Handled = True
        End If
    End Sub

Open in new window

0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
very Sorry try this(this is for rows)
Just be sure to change the selection mode to Cell Select

Private Sub MyDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyDataGridView.KeyDown
        If e.KeyCode = Keys.Return Then
            Dim numCols As Integer = MyDataGridView.ColumnCount
            Dim numRows As Integer = MyDataGridView.RowCount
            Dim currCell As DataGridViewCell = MyDataGridView.CurrentCell
            If currCell.ColumnIndex = numCols - 1 Then
                If currCell.RowIndex < numRows - 1 Then
                    MyDataGridView.CurrentCell = MyDataGridView.Item(0, currCell.RowIndex + 1)
                End If
            Else
                MyDataGridView.CurrentCell = MyDataGridView.Item(currCell.ColumnIndex+1, currCell.RowIndex)
            End If
            e.Handled = True
        End If
    End Sub

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Also you have to set the edit Mode on EdirtOnKeystroke or EdirtOnKeystrokeOrF2
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Finally my first post is for moving to the next Row and the second is to move in columns cell by cell
0
 
rflorencioAuthor Commented:
Not work, when press enter cursor move to next row, with selection mode to cell select  and edit mode to editonenter
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
that is why i said edit on Keystroke....
you want it with edit on enter?Ans also try the second code
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Did you try the code?
0
 
rflorencioAuthor Commented:
yes, both, but when press enter cursor move to next row and not to next column in same row.

properties set:
Selectionmode=  Cell Select
editmode=EdirtOnKeystroke or EdirtOnKeystrokeOrF2
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
i thing you are talking about pressing enter when the cell is EditMode.and not just navigate through the cells.Correct?
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
0
 
rflorencioAuthor Commented:
Still not working, the steps I did were these:
1 - Create a custom control called Mydatagrid
2 - Drag from toolbox new control to form
3 -  Put Sub FindNextCell in form, and in keydown event of new mydatagrid control put the code above
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Strange....it works Perfectly on me....with the same steps....let me check it again...
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
I check It ...It Works
Just Copy And Paste the codeas it is
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.