Overriding DataGridView ProcessDataGridViewKey and ProcessDialogKey

The ENTER key behavior of C# application can be customized by overriding ProcessCmdKeyIt will affect all controls in the form if you more than one control.

Our objective is to facilitate the Enter key action for DataGridView only. This can be done with inheriting and customizing the DataGridView.

What we going to do.

  1. Add C# Windows Form Application Project
  2. Add DataGridView control and also add some columns
  3. Then we inherit a new class
  4. Replacing the designer class declaration

Customizing the DataGridView Class

  • Inheriting a new class from DataGridView class
  • Overriding ProcessDialogKey which handle dialog characters, such as TAB, RETURN, ESCAPE, and arrow keys
  • Overriding Processes keys which is used for navigating in the DataGridView

 The new DataGridView

public class CustomDataGridView : DataGridView
Window = System.Security.Permissions.UIPermissionWindow.AllWindows)]

protected override bool ProcessDialogKey(Keys keyData)
// Extract the key code from the key value.
Keys key = (keyData & Keys.KeyCode);
// Handle the ENTER key as if it were a RIGHT ARROW key.
if (key == Keys.Enter)
return this.ProcessRightKey(keyData);
return base.ProcessDialogKey(keyData);
System.Security.Permissions.SecurityAction.LinkDemand, Flags =
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
// Handle the ENTER key as if it were a RIGHT ARROW key.
if (e.KeyCode == Keys.Enter)
if (base.CurrentCell.ColumnIndex == 9)
int cri = base.CurrentRow.Index;
base.CurrentCell = base.Rows[cri + 1].Cells[0];
catch(System.ArgumentOutOfRangeException error)
base.CurrentCell = base.Rows[cri + 1].Cells[0];
return ProcessRightKey(e.KeyData);

return base.ProcessDataGridViewKey(e);

Replacing the Form.DataGridView

Now we are ready to replace the system defined DataGridView with customized one.
Find the YourFom.Designer.cs file

Replace default object
this.dataGridView1 = new System.Windows.Forms.DataGridView();
private System.Windows.Forms.DataGridView dataGridView1
private CustomDataGridView dataGridView1;
this.dataGridView1 = new CustomDataGridView ();

Let’s Run and see what is happening. Enter key will take you next column and when you reach 10th column it will add a new row only if there are no more rows, which we made possible with catch section.

I hope this will work fine as mine.

Source : Codehat3

Published by


A developer,teacher and a blogger obsessed with Python,Dart and open source world from India

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.