Click here to Skip to main content
15,895,746 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:

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

       If cn.State = ConnectionState.Open Then
           cn.Close()
       End If
       che = 0
       Module1.conn()
       cn.Open()

       Dim ctr, i As Integer
       'cn.Open()
       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)
       Next
       'cn.Close()

       ds.Clear()
       'cn.Open()
       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
           ComboBox4.Items.Add(ds.Tables("Class").Rows(i)(1).ToString)
       Next
       'cn.Close()

   End Sub
Posted
Updated 17-Aug-20 20:05pm
v3
Comments
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:
VB
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"}
        DataGridView1.Rows.Add(row)
        row = New String() {"2", "Product 2", "2000"}
        DataGridView1.Rows.Add(row)
        row = New String() {"3", "Product 3", "3000"}
        DataGridView1.Rows.Add(row)
        row = New String() {"4", "Product 4", "4000"}
        DataGridView1.Rows.Add(row)
    End Sub

More details, refer: DataGridView.Columns Property (System.Windows.Forms) | Microsoft Docs[^]
 
Share this answer
 
A better approach would be to refine your SELECT statement to return only the data you want in the order you need it:
SQL
SELECT MyColumn AS [Title For Column], MyOtherColumn AS [Different Text] FROM MyTable ...
And then use the resulting DataTable as a DataSource for your DataGridView:
C#
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!
 
Share this answer
 

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