0
votes

Event : DataGridView1_CellEndEdit

    DataGridView1.CurrentCell = DataGridView1.Rows(e.RowIndex).Cells(2)
    DataGridView1.BeginEdit(True)

I want to focus cell row 0 column 2 for edit

I edit cell and enter . datagridview focus row 0 column 2 .
And i type it not focus row 0 column 2
it focus row 1 colum 2

it not work by this code.

Sorry.my english isn’t that good. Thank you.

I edit cell and enter:

enter image description here

datagridview focus row 0 column 2:

enter image description here

it focus row 1 colum 2:

enter image description here

1

1 Answers

0
votes

what i understand from your question that you want to move to next cell of datagridview when you press Enter key. Try this

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        try
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                int iColumn = dataGridView1.CurrentCell.ColumnIndex;
                int iRow = dataGridView1.CurrentCell.RowIndex;
                if (iColumn == dataGridView1.Columns.Count - 1)
                    dataGridView1.CurrentCell = dataGridView1[0, iRow + 1];
                else
                    dataGridView1.CurrentCell = dataGridView1[iColumn + 1, iRow];
            }
        }
        catch { }
    }

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == dataGridView1.Columns.Count - 1)
        {
            dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentRow.Index + 1].Cells[0];
        }
        else
        {
            SendKeys.Send("{UP}");
            SendKeys.Send("{left}");
        }

    }