Click here to Skip to main content
12,952,792 members (36,117 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hello There

I have a C# application in which there are two forms. ParentForm contains several controls like TextBoxes,DataGridView etc. Now I want to fetch the values of these controls on ChildForm.

In ParentForm, I declared this controls as Public and In ChildForm, I create object of ParentForm as ParentForm uxParent = new ParentForm().

There is a DataGridView Control in ParentForm which has 5 rows. Now I want to get the value of these rows in ChildForm but the rowcount display as 0.

Need Suggestion
Posted 1-Feb-13 20:00pm
Krunal Rohit 2-Feb-13 2:04am
Pass values using Constructors
Mayank Topiwala 2-Feb-13 2:07am
For that I have to create multiple constructors. Can I have any other options ?
Krunal Rohit 2-Feb-13 2:33am
Is it form app ?
Mayank Topiwala 2-Feb-13 2:41am
Windows Application

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Really, don't do that.
The first problem is that
ParentForm uxParent = new ParentForm();
Does not give you access to the actual parent - the new keyword is a clue here - it generates a new instance of the parent form. If your parent form automatically creates an instance of the child form in it's constructor, your program will immediately crash because it will loop itself. To do anything like that, yoiu need access to teh actual instance of the parent form.

While that is available (as Kishor syas in his solution), it is a very bad idea to use it for something like this, because it ties the design of the two forms together so that neither one can exist without the other, and you cannot make changes to one without considering any possible effects on the other. This is very bad practice, and against the principles of OOP. This is why all form controls are declared private by default!

It's not difficult to do the job properly - all you have to do is create properties in the child form to supply the data to, create an event in the child form which says it needs the data, and handle the event in the parent:
Child form:
public string MyProperty
    get { return myTextBox.Text; }
    set { myTextBox.Text = value; }
/// <summary>
/// Event to indicate DataRequired
/// </summary>
public event EventHandler ReadyForData;
/// <summary>
/// Called to signal to subscribers that DataRequired
/// </summary>
/// <param name="e"></param>
protected virtual void OnReadyForData(EventArgs e)
    EventHandler eh = ReadyForData;
    if (eh != null)
        eh(this, e);
To signal you need the data, just call the method:

In the parent form, add a handler when you construct the child instance:
            frmChild myChildForm = new frmChild();
            ... Do your other child initialization
            myChildForm.ReadyForData += new EventHandler(myChildForm_ReadyForData);
        void myChildForm_ReadyForData(object sender, EventArgs e)
            frmChild child = sender as frmChild;
            if (child != null)
                child.MyProperty = "Here is the new data";
Mayank Topiwala 2-Feb-13 3:40am
thanx originalgriff and can i have some website links through which I can design main menu of ERP.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,389
CHill60 3,490
Maciej Los 3,103
Jochen Arndt 1,975
ppolymorphe 1,920

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 2 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100