3

I have written the Below code for "Go to next cell when user press enter" but the code is not working and I am not able to find the error.

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Enter)
        {
            int col = dataGridView1.CurrentCell.ColumnIndex;
            int row = dataGridView1.CurrentCell.RowIndex;

            if (col < dataGridView1.ColumnCount - 1)
            {
                col++;
            }
            else
            {
                col = 1;
                row++;
            }

            if (row == dataGridView1.RowCount)
            {
                dataGridView1.Rows.Add();
                dataGridView1.CurrentCell = dataGridView1[col, row];

                e.Handled = true;
            }
        }
    }
  • 2
    `but the code is not working` what exactly is happening with this code? Are you getting error? What exact behaviour you are getting ? – Chetan Jun 20 '18 at 06:13
  • 1
    Possible duplicate of [DataGridView keydown event not working in C#](https://stackoverflow.com/q/4284370/9676724) – Ajay Gupta Jun 20 '18 at 06:17
  • 'Doesn't work' is __not a helpful__ problem description! Does it even enter the event? – TaW Jun 20 '18 at 07:07
  • The code is fine I think, but when I run the programme and press enter in a cell the cursor doesn't move into next cell its moving only with Tab as default. – Ajeet Kumar Singh Jun 20 '18 at 07:44
  • @AjeetKumarSingh Do you want to move to the next cell (to the right) or to the next line? What you said above and the title don't match up – JayV Jun 20 '18 at 08:20
  • I want to go to the next cell(right) when i press Enter and if I reach to last cell in current row then new row should we start – Ajeet Kumar Singh Jun 20 '18 at 09:35

2 Answers2

0

In the past I have found that the best way to implement this sort of behaviour is to create a Custom Control that inherits from the DataGridView and override the ProcessCmdKey function.

public class MyDataGridViewControl : DataGridView
{
    protected override Boolean ProcessCmdKey(ref Message msg, Keys keyData)
    {
        Boolean handled = false;

        if ((keyData == Keys.Enter || keyData == Keys.Return))
        {
            handled = NavigateToNextCell();
        }

        if (!handled)
        {
            handled = base.ProcessCmdKey(ref msg, keyData);
        }

        return handled;
    }

    private Boolean NavigateToNextCell()
    {
        Boolean retVal = false;

        if (CurrentCell != null)
        {
            Int32 columnIndex = CurrentCell.ColumnIndex;
            Int32 rowIndex = CurrentCell.RowIndex;

            DataGridViewCell targetCell = null;

            do
            {
                if (columnIndex >= Columns.Count - 1)
                {
                    // Move to the start of the next row
                    columnIndex = 0;
                    rowIndex = rowIndex + 1;
                }
                else
                {
                    // Move to the next cell on the right
                    columnIndex = columnIndex + 1;
                }

                if (rowIndex >= RowCount)
                {
                    break;
                }
                else
                {
                    targetCell = this[columnIndex, rowIndex];
                }
            } while (targetCell.Visible == false);


            if (targetCell != null)
            {
                CurrentCell = targetCell;
            }

            retVal = true;
        }

        return retVal;
    }
}
JayV
  • 3,238
  • 2
  • 9
  • 14
0

I have solved the problem. now its working fine.....

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Enter)
        {
            int row = dataGridView1.CurrentCell.RowIndex;
            int colIndex = dataGridView1.CurrentCell.ColumnIndex;

            if (colIndex < dataGridView1.Columns.Count - 1)
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[row].Cells[colIndex + 1];
                dataGridView1.Focus();
            }
            else if (colIndex == dataGridView1.Columns.Count - 1)
            {
                dataGridView1.Rows.Add(1);
                dataGridView1.CurrentCell = dataGridView1.Rows[row].Cells[0];
                dataGridView1.Focus();
            }

        }