current cell focus problem after using cellendedit event

ankit goel 766 Reputation points
2022-12-11T19:34:37.337+00:00

I am designing a c# datagridview in which the enter key works as tab , back key results in moving back to previous cell . Also if the focus is at the last cell of last row of last column it creates a new row .

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)  
     {  
        e.SuppressKeyPress = true;  
        int iColumn = dataGridView1.CurrentCell.ColumnIndex;  
        int iRow = dataGridView1.CurrentCell.RowIndex;  

        if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Right || e.KeyCode == Keys.Tab)  
        {  
            //to check if the pointer reach last column  
            if (iColumn == dataGridView1.ColumnCount - 1)  
            {  
                //executing when line ends                           
                if (dataGridView1.RowCount > (iRow + 1))  
                {  
                     
                    dataGridView1.CurrentCell = dataGridView1[0, iRow + 1];  
                }  
                    // fires when you reach last cell of last row and last column   
                else  
                {  
                      
                    dataGridView1.Rows.Add();  
                    dataGridView1.CurrentCell = dataGridView1[0, iRow +1];                    
                }  
            }  
            else  
            {  
                dataGridView1.CurrentCell = dataGridView1[iColumn + 1, iRow];  
            }  
        }  
        if (e.KeyCode == Keys.Down)  
        {  
            int c = dataGridView1.CurrentCell.ColumnIndex;  
            int r = dataGridView1.CurrentCell.RowIndex;  
            if (r < dataGridView1.Rows.Count - 1) //check for index out of range  
                dataGridView1.CurrentCell = dataGridView1[c, r + 1];  
        }  
        if (e.KeyCode == Keys.Up)  
        {  
            int c = dataGridView1.CurrentCell.ColumnIndex;  
            int r = dataGridView1.CurrentCell.RowIndex;  
            if (r > 0) //check for index out of range  
                dataGridView1.CurrentCell = dataGridView1[c, r - 1];  
        }  
        if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Left)  
        {  
            //to check if you are in first cell of any line   
            if (iColumn == 0)  
            {  
                //to check if you are in first cell of fist line   
                if (iRow == 0)  
                {  
                    MessageBox.Show("you reached first cell");  
                }  
                else  
                {  
                    dataGridView1.CurrentCell = dataGridView1[dataGridView1.ColumnCount - 1, iRow - 1];  
                }  
            }  
            else  
            {  
                dataGridView1.CurrentCell = dataGridView1[iColumn - 1, iRow];  
            }  
        }  
    }  
   Now , after this i want to edit the cell values and want to move to next cell when i press enter key  , so i add  the following event (as i am in cell editing mode due to this, the above key down event will not raise )   
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)  
    {  
        // MOVE TO NEXT CELL WHEN DONE EDITING   
        SendKeys.Send("{right}");  
    }  

But after using the above event cellendedit , i found out that the cell is moving down to the next row(default behavior of datagridview) and then to next row's next cell(due to cellendedit) . Instead it should move to the next column same row . Please suggest me to fix this behaviour .
@Jack J Jun

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,103 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.
11,487 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2022-12-12T02:30:08.777+00:00

    @ankit goel , Welcome to Microsoft Q&A, based on my test, I reproduced your problem. If you want to move to the next column same row, I recommend that you don't use the Enter or Tab to move the cell. Because you have defined it in the keydown event.

    I chose F1 key to make it worked and here is a code example you could refer to.

      private void dataGridView1_KeyDown(object sender, KeyEventArgs e)  
            {  
                e.SuppressKeyPress = true;  
                int iColumn = dataGridView1.CurrentCell.ColumnIndex;  
                int iRow = dataGridView1.CurrentCell.RowIndex;  
                if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Right || e.KeyCode == Keys.Tab)  
                {  
                    //to check if the pointer reach last column  
                    if (iColumn == dataGridView1.ColumnCount - 1)  
                    {  
                        //executing when line ends                           
                        if (dataGridView1.RowCount > (iRow + 1))  
                        {  
      
                            dataGridView1.CurrentCell = dataGridView1[0, iRow + 1];  
                        }  
                        // fires when you reach last cell of last row and last column   
                        else  
                        {  
      
                            dataGridView1.Rows.Add();  
                            dataGridView1.CurrentCell = dataGridView1[0, iRow + 1];  
                        }  
                    }  
                    else  
                    {  
                        dataGridView1.CurrentCell = dataGridView1[iColumn + 1, iRow];  
                    }  
                }  
                if (e.KeyCode == Keys.Down)  
                {  
                    int c = dataGridView1.CurrentCell.ColumnIndex;  
                    int r = dataGridView1.CurrentCell.RowIndex;  
                    if (r < dataGridView1.Rows.Count - 1) //check for index out of range  
                        dataGridView1.CurrentCell = dataGridView1[c, r + 1];  
                }  
                if (e.KeyCode == Keys.Up)  
                {  
                    int c = dataGridView1.CurrentCell.ColumnIndex;  
                    int r = dataGridView1.CurrentCell.RowIndex;  
                    if (r > 0) //check for index out of range  
                        dataGridView1.CurrentCell = dataGridView1[c, r - 1];  
                }  
                if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Left)  
                {  
                    //to check if you are in first cell of any line   
                    if (iColumn == 0)  
                    {  
                        //to check if you are in first cell of fist line   
                        if (iRow == 0)  
                        {  
                            MessageBox.Show("you reached first cell");  
                        }  
                        else  
                        {  
                            dataGridView1.CurrentCell = dataGridView1[dataGridView1.ColumnCount - 1, iRow - 1];  
                        }  
                    }  
                    else  
                    {  
                        dataGridView1.CurrentCell = dataGridView1[iColumn - 1, iRow];  
                    }  
                }  
            }  
            protected override bool ProcessCmdKey(ref Message msg, Keys keyData)  
            {  
                if (keyData == Keys.F1)  
                {  
                    if (dataGridView1.CurrentRow != null)  
                        dataGridView1.CurrentCell =  
                            dataGridView1  
                            .Rows[dataGridView1.CurrentRow.Index]  
                            .Cells[dataGridView1.CurrentCell.ColumnIndex + 1];  
                }  
                return base.ProcessCmdKey(ref msg, keyData);  
            }  
    

    Updated example:

    public class NewDataGridView:DataGridView  
        {  
            protected override bool ProcessDialogKey(Keys keyData)  
            {  
                // Extract the key code from the key value.   
                Keys key = (keyData & Keys.KeyCode);  
      
                // Handle the ENTER key as if it were a RIGHT ARROW key.   
                if (key == Keys.Enter||key==Keys.Right)  
                {  
                    Console.WriteLine("in edit");  
                    return this.ProcessTabKey(keyData);  
                }  
                return base.ProcessDialogKey(keyData);  
            }  
      
            protected override bool ProcessDataGridViewKey(KeyEventArgs e)  
            {  
                // Handle the ENTER key as if it were a RIGHT ARROW key.   
                if (e.KeyCode == Keys.Enter)  
                {  
                    Console.WriteLine("no edit");  
                    return this.ProcessTabKey(e.KeyData);  
                }  
                return base.ProcessDataGridViewKey(e);  
            }  
        }  
    

    After you edit the cells, you could use F1 key to move cells to the same row next column.

    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.


Your answer

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