WinForms DataGridView: Prevent Enter from moving to the next row and move focus to another column in the same row

Shahmeer .R 0 Reputation points
2026-08-06T23:21:03.6266667+00:00

I'm using a WinForms DataGridView for entering sales invoice items.

Suppose my grid looks like this:

Row Qty Net Rate
1 5 ...
-------- -------- --------
1 5 ...
2 6 ...
3 (new empty row)

If I edit Row 2 and change the quantity from 6 to 12, then press Enter, the DataGridView performs its default behavior and moves the current cell to Row 3 in the Qty column.

What I want instead is:

Finish editing the quantity in the current row.

Run my calculations (amount, discount, profit, etc.).

Keep the focus on the same row.

Move the current cell to the NetRate column of Row 2.

In other words, I want to override the default Enter key navigation so that pressing Enter in the Qty column does not move to the next row, but instead moves to another column in the current row.

What is the recommended way to achieve this in WinForms? Should I handle KeyDown, ProcessCmdKey, CellEndEdit, or another event?

Any example would be appreciated.

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


3 answers

Sort by: Most helpful
  1. Senthil kumar 1,730 Reputation points
    2026-08-07T04:44:48.85+00:00

    Hi @Shahmeer .R

    from datagridview uncheck enable adding options so no new row will not add. when edit row add event not perform.

    User's image

    Thanks.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

  2. Danny Nguyen (WICLOUD CORPORATION) 8,020 Reputation points Microsoft External Staff Moderator
    2026-08-07T02:48:56.1933333+00:00

    Hello @Shahmeer .R ,

    To override the default Enter key navigation in a DataGridView so that it stays on the same row and moves to a specific column, the recommended approach is to override the ProcessCmdKey method at the Form level. This allows you to intercept the Enter key press before the DataGridView handles it and moves the selection down.

    Here is an example demonstrating how you can achieve this:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        // Check if the Enter key was pressed and the focus is in the "Qty" column
        if (keyData == Keys.Enter && 
            dataGridView1.CurrentCell != null && 
            dataGridView1.CurrentCell.OwningColumn?.Name == "Qty")
        {
            // 1. Finish editing the current cell so its value is committed
            dataGridView1.EndEdit();
    
            // 2. Perform your calculations here 
            // (e.g., updating amount, discount, profit based on the new Qty)
    
            // 3. Move the focus to the "NetRate" column on the same row
            int currentRowIndex = dataGridView1.CurrentCell.RowIndex;
            dataGridView1.CurrentCell = dataGridView1.Rows[currentRowIndex].Cells["NetRate"];
    
            // 4. Return true to consume the key press, preventing the default behavior
            return true; 
        }
    
        // For all other keys and columns, retain the default behavior
        return base.ProcessCmdKey(ref msg, keyData);
    }
    

    In short:

    • ProcessCmdKey catches the key press early.
    • EndEdit() ensures that whatever value the user just typed into the cell is committed to the underlying cell/data source before your calculations run.
    • Returning true tells the framework that the keystroke has been handled, which specifically prevents the grids built-in behavior of moving to the next row.

    Let me know if it works for you. I would greatly appreciate it if you could follow this guidance or provide feedback. Thank you.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

  3. Danny Nguyen (WICLOUD CORPORATION) 8,020 Reputation points Microsoft External Staff Moderator
    2026-08-07T02:24:31.79+00:00

    I am currently handling the issue and will provide an update soon. Thank you for your patience.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.