move between datagridview rows using up and down

md110

Member
Joined
Oct 14, 2008
Messages
11
Programming Experience
1-3
hai my friends
i have vb.net form contains one datagridview with data from database

my datagridview load data at the time of load event of form


i have one textbox1 in my form

my focus is in textbox1 t the time of load form


i need to move up and down through my grid when i press up or down arrow accordingly

currently i should click in any row then only i can move

how i can move between rows using up and down arrow by keeping my focus in textbox1
what i need to write in keydown or keypress ??

i need always focus in textbox, moving through grid up and down using arrow key regards
 
Hello.

You're right, you need to get the KeyDown Event of the Textbox for this. I've implented such functions in an inherited DataGridView, so you could just call MyBase.OnKeyDown() from within the inherited DataGrid for this...

But back to your question:
VB.NET:
Select Case e.KeyCode
      Case Keys.Down
            If Me.yourDtGrd.CurrentCell.RowIndex < Me.your.DtGrd.RowCount - 1 Then
                  Me.yourDtGrd.CurrentCell = Me.yourDtGrd(Me.yourDtGrd.CurrentCell.ColumnIndex, Me.yourDtGrd.CurrentCell.RowIndex + 1)
            End If
            e.Handled = True
      Case Keys.Up
            If Me.yourDtGrd.CurrentCell.RowIndex > 0 Then
                  Me.yourDtGrd.CurrentCell = Me.yourDtGrd(Me.yourDtGrd.CurrentCell.ColumnIndex, Me.yourDtGrd.CurrentCell.RowIndex - 1)
            End If
            e.Handled = True
End Select

Me.yourTextBox.Focus()

Bobby
 
Back
Top