How to move cursor to next row first cell when write item code and press enter ?

asked 2022-09-25T21:31:40.047+00:00
ahmed salah 3,126 Reputation points

I working on c# windows form data grid view I face issue

I can't move cursor to next row first cell

to more details

I have data grid view have item code as first cell on current row

I will write on item code 1 then press enter

after press enter i need to move cursor from current row to next row then will focus on first cell on next row Item code

currently code after write item code it go to last cell on current row
but not add new row and focus on first cell

what i try

      private void GridTrxInvF_KeyDown(object sender, KeyEventArgs e)  
        {  
            try  
            {  
            
                e.Handled = true;  
                e.SuppressKeyPress = true;  
                int iColumn = GridTrxInvF.CurrentCell.ColumnIndex;  
                int iRow = GridTrxInvF.CurrentCell.RowIndex;  
                if (iColumn == GridTrxInvF.Columns.Count - 1)  
                    GridTrxInvF.CurrentCell = GridTrxInvF[0, iRow + 1];  
                else  
                    GridTrxInvF.CurrentCell = GridTrxInvF[iColumn + 1, iRow];  
  
                SendKeys.Send("{UP}");  
                SendKeys.Send("{left}");  
                GridTrxInvF.CurrentCell = GridTrxInvF.Rows[iRow].Cells["TaxSet"];  
  
                GridTrxInvF.CurrentCell.Selected = true;  
  
                GridTrxInvF.BeginEdit(true);  
}  

244520-whati-needtodo.png

.NET Standard
.NET Standard
A formal specification of .NET APIs that are available on multiple .NET implementations.
483 questions
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,316 questions
C#
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.
6,930 questions
No comments
{count} votes

Accepted answer
  1. answered 2022-09-26T02:51:19.073+00:00
    Jack J Jun 19,046 Reputation points Microsoft Employee

    @ahmed salah , Welcome to Microsoft Q&A, based on my test, I reproduced your problem. It seems that datagirdview will move cursor to the same column in the next row when we use the datagirdview keydown event.

    I recommend that you use ProcessCmdKey method to do it and here is a code example you could refer to.

     protected override bool ProcessCmdKey(ref Message msg, Keys keyData)  
            {  
                if (keyData == Keys.Enter)  
                {  
                    if (dataGridView1.CurrentRow != null)  
                        dataGridView1.CurrentCell =  
                            dataGridView1  
                            .Rows[Math.Min(dataGridView1.CurrentRow.Index + 1, dataGridView1.Rows.Count - 1)]  
                            .Cells[0];  
                }  
                return base.ProcessCmdKey(ref msg, keyData);  
            }  
    

    Note: Please delete the keydown event to avoid the conflict when we use the above method.

    Tested result:
    244635-5.gif

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    No comments

0 additional answers

Sort by: Most helpful