Taming your WinForms Grid–Special Key Behaviors

DevExpress Data Blog
08 September 2011

Yes, I know, I am the reporting guy. I’ll have you know, however, that I was also a DevExpress customer starting all the way back in 2006! I recently looked into our fancy issue tracking application and saw an entry on the 3rd of July 2006. With that in mind I want to come out an categorically state that I am a WinForms fan boi (and have been for quite some time). With that in mind, I recently got an email asking something like this:

I want the grid to go to the column directly below the current when I hit enter. I also want the text to be edit-ready as well. How do I do this?

So I dug in. Turns out it isn’t too hard! The first thing is to handle the KeyUp event on the GridView:

private void gridViewMain_KeyUp(object sender, KeyEventArgs e)
    if (e.KeyCode == Keys.Enter)
        var handle = gridViewMain.FocusedRowHandle;
        if (handle > -1)
            var col = gridViewMain.FocusedColumn;
            gridViewMain.FocusedRowHandle = ++handle % gridViewMain.RowCount;
            gridViewMain.FocusedColumn = col;

The gist:

  1. Get the focused row along with the currently focused column and save them
  2. Add one to the row handle and make this the the currently focused row (the % stuff makes it wrap to the top once it reaches the bottom)
  3. Set the focused column to the one we stored
  4. Show the editor.

Done! Here is what it looks like:


The skip you see in the animation is due to the looping and not the code Winking smile

As always, if there are any comments and/or questions, feel free to get a hold of me!

Seth Juarez
Email: sethj@devexpress.com
Twitter: @SethJuarez

Want The Best Reporting Tool Ever?

Get The No-Compromise Reporting Tool for WinForms, ASP.NET, Silverlight and WPF! - Native integration with DevExpress WinForms and ASP.NET Controls, unequalled design-time productivity, industrial-grade features. Try a fully-functional version of DXperience for free now: http://www.devexpress.com/Downloads/NET/

Let us know what you think of our Reporting Suite by rating it in the VS Gallery!

Follow SethJuarez on Twitter

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.