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

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

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

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,828 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,372 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.
10,245 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2022-09-26T02:51:19.073+00:00

    @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.

    0 comments No comments

0 additional answers

Sort by: Most helpful