Reputation: 179
following is the code which i have used while selecting the particular rows column value on pressing the F9 key.but i get the error as argument out of range exception was handled.detailed error comes as index out of range exception.
Private Sub dgsearchitemlist_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles dgsearchitemlist.KeyDown
If e.KeyCode = Keys.F9 Then
itemcode = dgsearchitemlist.SelectedRows(0).Cells(0).Value
description = dgsearchitemlist.SelectedRows(0).Cells(2).Value.ToString
uom = dgsearchitemlist.SelectedRows(0).Cells(3).Value.ToString
End If
End Sub
Upvotes: 0
Views: 5772
Reputation: 3940
Perhaps dgsearchitemlist.SelectionMode is NOT SET to either RowHeaderSelect or FullRowSelect. Manually selecting all the cells of a row does not select that row. please check and set the property to any of these values.
If you need just the last selected row, then you can use dgsearchitemlist.CurrentRow instead of dgsearchitemlist.SelectedRows(0). Then you don't have to check whether any rows have been selected or not.
Hope any of these alternatives will click !
Upvotes: 1
Reputation: 38865
It sounds like maybe in some cases, there are no rows selected. try this:
Private Sub dgsearchitemlist_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles dgsearchitemlist.KeyDown
If dgsearchitemlist.SelectedRows.Count = 0 Then Exit Sub
If e.KeyCode = Keys.F9 Then
itemcode = dgsearchitemlist.SelectedRows(0).Cells(0).Value
...
Upvotes: 0
Reputation: 9322
Most likely you reference a column index that does not exist, in this case it could be either of this code:
description = dgsearchitemlist.SelectedRows(0).Cells(2).Value.ToString
uom = dgsearchitemlist.SelectedRows(0).Cells(3).Value.ToString
That is Cells(2) or Cell(3). If you have only two columns and you have index of 2 that means you are accessing column 3 and if you have an index 3 it means you are accessing column 4. And any of those columns does not exist then it would be index out of range.
Upvotes: 0