I am using this Code in dataGridView1_SelectionChanged
and dataGridView1_CellEndEdit
event. It works properly but when I press the enter Key firstly the edit control focus jumps on new row of selected column and then automatically goes back to the upper row and select the next cell. But I want to transfer directly to the next cell of the selected row.
For more details I attached a picture.
Please help me. I am trying a lot of logic for this.
Here my code
try
{
if (MouseButtons != 0) return;
if (celWasEndEdit != null && dataGridView1.CurrentCell != null)
{
// if we are currently in the next line of last edit cell
if (dataGridView1.CurrentCell.RowIndex == celWasEndEdit.RowIndex + 1 &&
dataGridView1.CurrentCell.ColumnIndex == celWasEndEdit.ColumnIndex)
{
int iColNew;
int iRowNew = 0;
if (celWasEndEdit.ColumnIndex >= dataGridView1.ColumnCount - 1)
{
iColNew = 0;
iRowNew = dataGridView1.CurrentCell.RowIndex;
}
//if we Edit the cell and press Enter the focus on the next cell
else
{
iColNew = celWasEndEdit.ColumnIndex + 1;
iRowNew = celWasEndEdit.RowIndex;
}
dataGridView1.CurrentCell = dataGridView1[iColNew, iRowNew];
}
}
celWasEndEdit = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Create an extended control derived from the DataGridView and override the ProcessDialogKey() method.
For additional information see here.
Test sources
User control: using System.Windows.Forms;
Test form:
Test form code-behind: