Click here to Skip to main content
14,766,145 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to retrieve the table data to gridview but the vb gives me the following error -
"No row can be added to a DataGridView control that does not have columns. Columns must be added first."

What went wrong? Why this error is showing?

What I have tried:

Private Sub FrmFeesDetails_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

       If cn.State = ConnectionState.Open Then
       End If
       che = 0

       Dim ctr, i As Integer
       str = "select * from Fees ORDER BY FID ASC"
       cmd = New OleDbCommand(str, cn)
       da.SelectCommand = cmd
       da.Fill(ds, "Fees")
       ctr = ds.Tables("Fees").Rows.Count - 1
       For i = 0 To ctr
           DataGridView1.Rows.Add(ds.Tables("Fees").Rows(i)(0).ToString, ds.Tables("Fees").Rows(i)(1).ToString, ds.Tables("Fees").Rows(i)(2).ToString, ds.Tables("Fees").Rows(i)(3).ToString, ds.Tables("Fees").Rows(i)(4).ToString, ds.Tables("Fees").Rows(i)(5).ToString, ds.Tables("Fees").Rows(i)(6).ToString)

       str = "select * from Class ORDER BY CID ASC"
       cmd = New OleDbCommand(str, cn)
       da.SelectCommand = cmd
       da.Fill(ds, "Class")
       ctr = ds.Tables("Class").Rows.Count - 1
       For i = 0 To ctr

   End Sub
Updated 17-Aug-20 21:05pm
Sandeep Mewara 18-Aug-20 1:47am
[CAPS in online world is assumed shouting, please avoid it.]

You must be getting this error if you have not added columns before inserting the rows into the DataGridView1 above in your code.

Given you are adding data to grid row by row, you need to define columns before doing it.

2 options:
1. Add the columns before adding the rows. Add columns in a sequence you plan to add data in
2. Assign datatable to the grid as datasource to bind

Example for your above code to add columns:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGridView1.ColumnCount = 3
        DataGridView1.Columns(0).Name = "Product ID"
        DataGridView1.Columns(1).Name = "Product Name"
        DataGridView1.Columns(2).Name = "Product_Price"

        Dim row As String() = New String() {"1", "Product 1", "1000"}
        row = New String() {"2", "Product 2", "2000"}
        row = New String() {"3", "Product 3", "3000"}
        row = New String() {"4", "Product 4", "4000"}
    End Sub

More details, refer: DataGridView.Columns Property (System.Windows.Forms) | Microsoft Docs[^]
A better approach would be to refine your SELECT statement to return only the data you want in the order you need it:
SELECT MyColumn AS [Title For Column], MyOtherColumn AS [Different Text] FROM MyTable ...
And then use the resulting DataTable as a DataSource for your DataGridView:
dgvDisplay.DataSource = dt
That way, all the columns will be created for you, and the data loaded without more code being needed.
The same goes for your ComboBox: Set the DisplayMember and ValueMember properties and it'll work like a dream!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

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