Click here to Skip to main content
15,907,492 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I'm browsing the DGV that when he hit enter change column.
Have searched the net but the examples I found did not work, if you guys can help me.

I thank you.
I've tried, but it's not what I need
VB
If e.KeyCode = Keys.Enter Then
    SendKeys.Send (Keys.Tab)
end If

I've tried another one below it worked, but when I use a button to enter it works like tab, or form is turned in, I want to just stay active in DataGridView.
VB
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
    If (Me.DataGridView1.Focused OrElse Me.DataGridView1.EditMode) AndAlso keyData = Keys.Enter Then
        SendKeys.Send("{TAB}")
        Return True
    End If

    Return MyBase.ProcessCmdKey(msg, keyData)

End Function

is not what I need.

need that every time I press enter, it will enter that cell to cell and the column next to the finish line, not down.

I found this on the internet, it was almost but still not what I need.
VB
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean


        If keyData = Keys.Enter Then
            If TypeOf Me.ActiveControl Is DataGridViewTextBoxEditingControl OrElse _
              TypeOf Me.ActiveControl Is DataGridViewComboBoxEditingControl Then


                If dgv1.CurrentCell.ColumnIndex < dgv1.ColumnCount - 1 Then


                    dgv1.CurrentCell = dgv1.Rows(dgv1.CurrentCell.RowIndex).Cells(dgv1.CurrentCell.ColumnIndex + 1)


                Else
                    Return MyBase.ProcessCmdKey(msg, keyData)
                End If
            End If
        Else
            Return MyBase.ProcessCmdKey(msg, keyData)
        End If
    End Function

event datagrid
    Private Sub DataGridView_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView.CellEnter

        If e.ColumnIndex = 1 Then
            dgv1.BeginEdit(True)

        End If
    End Sub
Posted
Updated 12-Mar-12 12:56pm
v7
Comments
André Kraak 10-Mar-12 13:28pm    
Edited question:
Added pre tags
Formatted text/code

There is a pretty good answer right here on MSDN[^].
VB
Class MyDataGridView
    Inherits DataGridView

    Protected Overloads Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean
        If keyData = Keys.Enter Then
            MyBase.ProcessTabKey(Keys.Tab)
            Return True
        End If
        Return MyBase.ProcessDialogKey(keyData)
    End Function

    Protected Overloads Overrides Function ProcessDataGridViewKey(ByVal e As KeyEventArgs) As Boolean
        If e.KeyCode = Keys.Enter Then
            MyBase.ProcessTabKey(Keys.Tab)
            Return True
        End If
        Return MyBase.ProcessDataGridViewKey(e)
    End Function

End Class
Hope it helps :)
 
Share this answer
 
Comments
geice 12-Mar-12 9:39am    
is not what I need.
need that every time I press enter, it will enter that cell to cell and the column next to the finish line, not down.
Sander Rossel 12-Mar-12 19:00pm    
Actually that piece of code should just move to the next column if you hit enter.
In fact, enter will behave exactly the same as tab. That is what you want, right?
The code works fine for me...
I use this code in my KeyDown event for the DatGridView:
VB
Private Sub dgvMyGrid_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgvMyGrid.KeyDown
    Select Case e.KeyCode
        Case Keys.Enter
            e.Handled = True
            SendKeys.Send("{TAB}")
        End Select
    End Sub


I have my DataGridView.EditMode set to EditOnEnter. You may have to play with that setting to get it the way you want.
 
Share this answer
 
Comments
geice 13-Mar-12 14:19pm    
this code had already tested
geice 13-Mar-12 14:20pm    
and did not work
Kschuler 15-Mar-12 9:02am    
Why didn't it work? Did it give an error? I have encountered issues before when using SendKeys...some antivirus software can conflict with it. Have you tested to see if that is the reason it isn't working?
Nripendra Ojha 18-Jun-13 7:15am    
thanxx.. it's work for me.
Private Const WM_KEYDOWN = &H100

Protected Overrides Function ProcessKeyPreview(ByRef m As System.Windows.Forms.Message) As Boolean
Dim keyCode As Keys = CType(m.WParam.ToInt32() And Keys.KeyCode, Keys)
If m.Msg = WM_KEYDOWN And keyCode = Keys.Enter Then
SendKeys.Send("{TAB}")
Return True
End If
Return MyBase.ProcessKeyPreview(m)
End Function
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900