Results 1 to 10 of 10

Thread: [2005] DataGridView - Enter Key

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    [2005] DataGridView - Enter Key

    Is there a way to control how a DataGridView responds to an Enter keypress?

    Right now, if I edit a cell and press the Enter key, focus goes to the next row. I want to have a choice of staying on the same cell, moving to the next row in the same column, or moving to the next column in the same row.

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] DataGridView - Enter Key

    Wondering this myself recently and found a good post about this here:
    http://forums.microsoft.com/MSDN/Sho...57055&SiteID=1

    Hope that helps.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: [2005] DataGridView - Enter Key

    I'm still looking for an answer to this one. I want to edit a cell, press Enter, and stay in the same cell - not move to the next row. Sometimes I'll want to move to the next column on the same row.

    I suppose I could use SendKeys (does that still exist in VB2005) and send an UpArrow if I'm not on the last row already. But surely there's a better way.

  4. #4
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] DataGridView - Enter Key

    I can't say that I've really played around with this so I'm sure one of the more experienced programmers could help you further, but the post above didn't give any help? There is this code in there: "Here is the code you need to make the Enter key move the focus to the right:"

    I'm sure you'll find your solution. Good luck.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: [2005] DataGridView - Enter Key

    That code looks like C or some other language I don't know. Will it be the same in VB?

  6. #6
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] DataGridView - Enter Key

    It's C#. Convert here and give the code a shot!
    http://www.developerfusion.co.uk/uti...sharptovb.aspx

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: [2005] DataGridView - Enter Key

    Cool. Didn't know about the converter.

    Will I need to delete my current DataGridView controls from the form and replace them with derived ones or will adding this code to a class do the same thing, with my current DataGridViews?
    vb Code:
    1. Public Class dgv
    2.     Inherits DataGridView
    3.     Protected Overloads Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean
    4.         Dim key As Keys = (keyData And Keys.KeyCode)
    5.         If key = Keys.Enter Then
    6.             Return Me.ProcessRightKey(keyData)
    7.         End If
    8.         Return MyBase.ProcessDialogKey(keyData)
    9.     End Function
    10.     Protected Overloads Overrides Function ProcessDataGridViewKey(ByVal e As KeyEventArgs) As Boolean
    11.         If e.KeyCode = Keys.Enter Then
    12.             Return Me.ProcessRightKey(e.KeyData)
    13.         End If
    14.         Return MyBase.ProcessDataGridViewKey(e)
    15.     End Function
    16. End Class

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: [2005] DataGridView - Enter Key

    I tried something a little simpler (I think). I used SendKeys to send an UpArrow to the grid (except on the bottom row) in the EndEdit event.

    The only problem I see is that if you're editing a cell, and then leave the cell by pressing an arrow key (like RightArrow) the UpArrow is still sent. Is there a way to trap this and not send the UpArrow when the EndEdit was fired by another ArrowKey? Maybe a .LastKeyPressed property or something similar?

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: [2005] DataGridView - Enter Key

    I still don't understand what to do with this code.

    What I want is to have a choice of how the Enter key affects the current row and current column. I want 3 choices for when Enter is pressed after editing a cell.

    1. Stay in the same cell.
    2. Stay in the same column & go to the next row (if it exists).
    3. Stay in the same row & go to the next column (if it exists).

    Can anyone help with that?

    The grid would respond based on the value of CheckBoxes.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: [2005] DataGridView - Enter Key

    Nobody? I tried the code in the suggestion above and it doesn't really work for my situation.

Posting Permissions

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



Click Here to Expand Forum to Full Width