This is the line of code that causes the problem. This code works but there is a problem.
public void LoadProducts()
{
int i = 0;
dataGridView2.Rows.Clear();
cn.Open();
cm = new SqlCommand("Select p.pcode, p.barcode, p.pdesc, b.brand, c.category, p.price, p.qty from tblProduct as p inner join tblBrand as b on b.id = p.bid inner join tblCategory as c on c.id = p.cid where p.pdesc like @pdesc", cn);
cm.Parameters.AddWithValue("@pdesc", "%" + textAra.Text + "%");
dr = cm.ExecuteReader();
while (dr.Read())
{
i++;
dataGridView2.Rows.Add(i, dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), dr[4].ToString(), dr[5].ToString(), dr[6].ToString());
}
dr.Close();
cn.Close();
}
I am calling this function from a form inside another form and these two forms have separate datagridviews. Now the mother form has datagridview1 and child form has datagridview2.
Mother form has a button when clicked shows our child form and here is the problem. When i write
private void pictureBox1_Click(object sender, EventArgs e)
{
this.Dispose();
}
my child form closes and my mother form comes back. But it gives me an error saying
No row can be added to a DataGridView control that does not have columns. Columns must be added first
Now there is a code in the mother form which i use to call my child form.
private void btnSearch_Click(object sender, EventArgs e)
{
if(textSearch.Text == String.Empty)
{
frmLookProduct früb = new frmLookProduct();
früb.ShowDialog();
früb.LoadProducts();
}
}
i believe the problem is that früb.LoadProducts(); line.
What I have tried:
When i remove früb.LookProduct(); and use the same line of code in
private void frmLookProduct_Load(object sender, EventArgs e)
{
LoadProducts();
}
it gives no problems but wont this cause further problems?