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
998 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
ID: 40312639
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
ID: 40312827
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
ID: 40312830
The Application.DoEvents does help though.
0
 

Expert Comment

by:Ahmed Mustafa
ID: 41799650
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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