Click here to Skip to main content
16,018,805 members
Articles / Programming Languages / SQL
Tip/Trick

DataGridView: Make Enter Key Move to Next Column

Rate me:
Please Sign up or sign in to vote.
1.77/5 (5 votes)
7 Dec 2011CPOL 80.2K   5   8
DataGridView: Make Enter Key Move to Next Column
To make a datagridview move to the next column when you press the enter key.

Using Enter Key to Move to nextCell...

VB
Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
    SendKeys.Send("{up}")
    SendKeys.Send("{right}")
End Sub
Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
    If e.KeyCode = Keys.Enter Then
        SendKeys.Send("{up}")
        SendKeys.Send("{right}")
    End If

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student Self Employed
India India
« I Love to Code ».
In the meantime I like to surf the Internet and get to know about new Technology, Watching Movies and playing cricket.


CodeProject helped me a lot to learn and I am always thankful to all the Members who make this happen.

Comments and Discussions

 
QuestionLame code Pin
Lucky_Luk€4-Mar-17 2:23
Lucky_Luk€4-Mar-17 2:23 
QuestionDoesn't work Pin
Nitesh Kejriwal31-Jan-17 20:51
professionalNitesh Kejriwal31-Jan-17 20:51 
QuestionDatagridview Move Right On Enter Pin
Member 160021029-Nov-14 20:48
Member 160021029-Nov-14 20:48 
The Following Code I Got On A Site, It Worked For Me But One Problem Faced Me If One Of The Cells In My Datagrid Is Not Visible, So I Added A Code To Check If The Next Cell Is Not Visible Move To The Next. I Am Not Saying Its A Professional Solution The Way I Entered The Code To It But It Worked Fine For Me, I Hope It Works For Others
VB
Private Sub gvInvoice_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles gvInvoice.KeyDown
       If e.KeyCode = Keys.Enter Then
           Dim ri As Integer = gvInvoice.CurrentCell.RowIndex
           Dim ci As Integer = gvInvoice.CurrentCell.ColumnIndex
           e.SuppressKeyPress = True
           FindNextCell(gvInvoice, ri, ci + 1)  'checking from Next
       End If
   End Sub

   Sub FindNextCell(ByVal dgv As DataGridView, 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
                   If dgv.Rows(rowindex).Cells(columnindex).Visible = True Then
                       dgv.CurrentCell = dgv.Rows(rowindex).Cells(columnindex)
                       Exit Sub
                   Else
                       columnindex += 2
                   End If
               Else
                   columnindex += 1
               End If
           End While
           rowindex += 1
           columnindex = 0
       End While
   End Sub

QuestionAn Alternative to Above Code Pin
Nikhil Bhivgade26-Apr-14 1:40
professionalNikhil Bhivgade26-Apr-14 1:40 
AnswerRe: An Alternative to Above Code Pin
Ratnesh Mani Tripathi9-Jan-15 23:23
Ratnesh Mani Tripathi9-Jan-15 23:23 
GeneralMy vote of 3 Pin
Dr. Frank Heimes24-Mar-14 23:00
Dr. Frank Heimes24-Mar-14 23:00 
GeneralMake Enter Key Move to Next Column Pin
Bhongya16-Aug-12 8:38
Bhongya16-Aug-12 8:38 
GeneralRe: Make Enter Key Move to Next Column Pin
Jαved17-Aug-12 19:48
professionalJαved17-Aug-12 19:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.