locked
DataGridView - KeyDown in EditMode RRS feed

  • Question

  • Using VB2008, I want to look at each character that is entered into a DataGridView cell and perform some actions when necessary. For example, the user enters a couple characters and then presses F3. The F3 function looks up data for the cell and uses the first characters as its starting point. It works great in a TextBox. However the behaviour in a DGV cell is different. When characters are entered into a DGV cell, the cell goes into edit mode. The KeyDown nor KeyPress events does not fire while the cell is in edit mode.

     

    How can I examine each keyPress value that is done in a DGV cell?


    Price Brattin, SQLServer MCP, Microsoft Dynamics SL Consultant
    Thursday, February 25, 2010 1:04 AM

Answers

  • Hi Price Brattin,

    When the cell is in edit mode the datagridview is  not getting the key press event,because DataGridView is one of the complex controls in .net,its cell is inherited from TextBox control. So when the cell get focused, it non bussiness of the datagridview itself,the datagridview is just a container,so your key press will not fire the keyPress event of the DataGridView.

     You can add a handler to the textboxes keypress in the editing control showing event. Make sure you remove the handler to the keypress event to prevent the event from firing multiple times.
        Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
            If DataGridView1.CurrentCell.ColumnIndex = 1 AndAlso TypeOf e.Control Is TextBox Then
                RemoveHandler DirectCast(e.Control, TextBox).KeyPress, AddressOf CellKeyPress
                AddHandler DirectCast(e.Control, TextBox).KeyPress, AddressOf CellKeyPress
            End If
        End Sub

        Private Sub CellKeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
            Debug.Print(e.KeyChar)
        End Sub

    Hope this helps!

    Best Regards,
    Helen Zhou

    • Marked as answer by Helen Zhou Wednesday, March 3, 2010 9:15 AM
    Wednesday, March 3, 2010 8:33 AM
  • Price,

    you can get the editing control and add your own handler for the key events to get the keys which are pressed and act accordingly

    you can add the code inside the editing control showing event to your own datagridview's editing control showing event and you can copy the entire keydown handler sub.  this will hook up your editng control to the handler.
     
    Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        Dim EditingTxtBox As TextBox = CType(e.Control, TextBox)
        AddHandler EditingTxtBox.KeyDown, AddressOf DataGridView1_EditingModeKeyDown
      End Sub
    
      Private Sub DataGridView1_EditingModeKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        MsgBox(e.KeyCode.ToString)
      End Sub
     
    FREE
    DEVELOPER TOOLS     CODE     PROJECTS

    DATABASE CODE GENERATOR
    DATABASE / GENERAL  APPLICATION TUTORIAL
    Upload Projects to share or get help on and post the generated links here in the forum
    www.srsoft.us
    • Marked as answer by Helen Zhou Wednesday, March 3, 2010 9:15 AM
    Thursday, February 25, 2010 3:22 AM

All replies

  • Price,

    you can get the editing control and add your own handler for the key events to get the keys which are pressed and act accordingly

    you can add the code inside the editing control showing event to your own datagridview's editing control showing event and you can copy the entire keydown handler sub.  this will hook up your editng control to the handler.
     
    Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        Dim EditingTxtBox As TextBox = CType(e.Control, TextBox)
        AddHandler EditingTxtBox.KeyDown, AddressOf DataGridView1_EditingModeKeyDown
      End Sub
    
      Private Sub DataGridView1_EditingModeKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        MsgBox(e.KeyCode.ToString)
      End Sub
     
    FREE
    DEVELOPER TOOLS     CODE     PROJECTS

    DATABASE CODE GENERATOR
    DATABASE / GENERAL  APPLICATION TUTORIAL
    Upload Projects to share or get help on and post the generated links here in the forum
    www.srsoft.us
    • Marked as answer by Helen Zhou Wednesday, March 3, 2010 9:15 AM
    Thursday, February 25, 2010 3:22 AM
  • Hi Price Brattin,

    When the cell is in edit mode the datagridview is  not getting the key press event,because DataGridView is one of the complex controls in .net,its cell is inherited from TextBox control. So when the cell get focused, it non bussiness of the datagridview itself,the datagridview is just a container,so your key press will not fire the keyPress event of the DataGridView.

     You can add a handler to the textboxes keypress in the editing control showing event. Make sure you remove the handler to the keypress event to prevent the event from firing multiple times.
        Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
            If DataGridView1.CurrentCell.ColumnIndex = 1 AndAlso TypeOf e.Control Is TextBox Then
                RemoveHandler DirectCast(e.Control, TextBox).KeyPress, AddressOf CellKeyPress
                AddHandler DirectCast(e.Control, TextBox).KeyPress, AddressOf CellKeyPress
            End If
        End Sub

        Private Sub CellKeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
            Debug.Print(e.KeyChar)
        End Sub

    Hope this helps!

    Best Regards,
    Helen Zhou

    • Marked as answer by Helen Zhou Wednesday, March 3, 2010 9:15 AM
    Wednesday, March 3, 2010 8:33 AM
  • Hi,
    I want to walk through a DataGridview horizontally by pressing ENTER. So I tried to use your Code when the Cell is in Editor-Mode...

    Unfortunately I cannot handle the ENTER- or RETURN- Button with it, all other Keys work. By Pressing ENTER it seems that the Events "KeyPress" or "KeyDown" do not happen. And the Fokus is set to the next Cell below 

    Can you please help me with it? I'm using VS2012. Here my Code:


    Public Class TableInput
        Inherits System.Windows.Forms.DataGridView


    ...

    ...

    Private Sub _EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles Me.EditingControlShowing
            If TypeOf e.Control Is TextBox Then
                RemoveHandler DirectCast(e.Control, TextBox).KeyDown, AddressOf CellKeyDown
                AddHandler DirectCast(e.Control, TextBox).KeyDown, AddressOf CellKeyDown
            End If
    End Sub

    Private Sub CellKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
            If e.KeyCode = Keys.Enter Then
                e.SuppressKeyPress = True
                Windows.Forms.SendKeys.Send("{TAB}")
            Else
                e.SuppressKeyPress = False
            End If
    End Sub

    End Class

    Thank you so much for any help...

    Tuesday, February 4, 2014 12:00 PM