Click here to Skip to main content
15,041,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am working on an editable grid where I can add, edit delete records.

But the problem is when I click Enter key, the selected cell whose editing is enabled the SelectionChanged event gets triggered.

And because of this, the selected row gets changed to the next line.

I added CellEndEdit event to make selected cell remains selected but the problem is the SelectionChanged event trigger after CellEndEdit event.

C#
private void DgvMaster_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    dgvMaster.CurrentCell = dgvMaster[dgvMaster.CurrentCell.ColumnIndex, dgvMaster.CurrentCell.RowIndex];
}


So, how can I prevent the SelectionChanged event from firing or at least return in this case?

EditCell >> Click Enter >> End Edit >> Cell Remain same.

And also, is there any way to check whether the Cell Edit is enabled or disbaled.

What I have tried:

C#
//My keydown event to switch to next cell and enable edit

private void DgvMaster_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;
            int iColumn = dgvMaster.CurrentCell.ColumnIndex;
            int iRow = dgvMaster.CurrentCell.RowIndex;
            if (iColumn == dgvMaster.ColumnCount - 1)
            {
                if (dgvMaster.RowCount > (iRow + 1))
                {
                    dgvMaster.CurrentCell = dgvMaster[1, iRow];
                }
                else
                {
                    dgvMaster.CurrentCell = dgvMaster[dgvMaster.CurrentCell.ColumnIndex + 1, dgvMaster.CurrentCell.RowIndex];
                }
            }
            else
                dgvMaster.CurrentCell = dgvMaster[iColumn + 1, iRow];

            dgvMaster.BeginEdit(true);
        }
Posted
Updated 8-Jul-21 19:50pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900