DataGridView not recognizing Dataset?

Posted on 2014-08-14
Last Modified: 2014-08-14
I have a function that returns a single row from a SQL Server Database. I'm getting the following error message: "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" on the line marked "Error occurs here". That tells me that even though the dataset contains a valid record, for some reason it's not being assigned to the DataGridView. What's up with that? See attached image.

    Private Function GetDetailRecords(ByVal iEquipmentID As Integer)
            clsDE = New CalTableClasses.CalibrationClasses.DETAILS_Equipment

            BL.GetDetailsEquipmentRecord(iEquipmentID, clsDE, EH)

            If EH.Success Then
                dgvComponents.DataSource = Nothing

                If EH.ErrorMessage = "" Then
                    If EH.DataSet.Tables(0).Rows.Count > 0 Then
                        dgvComponents.DataSource = EH.DataSet.Tables(0)
                        dgvComponents.Columns(18).Visible = False     'Error occurs here
                        dgvComponents.Columns(19).Visible = False
                        dgvComponents.Columns(20).Visible = False
                        dgvComponents.Columns(21).Visible = False
                        dgvComponents.Columns(22).Visible = False
                        dgvComponents.Columns(23).Visible = False
                        dgvComponents.Columns(24).Visible = False
                        dgvComponents.Columns(25).Visible = False

                        For Each row As DataRow In EH.DataSet.Tables(0).Rows
                            If Not IsDBNull(row(1)) Then
                                iSeqNO = row(1)
                            End If
                    End If
                End If
            End If

        Catch ex As Exception
            EH.ErrorMessage = "GetDetailRecords() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        Return EH
    End Function

Open in new window

Question by:BlakeMcKenna
    LVL 25

    Accepted Solution

    The image and code do not provide enough information. How do you have your datagridview configured? Are you manually defining the columns or are you auto-generating them? If auto-generating them, does your query return 19+ columns of data? If manually defining them, have you added 19+ columns to it? (The plus sign is because, according to your code, you have 26 columns of data to display.)

    Author Closing Comment


    You just sparked a thought. The grid is empty (no columns/rows). I forgot to set the "AutoGenerateColumns" property. I inserted that statement and it worked as it should!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now