Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1361
  • Last Modified:

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

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
BlakeMcKenna
Asked:
BlakeMcKenna
  • 2
1 Solution
 
Jacques Bourgeois (James Burger)Commented:
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
 
BlakeMcKennaAuthor Commented:
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
 
BlakeMcKennaAuthor Commented:
The Application.DoEvents does help though.
0
 
Ahmed MustafaDeveloperCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now