DevExpress Data Blog

Taming your WinForms Grid–Special Key Behaviors

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;
            gridViewMain.ShowEditor();
        }
    }
}

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:

grid

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

Published Sep 08 2011, 02:11 PM by
Filed under: , , ,
Bookmark and Share

Comments

Ediz Sağlam

AspxGridView?

September 13, 2011 11:40 AM

Mehul Harry (DevExpress)

Ediz,

You can put the ASPxGridView into a multiple-edit mode:

www.devexpress.com/.../E324.aspx

You can also setup your own keyboard actions:

www.devexpress.com/.../E352.aspx

Thanks.

September 21, 2011 6:21 PM
LIVE CHAT

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2017 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners