We help IT Professionals succeed at work.

How to pass the C# DataGridView

Vik Shah
Vik Shah asked
on
Medium Priority
752 Views
Last Modified: 2008-03-10
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
Comment
Watch Question

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
Vik ShahLead .Net Web Developer / Analyst

Author

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



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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Vik ShahLead .Net Web Developer / Analyst

Author

Commented:
Thanks a lot
I got it
Vik ShahLead .Net Web Developer / Analyst

Author

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

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
Vik ShahLead .Net Web Developer / Analyst

Author

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

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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.