Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
1,262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

730 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