Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
This is the line of code that causes the problem. This code works but there is a problem.

C#
public void LoadProducts()
        {//this line of code is completely alien to me and it is for to search bar
            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

C#
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.


C#
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

C#
private void frmLookProduct_Load(object sender, EventArgs e)
        {
            LoadProducts();
        }


it gives no problems but wont this cause further problems?
Posted
Updated 10-Sep-20 22:40pm

1 solution

Dispose is a way of saying "I am finished with this forever; I never want anything in it ever again, please recycling it for me".
When you call Dispose on your form it closes, and all the resources the form used are Disposed as well.
So the form tells the DataGridView to unload all information it uses and it does: it discards all it's column info and everything to do with it.

It's like feeding a printed letter into a shredder, then burning the shreds.

Don't Dispose anything and expect it to work afterwards - just like the letter, it's gone!

Instead of Dispose in your Click handler, call Close.
But think about it: why are you trying to load data into a control on a form you just closed? Shouldn't you be calling LoadProducts before you tell it to display? ShowDialog doesn't return until the form is closed...
private void btnSearch_Click(object sender, EventArgs e)
        {
            if(textSearch.Text == String.Empty) 
            {
                frmLookProduct früb = new frmLookProduct();
                früb.LoadProducts();
                früb.ShowDialog();
            }
        }
Might work better.
 
Share this answer
 
Comments
mekenix 11-Sep-20 5:03am    
That worked. Thanks sir
OriginalGriff 11-Sep-20 5:21am    
You're welcome!

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