Hi!,
I have a formProd and a dataGridView in it.
I want to access the dataGridview of formProd from formMain.

foreach (Form form in Application.OpenForms)
                {
                    formProd prd = (formProd)form;
                    if (prd is formProd)

                        prd.itemsGridView.DataSource = DS.Tables["product_Header"];
                }

this gives me InvalidCastException.
can any one tell me how to correct this.

Thanks in advance

Hey there.
I had the exact same problem. You wont be able to do it directly as far as i know.
What i did was i created a class. (that class can be accessed by any form)
so you create that class, and let formProd set values to it, then let formMain get it.

Then it will work.

you can do something like below

foreach (Form form in Application.OpenForms)
{
         if (form is formProd)
              form.itemsGridView.DataSource = DS.Tables["product_Header"];
}

Hi!,
I have a formProd and a dataGridView in it.
I want to access the dataGridview of formProd from formMain.

foreach (Form form in Application.OpenForms)
                {
                    formProd prd = (formProd)form;
                    if (prd is formProd)

                        prd.itemsGridView.DataSource = DS.Tables["product_Header"];
                }

this gives me InvalidCastException.
can any one tell me how to correct this.

Thanks in advance

Hey there.
I had the exact same problem. You wont be able to do it directly as far as i know.
What i did was i created a class. (that class can be accessed by any form)
so you create that class, and let formProd set values to it, then let formMain get it.

Then it will work.

Thank you.....

you can do something like below

foreach (Form form in Application.OpenForms)
{
         if (form is formProd)
              form.itemsGridView.DataSource = DS.Tables["product_Header"];
}

Thank you......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.