Developing For .NET

Real World .NET Methods, Tricks, and Examples

DataGridView control and the Enter key.



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.








16 comments    

16 Comments so far

  1. rafael July 18th, 2008 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. Joel July 24th, 2008 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 29th, 2008 8:15 pm

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

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

    But where is the RecordSelected() function

  5. Joel October 20th, 2008 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.

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

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

  7. Tom September 22nd, 2009 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 22nd, 2009 5:45 pm

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

  9. Digvijay October 23rd, 2009 6:17 am

    Hi Joel,

    This was a nice tip!

    Saved me some time – thanks for sharing!

    /Dig

  10. olibara January 16th, 2010 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. Joel January 18th, 2010 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 6th, 2010 1:00 am

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

  13. borg April 28th, 2010 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. bitlink May 16th, 2010 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 17th, 2010 4:32 am

    Great. Works perfect for me.

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

    thanks for sharing…It saved my time. :)

Leave a reply

Spam Protection by WP-SpamFree