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

Edited 5 Years Ago by __avd: Correct code tags.

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

This question has already been answered. Start a new discussion instead.