The DataGridView is a terrific control built into .NET that provides a customizable table for entering and displaying data. If you provide the DataGridView in your software as a means for the user to enter multiple rows of data, you may wish to redefine the default behavior of the Enter key.
By default, when you press the Enter key in the DataGridView, the cursor moves to the cell in the same column immediately below the current cell (red arrow in the image below). But when entering multiple rows of data, a better response from the Enter key would be to move the cursor to the first cell in the next row (blue arrow).
To do this, you can derive a new class from the DataGridView:
public class Grid : DataGridView {
Then override the OnKeyUp protected method as follows:
protected override void OnKeyUp( KeyEventArgs e ) { if (e.KeyCode == Keys.Enter) { int currentRow = this.CurrentRow.Index; if (currentRow >= 0) this.CurrentCell = this.Rows[currentRow].Cells[0]; } base.OnKeyUp( e ); }
Of course, if you wish to provide this capability for an existing DataGridView, you can simply subscribe to the KeyUp event and execute the same code above in the event handler.
i have search this solution couple of hours. Am really happy to find this solution. thanks
me too ๐
thanx for the above mentioned code,
but just being a new programmer,
please clearify my doubt :
even if i derive a new class grid,
how can we use that class in our code,
what i mean is datagridview is a control,so
dont we have to work with the same object,say dtg1 ?
and when we will press enter key in dtg1,how overriden method from grid class will get called??
i know i might be asking a childish one,
but just want to clear my concept,please reply
Swaroop, there are two ways to use a derived DataGridView control:
1. You can add your derived DataGridView to the Toolbox, then drag and drop your derived object onto your Design view.
2. You can edit the designer-generated code directly and replace the DataGridView class name with your derived class name. Note that this must be done in two places: the object definition, and the statement that creates the object.
timm: i only made change in InitializeComponent()
I dont know where to find Statement that creates the object in designer code.
Thank you
@Boki – I think that would be enough, I also here and it worked fine.
Thank you everybody!
protected override void OnKeyDown( KeyEventArgs e )
{
if (e.KeyCode == Keys.Enter)
{
e.Keysupress = true; //suppress ENTER
SendKeys.Send(“{Tab}”); //Next column (or row)
}
base.OnKeyDown( e );
}
protected override void OnKeyDown( KeyEventArgs e )
{
if (e.KeyCode == Keys.Enter)
{
e.Keysupress = true; //suppress ENTER
SendKeys.Send(โ{Tab}โ); //Next column (or row)
}
base.OnKeyDown( e );
}
use upper coading for move the cursur to next colom
but enter the date and press enter key then cursur move next rows.then shoud be sam rows.
pease give coading for upper problums.
hi,
I need a similar solution.
when pressing the Enter key, I would like to “stay” at the same cell, but entering a new line (such as Ctrl+Enter does).
can you help?
Tomer.
I am really new to vb. I have the same problem as above but I use .net not C#. How do I convert this info for my use and where in the code do I put the code? My DataGridView is named Senior_dataDataGridView. I have tried many so called solutions but non have worked for me, probably because I don’t know where to put the code at and I have never had a .net solution. I you could help me I would really appreciate it.
Hi Bill,
I am a C# programmer I was looking for the same solution in C# but I found of VB.NET..
you can check it here..
http://www.daniweb.com/software-development/vbnet/threads/255372
This is tough. Iโm not calling you out though, I think it is everyone else out there that isnโt taking notice.
how to use the above code ,doi need to create the object if so where ,in event fire?
Nice its working good but it should go down and come back up how make that but its very good
Hi its working good but it should go down and come back up ….but i write some validations for every cell…validations false means i got error..how can i solve this issue…i want validations and also enter key…please help me anybody ….my id…
Thanks,for this solution
but i edit the cell,it doesn’t work,any suggestions?