Solved

No row can be added to a DataGridView control that does not have columns. Columns must be added first???

Posted on 2014-09-09
4
815 Views
Last Modified: 2016-09-15
I'm getting the above error message when I go to add a row the DataTable. At the point when the exception is thrown...I check the DT's Column count and there are 4, which is what it is supposed to be. I actually create the DataTable in the Form_Load() Event and when I'm done adding records to the Database, I just clear the rows. Am I missing something obvious. Below is the sub-routine in which the exception is thrown.

    Private Sub LoadRepeatabilityTestGrid()
        Debug.Print("LoadRepeatabilityTestGrid")

        Try
            Dim intLoadValues As Integer = 0

            EH.ErrorMessage = ""
            strTestStartTime = Now

            If cboExcitation.Checked And Not blnExciTestComplete Then
                EH.ErrorMessage = "Excitation Reading required!" & "~I"
                GoTo ProcessMessage
            ElseIf cboResistance.Checked And Not blnResiTestComplete Then
                EH.ErrorMessage = "Rin/Rout Readings required!" & "~I"
                GoTo ProcessMessage
            ElseIf strFirstZero.ToString.Length = 0 Then
                EH.ErrorMessage = "1st Zero Reading required!" & "~I"
                GoTo ProcessMessage
                'ElseIf cboLeakage.Checked And Not blnLeakTestComplete Then
                '    EH.ErrorMessage = "Leakage required!" & "~I"
                '    GoTo ProcessMessage
            End If

            If Not CheckParameterValuesAgain() Then
                EH.ErrorMessage = "Please check parameter values!" & "~I"
            Else
                mlStatusMessage.Text = "Repeatability Test in progress for S/N: " & txtSNJN.Text
                Application.DoEvents()

                If txtTemperature.Text.Length = 0 And txtHumidity.Text.Length = 0 Then
                    GetTemperatureHumidity()
                End If

                If EH.ErrorMessage = "" Then
                    pnlControl.Enabled = False
                    grpStandards.Enabled = False
                    grpOutputInfo.Enabled = False
                    grpSubTestTypes.Enabled = False
                    grpChannelDirection.Enabled = False

                    If txtLoadingCapacity.Text > "" Then
                        intLoadValues = CInt(Trim(txtLoadingCapacity.Text))
                    End If

                    dgvRepeatability.DataSource = Nothing

                    Dim iLastRow As Integer = updRepeatabilityIterations.Value * 2
                    Dim iLoadPoint As Integer = 1
                    Dim dtRow As DataRow

                    For y As Integer = 0 To updRepeatabilityIterations.Value - 1
                        For x As Integer = 0 To 1
                            dtRow = dbDETAILS_Repeatability.NewRow
                            dtRow("loadingPoint") = iLoadPoint

                            If x = 0 Or x = iLastRow Then
                                dtRow("loadValue") = 0
                            Else
                                dtRow("loadValue") = GetDecimalPlaces(intLoadValues, gLoadDecimalPlaces)
                            End If

                            dtRow("output") = String.Empty
                            dtRow("repeatabilityError") = String.Empty

                            dbDETAILS_Repeatability.Rows.Add(dtRow)   ---------> Exception is thrown on this line
                            iLoadPoint += 1
                        Next
                    Next

                    dgvRepeatability.DataSource = dbDETAILS_Repeatability
                    BuildRepeatabilityGrid()

                    GetFocusBackColor(btnSTStartTest, False)

                    iRowIDX = 0
                    iColIDX = 2

                    pnlExcitation.Enabled = False
                    pnlResistance.Enabled = False
                    pnlFirstFinalZero.Enabled = False

                    mlStatusMessage.Text = "Repeatability Test for S/N: " & txtSNJN.Text

                    dgvRepeatability.Enabled = True
                    dgvRepeatability.Focus()
                    dgvRepeatability.CurrentCell = dgvRepeatability(iColIDX, iRowIDX)
                End If
            End If

ProcessMessage:

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

Open in new window

0
Comment
Question by:BlakeMcKenna
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
Comment Utility
Databinding has these strange ways of doing things that do not seem to make sense in your back.

First of all, the message is not about the number of columns in the DataTable, it's about the number of columns in the grid. So the count of rows in the DataTable is not useful.

I have seen instances where unbinding the data store takes a little while, which causes problems such as the one you are encountering. Do you still have the problem if you slowly go step by step in debugging mode? If not, then this is probably the root of your problem.

In such a case, I would thry an Application.DoEvents or a short Threading.Thread.Sleep call right after the line where you set your DataSource to Nothing. This might leave time for the binding to completely break, and your DataTable will then be completely independent of the grid.
0
 

Author Comment

by:BlakeMcKenna
Comment Utility
James,

It turns out that I was setting the dgv's datasource to nothing...but the funny thing is that code had been working before. It must have been something else I changed and forgot about.
0
 

Author Closing Comment

by:BlakeMcKenna
Comment Utility
The Application.DoEvents does help though.
0
 

Expert Comment

by:Ahmed Mustafa
Comment Utility
Changing the order of control definition in Form.designer.vb/cs so as to enable the form_load event that interact with dgv will make the form recognize the existing colmun definition and properties settings
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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 (http://www.ecb.europa.eu/stats/exch…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

8 Experts available now in Live!

Get 1:1 Help Now