DataGridView not recognizing Dataset?

Posted on 2014-08-14
Medium Priority
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 27

Accepted Solution

Shaun Kline earned 2000 total points
ID: 40261166
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

ID: 40261178

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!


Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline
Suggested Courses

862 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