Click here to Skip to main content
15,795,793 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello Friends

I have a query related to focus on cell in datagridview.

Acutally i have a datagridview and i want to move from cell 1 to cell 2 on keydown event and also check if the cell 1 is not blank then it moves to cell 2 otherwise move to cell 3.

My code works fine for column but it moves to next row.

here is my code on key down event

If e.KeyCode = Keys.Enter Then
            Select Case DataGridView1.CurrentCell.ColumnIndex
                Case 1
                    If Len(Trim(DataGridView1.CurrentRow.Cells(1).Value)) > 0 Then
                        DataGridView1.CurrentCell = DataGridView1(2, DataGridView1.CurrentCell.RowIndex)
                        DataGridView1.CurrentCell = DataGridView1(3, DataGridView1.CurrentCell.RowIndex)
                    End If
                Case 2
                    DataGridView1.CurrentCell = DataGridView1(3, DataGridView1.CurrentCell.RowIndex)
                Case 3
                    DataGridView1.CurrentCell = DataGridView1(4, DataGridView1.CurrentCell.RowIndex)
                Case 4
                    DataGridView1.CurrentCell = DataGridView1(5, DataGridView1.CurrentCell.RowIndex)
                Case 5
                    DataGridView1.CurrentCell = DataGridView1(1, DataGridView1.CurrentCell.RowIndex + 1)
            End Select
        End If

it works but move to next rows .

Please help me out as soon as possible.

Thanks & Regards

1 solution

It will move to the next row because that's what the ENTER key normally does. You need to tell it that you are handling it on your own and it should skip that. To do that, you need to add this statement right after you've determined that it's the Enter key.

e.Handled = True
Share this answer
Sunil.yadava09 5-Jul-13 1:02am    
Thanks for help

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