title: "No row can be added to a DataGridView control that does not have columns. Columns must be added first." in VB.net
body: <p>I have been encountering an error in my VB.Net code that says &quot;No row can be added to a DataGridView control that does not have columns. Columns must be added first.&quot;. I was trying to code a live search on 3 datagrids and only 2 of them are working even though they have the same code. Please help me.</p>
  <p>Here is the code for the datagrid live search that encounters an error:</p>
  <pre><code>Private Sub txtProdSearch_TextChanged(sender As Object, e As EventArgs) Handles txtProdSearch.TextChanged
  
      Connect()
      Query = &quot;select * from tbl_productdetails where name like '%' @name '%' or code like '%' @code '%'&quot;
  
      With Command
          .CommandText = Query
          .Connection = Conn
          .Parameters.Clear()
          .Parameters.Add(&quot;@name&quot;, MySqlDbType.VarChar).Value = txtProdSearch.Text.Trim
          .Parameters.Add(&quot;@code&quot;, MySqlDbType.VarChar).Value = txtProdSearch.Text.Trim
  
          Reader = .ExecuteReader
      End With
  
      dgProd.Rows.Clear()
  
      While Reader.Read
          dgProd.Rows.Add(Reader(&quot;code&quot;), Reader(&quot;name&quot;), Reader(2), Reader(3), Reader(4))
      End While
  End Sub
  </code></pre>
  <p>And here is a code of one of the working datagrid live search:</p>
  <pre><code>Private Sub txtSearchTrans_TextChanged(sender As Object, e As EventArgs) Handles txtSearchTrans.TextChanged
      Connect()
      Query = &quot;select * from tbl_sales where transaction_num like '%' @transaction_num '%'&quot;
  
      With Command
          .CommandText = Query
          .Connection = Conn
          .Parameters.Clear()
          .Parameters.Add(&quot;@transaction_num&quot;, MySqlDbType.VarChar).Value = txtSearchTrans.Text.Trim
  
          Reader = .ExecuteReader
      End With
  
      dgSales.Rows.Clear()
  
      While Reader.Read
          dgSales.Rows.Add(Reader(&quot;transaction_num&quot;), Reader(1), Reader(2))
      End While
  End Sub
  </code></pre>