Results 1 to 6 of 6

Thread: Index was out of range. Must be non-negative and less than the size of the collection

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    17

    Index was out of range. Must be non-negative and less than the size of the collection

    I want to populate a datagridview control with data from a database. The thing is that the datagridview has five columns but the last three are Read only because they populate based on the values of the first two. I'm getting an out of index error. Following is the code and an image of the DGV control and the error.

    Code:
     SQL.RunQuery("SELECT partNumber, lineQty FROM SERVICE_ORDER_LINE WHERE servNumber = '" & intValue & "' ")
                    ' To verify there is data and that is correct
                    Dim debug1, debug3 As String
                    Dim debug2, debug4 As Integer
                    debug1 = SQL.SQLDS.Tables(0).Rows(0)(0)
                    debug2 = SQL.SQLDS.Tables(0).Rows(0)(1)
                    debug3 = SQL.SQLDS.Tables(0).Rows(1)(0)
                    debug4 = SQL.SQLDS.Tables(0).Rows(1)(1)
                    MsgBox(debug1)
                    MsgBox(debug2)
                    MsgBox(debug3)
                    MsgBox(debug4)
                    Dim var As Integer = SQL.SQLDS.Tables(0).Rows.Count - 1
                    MsgBox(var)
                    For i As Integer = 0 To var
                        For j As Integer = 0 To 1
                            frmServiceOrder.dgvPartsSO.Rows(i).Cells(j).Value = SQL.SQLDS.Tables(0).Rows(i)(j)
                        Next
                    Next
    Name:  dgvPartsSO.PNG
Views: 1538
Size:  3.7 KBName:  displayPartsErr.jpg
Views: 1538
Size:  14.9 KB

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Index was out of range. Must be non-negative and less than the size of the collec

    you only have 1 row in your dgv

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    17

    Re: Index was out of range. Must be non-negative and less than the size of the collec

    Yes because that is its starting size. All DGV start that way. Is there a way to make it display more rows before actually inserting any value???

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Index was out of range. Must be non-negative and less than the size of the collec

    dgv.rows.add(number of rows)

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    17

    Re: Index was out of range. Must be non-negative and less than the size of the collec

    Ok will try it...

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    17

    Re: Index was out of range. Must be non-negative and less than the size of the collec

    Thanks a lot it worked...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width