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
1,078 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 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: 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

Suggested Solutions

Title # Comments Views Activity
VB/ASP.Net "Value of type 'Date' cannot be converted to 'Integer'" 12 59
Asp.Net Session Question 2 49
UDP Data Capture Thread Issue 7 44
Javascript function 3 20
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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