VS 2008 How do I create an On-KeyDown event for a datagridview cell?-VBForums
Results 1 to 4 of 4

Thread: How do I create an On-KeyDown event for a datagridview cell?

  1. #1

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Question How do I create an On-KeyDown event for a datagridview cell?

    I'm using an on key down event for my datagridviewer that fires every time a key is pressed while the datagridviewer is selected(Focused).
    I want the same thing to happen while in a cell, but there is no event for key down in a cell.

    How do I code one?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,653

    Re: How do I create an On-KeyDown event for a datagridview cell?

    The way the DataGridView works is that the contents of each cell is simply drawn on until you actually start editing a cell. At that point a control of the appropriate type is created if one doesn't already exists and it is embedded in the cell. At that point, you have to handle events of that control rather than of the grid. To do that, you handle the EditingControlShowing event of the grid, get a reference to the editing control and use AddHandler to attach its event(s) to the appropriate handler(s).

    One important point is that, if that's all you do, you'll end up handling the same event multiple times because you'll keep adding the same handler over and over. For that reason, you should precede AddHandler with RemoveHandler. That will have no effect if there's no existing handler, otherwise it will remove the existing handler before adding the new one. E.g.
    vb.net Code:
    1. Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, _
    2.                                                 ByVal e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    3.     'Make sure we are in the first column.
    4.     If Me.DataGridView1.CurrentCellAddress.X = 0 Then
    5.         With e.Control
    6.             'Remove the existing handler if there is one.
    7.             RemoveHandler .TextChanged, AddressOf TextBox_TextChanged
    8.  
    9.             'Add a new handler.
    10.             AddHandler .TextChanged, AddressOf TextBox_TextChanged
    11.         End With
    12.     End If
    13. End Sub
    14.  
    15. Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    16.     '...
    17. End Sub

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Re: How do I create an On-KeyDown event for a datagridview cell?

    Oky I see where youre going with this. I have a certain code that works for my dgv-keyDown. How do I implement it in the code you posted above?

    vb Code:
    1. Private Sub DataGridView1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
    2.             Select Case e.KeyCode
    3.             Case Keys.Return
    4.                 e.SuppressKeyPress = True
    5.                 SendKeys.Send("{TAB}")
    6.         End Select
    7.     End Sub

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,653

    Re: How do I create an On-KeyDown event for a datagridview cell?

    So what efforts have you made so far? I've shown you how to get a reference to the editing control and how to add a handler for an event of that control. All you have to do is pick the appropriate event and write a handler for it. You've already got an event handler because you wrote one for the griod, so you don't even have to do that part.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.