Click here to Skip to main content
16,005,058 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,

I am using dataGridView control. Usually while pressing Enter Key in a cell it will focus on cell in next row, I want to set focus on next column. I have achieved this by using below code.

C#
private void dicadmasDataGridView_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                if (dicadmasDataGridView.CurrentCell.ColumnIndex == dicadmasDataGridView.ColumnCount - 1)
                    dicadmasDataGridView.CurrentCell = dicadmasDataGridView[0, dicadmasDataGridView.CurrentCell.RowIndex + 1];
                else
                    dicadmasDataGridView.CurrentCell = dicadmasDataGridView[dicadmasDataGridView.CurrentCell.ColumnIndex + 1, dicadmasDataGridView.CurrentCell.RowIndex];
                e.SuppressKeyPress = true;
            }
        }



My problem is that if I am editing something in a particular cell and press enter it goes to the next row. I want to apply same behavior on the cell while editing also.

Thanks & Regards
Thahir
Posted
Updated 12-Feb-18 23:33pm
Comments
Sergey Alexandrovich Kryukov 8-Jul-12 14:13pm    
System.Windows.Forms? Tag it: "Forms".
--SA
Sergey Alexandrovich Kryukov 8-Jul-12 14:20pm    
Not clear what exactly do you want, "while editing" vs. something else.
--SA
Thahir Pattanathel 9-Jul-12 0:34am    
After "Enter" key cursor should focus on to next column. but now it goes to next row.
Sergey Alexandrovich Kryukov 11-Jul-12 15:54pm    
Well, this is the default behavior, as far as I can see. You are trying to change it. How do you know that your handler (shown above) is even called? Did you run it under the debugger? I don't see your "+=" operator (adding a handler to the invocation list of an event). Also, you might need to handle PreviewKeyDown instead.
--SA
yogesh vaidya 24-Nov-17 14:32pm    
need help for same question for visual basic 2013/2014

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