@ankit goel , Welcome to Microsoft Q&A, based on my test, I reproduced your problem. If you want to move to the next column same row, I recommend that you don't use the Enter or Tab to move the cell. Because you have defined it in the keydown event.
I chose F1 key to make it worked and here is a code example you could refer to.
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
int iColumn = dataGridView1.CurrentCell.ColumnIndex;
int iRow = dataGridView1.CurrentCell.RowIndex;
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Right || e.KeyCode == Keys.Tab)
{
//to check if the pointer reach last column
if (iColumn == dataGridView1.ColumnCount - 1)
{
//executing when line ends
if (dataGridView1.RowCount > (iRow + 1))
{
dataGridView1.CurrentCell = dataGridView1[0, iRow + 1];
}
// fires when you reach last cell of last row and last column
else
{
dataGridView1.Rows.Add();
dataGridView1.CurrentCell = dataGridView1[0, iRow + 1];
}
}
else
{
dataGridView1.CurrentCell = dataGridView1[iColumn + 1, iRow];
}
}
if (e.KeyCode == Keys.Down)
{
int c = dataGridView1.CurrentCell.ColumnIndex;
int r = dataGridView1.CurrentCell.RowIndex;
if (r < dataGridView1.Rows.Count - 1) //check for index out of range
dataGridView1.CurrentCell = dataGridView1[c, r + 1];
}
if (e.KeyCode == Keys.Up)
{
int c = dataGridView1.CurrentCell.ColumnIndex;
int r = dataGridView1.CurrentCell.RowIndex;
if (r > 0) //check for index out of range
dataGridView1.CurrentCell = dataGridView1[c, r - 1];
}
if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Left)
{
//to check if you are in first cell of any line
if (iColumn == 0)
{
//to check if you are in first cell of fist line
if (iRow == 0)
{
MessageBox.Show("you reached first cell");
}
else
{
dataGridView1.CurrentCell = dataGridView1[dataGridView1.ColumnCount - 1, iRow - 1];
}
}
else
{
dataGridView1.CurrentCell = dataGridView1[iColumn - 1, iRow];
}
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.F1)
{
if (dataGridView1.CurrentRow != null)
dataGridView1.CurrentCell =
dataGridView1
.Rows[dataGridView1.CurrentRow.Index]
.Cells[dataGridView1.CurrentCell.ColumnIndex + 1];
}
return base.ProcessCmdKey(ref msg, keyData);
}
Updated example:
public class NewDataGridView:DataGridView
{
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||key==Keys.Right)
{
Console.WriteLine("in edit");
return this.ProcessTabKey(keyData);
}
return base.ProcessDialogKey(keyData);
}
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
// Handle the ENTER key as if it were a RIGHT ARROW key.
if (e.KeyCode == Keys.Enter)
{
Console.WriteLine("no edit");
return this.ProcessTabKey(e.KeyData);
}
return base.ProcessDataGridViewKey(e);
}
}
After you edit the cells, you could use F1 key to move cells to the same row next column.
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.