Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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
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.
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.
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 10-Mar-12 8:23am
geice427
Edited 12-Mar-12 13:56pm
v7
Comments
André Kraak at 10-Mar-12 13:28pm
   
Edited question:
Added pre tags
Formatted text/code
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There is a pretty good answer right here on MSDN[^].
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 Smile | :)
  Permalink  
Comments
geice at 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.
Naerling at 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...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I use this code in my KeyDown event for the DatGridView:
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.
  Permalink  
Comments
geice at 13-Mar-12 14:19pm
   
this code had already tested
geice at 13-Mar-12 14:20pm
   
and did not work
Kschuler at 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 at 18-Jun-13 7:15am
   
thanxx.. it's work for me.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,142
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 15 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100