Tab key work as an Enter key in Datagridview

To make the Tab key work as Enter key, we have to inherit DataGridView controls and override two methods ProcessDataGridViewKey and ProcessDialogKey. In both the methods if the user has click Tab then we have to ProcessEnterKey of Datagridview.

VB Code

Public Class NewDataGridView
Inherits DataGridViewProtected Overrides Function ProcessDataGridViewKey(ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
If e.KeyCode = Keys.Tab Then
Me.ProcessEnterKey(e.KeyData)
Return True
End If
Return MyBase.ProcessDataGridViewKey(e)
End Function


Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
If keyData = Keys.Tab Then
Me.ProcessEnterKey(keyData)
Return True
End If
Return MyBase.ProcessDialogKey(keyData)
End Function
End Class

C# Code
protected override bool ProcessDataGridViewKey(System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
this.ProcessEnterKey(e.KeyData);
}
return base.ProcessDataGridViewKey(e);
}


protected override bool ProcessDialogKey(System.Windows.Forms.Keys keyData)
{
Keys key = (keyData & Keys.KeyCode);
if (key == Keys.Tab)
{
this.ProcessEnterKey(keyData);
}return base.ProcessDialogKey(keyData);
}

Advertisements

About Asif
Hello Friends… I have done my Bachelor of Engineering in Information Technology from Gujarat University. I have 1.7 years experience of web and windows based application development.. My basic idea to start a blog is to store all important link which help me in my application and access to every where. Also this information is help others who want to makes their application powerful. I am interested in .net framework (1.0,2.0,3.0,3.5 and more). This blog contain also many information related to SQL, Custom Controls in C#.net Please share your ideas and any query if you have...

6 Responses to Tab key work as an Enter key in Datagridview

  1. Gicci says:

    I do not believe I have seen this described in such an informative way before. You really have clarified this for me. Thank you!

  2. kousik says:

    Hi,

    I want to focus a particular cell when press enter in that cell again press i need default behavior of enter kay.

    Thanks.,
    Kousik

    • Asif says:

      Hi Kousik.

      If you want to edit any cell in DataGridView then press F2.

      As per your information you want it on Enter key.
      when you set enter key as right then tough but you can check your cell in ProcessDialogKey like grdview.row[currentrow].cell[currentcell] and if match then simply return.

      This might help you.
      Thanks

  3. lakhan says:

    my complete problem is that…….
    i have one table ..in sql2003..and columns like…..
    setno
    date

    at page load event …
    code for binddata in data gridview..

    at run time it show me like

    setno date day
    1 31/4/2011 friday

    //31/4/2011 is last financial year date which already save in table

    now user edit this date enter another date which should be less than this date
    let he enter 1//1/2010 and press enter key
    at first enter prees datagridview should like this
    setno date day
    1 1/1/2010 Monday
    2 8/1/2010 Monday

    and now at 2nd time as he press enterkey gridview should like ..
    1 1/1/2010 Monday
    2 8/1/2010 Monday
    3 15/1/2010 Monday
    and so on …but not greater than financial year date

    i think now u can understand my problem ….if u can help me than please please help me …….

    thanks in both the cases
    😦 lakhan

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: