Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7592
  • Last Modified:

How to get DatagridView to perform TAB when ENTER key is hit (during edit)?

The following code works perfectly in the KeyDown event sub belonging to my datagridview (when not in edit mode):

        If e.KeyCode = Keys.Enter Then
            SendKeys.Send("{TAB}")
            e.Handled = True
        End If

This forces a TAB action when the ENTER key is hit.  So far so good.

The problem is when the active cell is in the middle of an edit and the ENTER key is hit.  In this case, the ENTER key performs as usual (sending focus to the cell below instead of to the right like I want).  Turns out that when a cell is in edit mode the keydown subroutine never even fires.  Thanks in advance.
0
David L. Hansen
Asked:
David L. Hansen
  • 6
  • 3
2 Solutions
 
CodeCruiserCommented:
Try handling the EditingControlShowing event of the grid and subscribe to the keydown event of textbox that gets added to cell in edit mode. Then you can add code to transform Enter to Tab.

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.editingcontrolshowing.aspx
0
 
David L. HansenProgrammer AnalystAuthor Commented:
Cruiser, thanks!

This works with every alpha-numeric key except with the ENTER key...weird huh?

    'This allows the user to hit the enter key while editing a value and have it TAB instead of moving to the next row
    Private Sub dgvCancelled_TextBoxEditing(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            SendKeys.Send("{TAB}")
            e.Handled = True
        End If
    End Sub

    'This allows the user to hit the enter key while editing a value and have it TAB instead of moving to the next row
    Private Sub dgvCancelled_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvCancelled.EditingControlShowing
        Dim dgv As DataGridView = DirectCast(sender, DataGridView)
        Dim myTextBox As TextBox = DirectCast(dgv.EditingControl, System.Windows.Forms.TextBox)
        AddHandler myTextBox.KeyDown, AddressOf dgvCancelled_TextBoxEditing
    End Sub

Open in new window

0
 
CodeCruiserCommented:
If you step through the code, what does e.Keycode come back as?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
David L. HansenProgrammer AnalystAuthor Commented:
It simply never enters the subroutine.  If I type "a" or "z" or "1" or "0" no problem, the breakpoint is hit (at the IF line) however, if I type ENTER, the breakpoint is never hit (it doesn't actually fire that event I believe).   I've tried both ENTER keys.  Although the focus does change to the cell just below (as expected by the grid's default nature).  Not sure why.

Can you replicate it?
0
 
David L. HansenProgrammer AnalystAuthor Commented:
It must be firing a different event.
0
 
David L. HansenProgrammer AnalystAuthor Commented:
I've confirmed that the textbox's "validating" event fires on ENTER.
0
 
CodeCruiserCommented:
0
 
David L. HansenProgrammer AnalystAuthor Commented:
Excellent.  It works!  Thanks a ton Cruiser.

For the benefit of those reading this later...I chose the following solution out of the suggested links:
Public Class DataGridView2
    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

Open in new window

After creating this DataGridView2 class, I recompiled.  Then I deleted my original DataGridView on the form.  Doing so has a strange side-effect; it removes all of the "Handles" keywords in the code related to the original DataGridView.  Anticipating this, I then added the new custom DataGridView2 to the form and then renamed it to the same original name of the grid.  Then I searched through the code and restored the "Handles" keywords back to each subroutine affected.
0
 
David L. HansenProgrammer AnalystAuthor Commented:
Thanks! :-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now