• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 705
  • Last Modified:

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
0
Vik Shah
Asked:
Vik Shah
  • 4
  • 4
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now