Home > .NET 2.0, C# 2.0 > DataGridView control and the Enter key.

DataGridView control and the Enter key.

July 16, 2007

Because of the type of data processing applications I develop, I try to design them in such a fashion that the mouse is largely unnecessary. This means a lot of function keys, hidden context menus, tab order, and Focus() manipulation. For the most part, with just a little forethought and effort, you can make an app very responsive to a “heads down” data processor.

Here is one example I ran in to today while working on a fairly typical database management type utility. The user performs a database search and is presented with a list of records that match the query. These records are listed in a DataGridView control and immediately after the search is complete, Focus() is transferred to the DataGridView control. Now the user can easily use the Up and Down arrow keys to navigate the Grid, but record selection becomes a little tricky.

Intuitively, the user should be able to highlight the desired record and press the Enter key on the keyboard to select the record. Unfortunately, the default behavior of the DataGridView control does not function this way: when the Grid has the focus, pressing the Enter key will advance the highlighted cell to the one immediately below it, just like pressing the down arrow key. This is not intuitive and in my case is down right unacceptable.

To bypass this behavior, we need to monitor the KeyDown event, and if the Enter key has been pressed, instruct the Grid control to ignore its default behavior. We do so by setting the KeyEventArgs Handled property to true:

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyData == Keys.Enter)
    {
        e.Handled = true;
    }
}

Now the highlighted cell will not change when the Enter key is pressed. Then in the KeyUp event, we can instruct the Grid to perform whatever task we wish to process the record:

private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyData == Keys.Enter)
    {
        this.RecordSelected();
    }
}

And now we have a DataGridView control that handles the Enter key in a more intuitive fashion.

Advertisements
Categories: .NET 2.0, C# 2.0
  1. July 18, 2008 at 11:02 am

    Nice tip, but i encountered a little problem, yet when i edit the cell and press enter button the focus goes down(cell down).

  2. July 24, 2008 at 8:56 am

    That was the original problem I had, and the above code was the solution. Could you post some of what you have for comparison? Also, this was written for 2.0: what version are you coding in?

  3. Dave N
    August 29, 2008 at 8:15 pm

    Very nice. This is exactly what I was looking for — it works! Joel, thank you.

  4. Arif
    October 18, 2008 at 10:43 pm

    But where is the RecordSelected() function

  5. October 20, 2008 at 1:31 pm

    Hi Arif,

    The method call here represents whatever action you want to occur when the Enter key is pressed. Those implementation details are irrelevant to the particular task of getting the Enter key to function in the desired manner.

    In the case of my software, it was used to test whether or not a Tab in a TabControl existed for that record: if it did, the screen navigated to that Tab, if not a new tab was created and then opened. That behavior was defined in the “RecordSelected()” method.

    • al
      April 21, 2013 at 9:11 pm

      can you explain the “RecordSelected()” method?

      • April 22, 2013 at 6:20 am

        It’s whatever you want it to be, in this case a call to a method that selected the highlighted record. Check out this comment above:

  6. Tanu
    August 21, 2009 at 3:37 am

    Thanks a lot. It actually caters to my problem with the right solution 🙂

  7. Tom
    September 22, 2009 at 5:30 pm

    Hi,

    I have the same problem but I can’t get this solution to work. The KeyDown event is not called when I press the Enter key… only go on KeyUp. Does anyone knows why?

  8. Tom
    September 22, 2009 at 5:45 pm

    Ok I just found it. I have to use the PreviewKeyDown event instead of KeyDown. Works perfectly.

  9. October 23, 2009 at 6:17 am

    Hi Joel,

    This was a nice tip!

    Saved me some time – thanks for sharing!

    /Dig

  10. January 16, 2010 at 1:22 pm

    Hello
    Nice tip
    However I’ve just found that in one of My DGV no key event are working for key.Enter
    That DGV contains ComBoBox column : can it be the problem M

  11. January 18, 2010 at 11:05 am

    I haven’t done WinForms for a while, but my first guess would be that Hittesting is somehow disabled.

  12. Coty
    April 6, 2010 at 1:00 am

    THANK YOU! The other methods on the web were completely useless!

  13. borg
    April 28, 2010 at 6:09 am

    hi to all,

    The correct event is CellEndEdit, here you can decide where to place enter key after edit, and if not on edit mode keypress event should be process.

    hope it helps you.

  14. May 16, 2010 at 11:43 am

    Thanks, very useful

    Another interesting topic I found on gridviews (while searching this enter issue) was the right click – popup context menu. See solution:
    http://blog.bitlinkit.com/post/Right-Click-and-Auto-Row-Select-with-a-ContextMenuStrip.aspx

    thanks again for resolving my enter problem
    andy

  15. ashhar
    June 17, 2010 at 4:32 am

    Great. Works perfect for me.

  16. Nish
    June 23, 2010 at 9:20 am

    thanks for sharing…It saved my time. 🙂

  17. Gabriel
    October 21, 2010 at 9:09 am

    This I just tried but it didnt work with the currently latest Silverlight. Have to decrease the selectedIndex on my datagrid by each Enter keyUp.

    • October 21, 2010 at 10:02 am

      Hi Gabriel,

      This was an old WinForms post from 2007, I wouldn’t expect it to work in Silverlight.

      Having never done this before (I am not a fan of DataGrid), here are some things I would investigate:

      1) Add a property in your ViewModel for tracking the selected index and 2 way bind it to the DataGrid’s SelectedIndex property.

      2) Add a Command(s) to manipulate the selected index property in the ViewModel.

      3) Look into KeyBinding to handle the Enter key, or check out something like this:
      http://blogs.msdn.com/b/nickkramer/archive/2009/01/15/command-helper-classes-for-silverlight-wpf.aspx

  18. December 5, 2010 at 3:11 pm

    PreviewKeyDown did it for me. Thanks!

  19. Stef
    December 23, 2010 at 2:04 pm

    Grand merci, pour gérer KeyEnter c’est LA solution.

  20. Varinder
    August 23, 2011 at 2:10 am

    What In the case CellEndEdit Event , it is not Working
    ?

  21. Varinder
    August 23, 2011 at 2:19 am

    What In the Case of cellEndEdit Event..?

  22. Paulo
    September 19, 2011 at 3:27 pm

    Thanks for this, I had worked out keydown part but needed your help to sort out the kryup – select record.
    Thanks again
    Paulo

  23. Vincent Paukgyi
    September 28, 2012 at 11:12 pm

    Thank you very much.

  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: