0

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
10
  • How many columns are in your gridview and on which line the error came up? Commented Nov 6, 2013 at 11:47
  • that will happen if there are no SelectedRows or if you have fewer columns than you are referencing Commented Nov 6, 2013 at 11:51
  • the error came in the first line itself..itemcode = dgsearchitemlist.SelectedRows(0).Cells(0).Value Commented Nov 6, 2013 at 11:55
  • 1
    if there are only 3 columns, then Cells(3) is out of range (0, 1, 2) = 3 Commented Nov 6, 2013 at 12:00
  • 1
    Try to comment out the cells(0) line and see what happens. Or are there rows in the first place? Commented Nov 6, 2013 at 12:04

3 Answers 3

1

Alternative 1:

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.

Alternative 2:

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 !

Sign up to request clarification or add additional context in comments.

1 Comment

thanks man..you solution is just what the doctor ordered.yes i was searching for that only..i need only one value to be fetched on the F9 key event.thanks a lot yaar..the code is working fine now
0

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.

4 Comments

above lines are not giving error as it shows in cells(0) itself..also i have commented out these lines..description = dgsearchitemlist.SelectedRows(0).Cells(2).Value.ToString uom = dgsearchitemlist.SelectedRows(0).Cells(3).Value.ToString
@vimalvasudevan could it be that there are no rows to select in the first place? And the error comes from SelectedRows(0)?
but why no rows are being selected even when i am selecting the rows
Are you using multi-threading or is there any thread running in the background?
0

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
        ...

13 Comments

now when i am in debug mode..the statement if dgsearchitemlist.SelectedRows.Count = 0 Then Exit Sub is always coming true even if i am selecting the rows... is something wrong with the event handler
could there be other code be unselecting rows before this code runs?
set a do nothing break in the selectionchanged event to be sure. something is clearing your selection which explains the original error (it doesnt have to be your code, it could be a DGV method).
how to set the do nothing break.
console.writeline("DGV row selection changed") - NOT a messagebox! with a break on that line, you can look at the stack to see what is deselecting, without a break you can see if it is happening in the output window. Dim i AS integer = 0 is another example
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.