Link to home
Create AccountLog in
Avatar of GRChandrashekar
GRChandrashekarFlag for India

asked on

Datagridview

Private Sub DgridKeyDown(sender As System.Object,
                             e As Windows.Forms.KeyEventArgs) Handles dgrid.KeyDown
        Dim crRowIndex As Integer = dgrid.CurrentCell.RowIndex
        If crRowIndex + 1 = dgrid.Rows.Count Then
        Else
            Dim row As DataGridViewRow = dgrid.SelectedRows(0)
            Loadcontrolsfromgridview(row)
        End If
    End Sub

Open in new window

Private Sub Loadcontrolsfromgridview(row As DataGridViewRow)
        _primarykey = 0
        tbxdescription.Text = row.Cells("Description").Value
        _primarykey = row.Cells("TitleID").Value
        btnsave.Text = "Update [F2]"
    End Sub

Open in new window

Hi,

The Problem with above code is
1. unless I click on the circled place and press enter, the data wont get loaded in textbox.
2. Pressing enter should not move to next row in grid. Only arrow keys should be used to navigate.
Avatar of GRChandrashekar
GRChandrashekar
Flag of India image

ASKER

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Something wrong

I get error
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
That seems to be unrelated to this. Do you know which line throws the error?
This Line

Dim row As DataGridViewRow = dgrid.SelectedRows(0)

The moment i try to press down arrow it throws error
>Dim row As DataGridViewRow = dgrid.SelectedRows(0)

That's because now row is selected. Add a check around this code

If dgrid.SelectedRows.Count > 0 Then
   Dim row As DataGridViewRow = dgrid.SelectedRows(0)
   ...
End If
after this change updown arrow keys do not work for navigation and have to click on row and then enter to select :(
Then you need to select the first row if no row is currently selected because you want to move to next row of selected row when you press down arrow so if there is no selected row then which row you move to?