Tab key work as an Enter key in Datagridview

Hi, this code at keydown and add blank row when you at last row and last column

private void DataGridView1_KeyDown(object sender, KeyEventArgs e)
{
//check for enter key
if (e.KeyCode == Keys.Enter)
{
DataGridView vw = (DataGridView)sender;
//check for last row
if (vw.CurrentCell.RowIndex == vw.Rows.Count - 1)
{
//if it's not the last cell, move to the next one
if (vw.CurrentCell.ColumnIndex != vw.ColumnCount - 1)
{
vw.CurrentCell = vw.Rows[vw.CurrentCell.RowIndex].Cells[vw.CurrentCell.ColumnIndex + 1];
}
else // if last cell then either enter new raw or move to first cell.
{
//move to first column in new row
vw.Rows.Add(1);
vw.Refresh();
SendKeys.Send("{HOME}");
}
}
else
{
//if it's the last cell, move to the next row, first cell
if (vw.CurrentCell.ColumnIndex == vw.ColumnCount - 1)
{
vw.CurrentCell = vw.Rows[vw.CurrentCell.RowIndex + 1].Cells[0];
}
else
{
//move to the next cell on the current row
vw.CurrentCell = vw.Rows[vw.CurrentCell.RowIndex].Cells[vw.CurrentCell.ColumnIndex + 1];
}
}
//mark the keycode as handled
e.Handled = true;
}
}

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...

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

  1. Amey Gandhi says:

    Hey Asif,
    Thank you for such a descriptive coding.
    I was searching such code for very long time but meny people tells to overrides the event.
    Thanks Really Thanks.
    Regards
    Amey Gandhi

  2. Amey Gandhi says:

    Hi Asif
    I have one problem,
    In this code when you write something in cell and then press enter it goes below the current cell. but after writing i want to focus to next cell.
    I will be thankful if you help.
    Thanks in advance
    Amey Gandhi

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: