Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I've been using the same code for the past 3 months, but when I opened and tried to run my code, it kept on showing the error but all the necessary data is still showing fine. I've already created the column in design but idk what happen with it. My other dgv are working fine, it's just that the error keeps on popping up whenever I load that specific page.

VB
Sub LoadRecords()
    Try
        sdate1 = dt1.Value.ToString("yyyy-MM-dd")
        sdate2 = dt2.Value.ToString("yyyy-MM-dd")
        DataGridView.Rows.Clear()
        conn.Open()
        cm = New MySqlCommand("SELECT * FROM tblaudit WHERE sdate _
             BETWEEN '" & sdate1 & "' AND '" & sdate2 & "' ORDER BY id DESC", conn)
        dr = cm.ExecuteReader
        While dr.Read
            DataGridView.Rows.Add(dr.Item("id").ToString, dr.Item("user").ToString, dr.Item("summary").ToString, Format(CDate(dr.Item("sdate").ToString), "MM-dd-yyyy"), dr.Item("stime").ToString)
        End While
        dr.Close()
        conn.Close()

        lblCount.Text = "          Record count (" & Format(CLng(DataGridView.RowCount), "#,##0") & ")"
    Catch ex As Exception
        conn.Close()
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub


What I have tried:

I've tried to delete and create another dgv but the error is still there. I don't want to do the databinding since my code is still working with other dgv.
Posted
Updated 2-Oct-23 5:18am
v3
Comments
Graeme_Grant 25-Sep-23 6:29am    
You know that we can't see your problem if you don't post the code ... right? Also, post the full error... You can do this by clicking on the "Improve question" link.
Ronalyn Ablang 2023 25-Sep-23 6:33am    
sorry, I added the source code
Graeme_Grant 25-Sep-23 6:34am    
...and the full error message?
Ronalyn Ablang 2023 25-Sep-23 6:37am    
"No row can be added to a DataGridView control that does not have columns. Columns must be added first."

But I already created the columns in design, the same way I added columns in other dgv. This is the only dgv that has an error and it's the first time I encountered it
Graeme_Grant 25-Sep-23 6:38am    
That's the message part, not the full error message.
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

ex.Message is only one part, there is lots more...

1 solution

Read the error message: it couldn't be any clearer:
Error:
No row can be added to a DataGridView control that does not have columns. Columns must be added first.
It's even telling you what you need to do first!
Quote:
But I already created the columns in design, the same way I added columns in other dgv
Well, clearly not, or somewhere in your code you are clearing the columns out.

Start be checking that you have the right DGV: the name implies it's not created by the designer automatically (it adds a number to the end, and nobody sane renames a control from dataGridView2 to DataGridView so it's likely that you got it wrong.

If you have carefully checked, and are absolutely sure, then do a search for all references of the control and look to see where columns are added and if they are ever removed.

Sorry, but we can't do any of that for you!
 
Share this answer
 
Comments
Graeme_Grant 25-Sep-23 7:04am    
I'm trying to get him to look at the complete error message ... there should be more there for him.
Ronalyn Ablang 25-Sep-23 7:15am    
Sorry but my other dgv works just fine, and the dgv is showing the right info but it's just that error that keeps on popping
Graeme_Grant 25-Sep-23 7:16am    
Why are you here asking for help then?
Ronalyn Ablang 25-Sep-23 7:18am    
Because of that error 😭
Graeme_Grant 25-Sep-23 7:26am    
We are trying to help you but you seem to know better....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900