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.
Nice tip, but i encountered a little problem, yet when i edit the cell and press enter button the focus goes down(cell down).
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?
Very nice. This is exactly what I was looking for — it works! Joel, thank you.
But where is the RecordSelected() function
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.
can you explain the “RecordSelected()” method?
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:
Thanks a lot. It actually caters to my problem with the right solution 🙂
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?
Ok I just found it. I have to use the PreviewKeyDown event instead of KeyDown. Works perfectly.
Hi Joel,
This was a nice tip!
Saved me some time – thanks for sharing!
/Dig
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
I haven’t done WinForms for a while, but my first guess would be that Hittesting is somehow disabled.
THANK YOU! The other methods on the web were completely useless!
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.
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
Great. Works perfect for me.
thanks for sharing…It saved my time. 🙂
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.
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
PreviewKeyDown did it for me. Thanks!
Grand merci, pour gérer KeyEnter c’est LA solution.
What In the case CellEndEdit Event , it is not Working
?
What In the Case of cellEndEdit Event..?
Thanks for this, I had worked out keydown part but needed your help to sort out the kryup – select record.
Thanks again
Paulo
Thank you very much.