How to pass the C# DataGridView

hi
I have defined the DataGridView columns properties on one form
I want to pass the object with all the properties to other form to display.
I tried to pass the refrence to other class, but it seems that the DataGridView object doesn't show
anything on the second form.
Please help !!!
Thanks a lot
Vik ShahLead .Net Web Developer / AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nayernaguibCommented:
Use the Controls.Add() method of the new form to add the DataGridView control to the form. Now the control will be displayed.

_______________

  Nayer Naguib
0
Vik ShahLead .Net Web Developer / AnalystAuthor Commented:
there is no Controls.add() associated to the Form
but I use the DataGridView1.Controls.Add() methos to add the controls from one DataGridView to another DataGridView
but it still show nothing on the second one

here is the code:

            IEnumerator controls = RegularOrderView.Controls.GetEnumerator();

            while(controls.MoveNext())
            {
                dataGridView1.Controls.Add((Control)controls.Current);
            }
            dataGridView1.Enabled = true;

 Can you tell me what's wrong with it?

Thanks



0
nayernaguibCommented:
There *is* a Controls.Add() method, and the Form object contains a Controls collection that represents the set of controls that the form contains.

Try this in the form load event handler:

              this.Controls.Add(new DataGridView());

You will see that a new DataGridView is added to the control at runtime. Similarly, if you pass a reference for the DataGridView that you have created on the first form to the second form, you can use this reference instead of the "new DataGridView()" reference in the above statement.

_______________

  Nayer Naguib
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Vik ShahLead .Net Web Developer / AnalystAuthor Commented:
Thanks a lot
I got it
0
Vik ShahLead .Net Web Developer / AnalystAuthor Commented:
new problem:

when I show the Grid on the second form, the grid on the first form disappear!!!!
how can I  keep it on the both form?

Thanks

0
nayernaguibCommented:
Do you want to show a form with the same controls as the first form? You can just create a new instance of the form class and display it.

_______________

  Nayer Naguib
0
Vik ShahLead .Net Web Developer / AnalystAuthor Commented:
on my first Form, I have 4 DataGridViews, but each one only shows part of the datatable
on each view I have a button. when I click on the button, it should show complete datatable
on a seperate form.
so when I close the second form, I should be able to go back to the first form
but now as soon as I open the second form, one of the 4 views on the first form disappear
it looks like the dataGridView got disposed on both forms

how I can resolve it?

Thanks

0
nayernaguibCommented:
When you add the DataGridView control to the second form, you actually don't *copy* the control, but rather, you are refering to the same control using a copy of the reference. There's no straightforward method to clone Windows Forms controls in .NET. Take a look at this article:

  http://www.codeproject.com/cs/miscctrl/ControlCloneTst.asp

The article includes source code that might help you clone your control.

Another solution is to just instantiate another form of the same type as the first form, and then hide the unwanted three DataGridView controls, resize the remaining DataGridView control, and display the complete table in the control.

_______________

  Nayer Naguib
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.