Click here to Skip to main content
13,050,319 members (121,036 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi All,

I have a Parent Form QuotationDetails in which I have a button called ChooseCustomer and when I click it a child form named CustomerSearchForm is opened.Now there is a Datagridview CustomerSearchForm with two button OK and CANCEL.So when I select a Row from the DGV and Click OK all the details from that row are filled in the QuotationDetails (around 20 Textboxes) with the selected customer Customer data.I have written all the code and its working fine.But the problem is I am doing this by closing the Parent form QuotationDetails and opening a new instance of it.But the requirement is I need to display the Parent form and update the Textboxes from Childform.

Below is the Code for Loading Customer Details from ChildForm
public void btnLoadCustomerDetails_Click(object sender, EventArgs e)
            QuotationManagement objQM = new QuotationManagement();
           string[] Details = new string[gvCustomerDetails.SelectedRows[0].Cells.Count];
            for (int i = 1; i < gvCustomerDetails.SelectedRows[0].Cells.Count; i++)
                Details[i] = gvCustomerDetails.SelectedRows[0].Cells[i].Value.ToString();
            objQM.txtCustomerdetails.Text = Details[3] + Environment.NewLine + Details[4] + "," + Details[5] + "," + Details[6] + "," + Details[7];
            objQM.txtcustContact.Text = Details[3];
            objQM.txtCustPhoneno.Text = Details[10];
            objQM.txtfaxNo.Text = Details[12];
            objQM.txtCustMobile.Text = Details[15];
            objQM.txtcustemail.Text = Details[14];
            objQM.txtCustWeb.Text = Details[16];
            objQM.txtcustsource.Text = Details[29];
            objQM.txtCustActivestatus.Text = Details[27];
            objQM.txtCustomerType.Text = Details[44];
            objQM.txtCustNomAccType.Text = "Customer Quotations";
            objQM.txtCustAccStatus.Text = Details[25];
            objQM.txtTerms.Text = Details[31];
            objQM.txtCurrency.Text = Details[33];
            objQM.txtcountryname.Text = Details[9];
            objQM.lblCustomermasterId.Text = Details[0];
            objQM.tabQuotationManagement.SelectedIndex = 1;

I have searched for a solution and got an Idea to use Event and a Delegate to trigger it.

But I am not sure how to implement the same in my scenario.

Please advise.

Thank you.
Posted 13-Feb-13 7:26am
Updated 13-Feb-13 7:51am
Sergey Alexandrovich Kryukov 13-Feb-13 15:14pm
There are no parent-child relationships between form. Who do you call one "child" and another "parent"?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

See similar questions and answers:
Pass data between forms[^]
Getting Value One Form In Another Form[^]
Pass value between two form in[^]

Please, use Search box in the top-right corner.
Prathap Gangireddy 13-Feb-13 14:02pm
I have never used an event and Delegate combination. Can you please tell me if we can send the Datagridviewrow which is selected in ChildForm so that I can update the corresponding textboxes with the values from the DatagridviewRow.It wud be great if you can provide a sample code..Thanks.
Maciej Los 13-Feb-13 14:05pm
Please, follow the links. There you'll find examples. OK?
Prathap Gangireddy 13-Feb-13 14:42pm
I have gone through the links.I will try to implement my idea of sending the selected DataGridViewRow to the Parent form and accesing its values to update the textboxes.Will let you if I got struck.Thanks.
Maciej Los 13-Feb-13 14:46pm
OK. And i'll promise to update my answer/solution.
Prathap Gangireddy 14-Feb-13 2:01am
Hi, I have used constructor method to implement my requirement as shared below.But using that a new instance of form is created. I mean After I click the button for searching customer details, Quotation Details form is still open and i need that.After selecting the customer and clicked on OK button in CustomerForm a new instance of Quotation Details form is opened while the already opened form is still existing i.e two instances of Quotation Details form are opened.Can you help me with this.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Please see my comment to the question. If you don't understand that, I'll explain.

Now, about your problem…

This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:[^],[^].

Prathap Gangireddy 14-Feb-13 3:01am
Hi, I have used constructor and delegate methods and both working fine.But the problem is two instances of Quotation Details are opened.Let me explain.In Form1 there is a link to Form2(but I am not closing the Form1 that is the requirement.When a button in Form2 is clicked all the details get updated in Form1 and it is shown.But here is a new instance is opened as we are not closing the already opened form.How to achieve this.Thank you.
Sergey Alexandrovich Kryukov 14-Feb-13 11:44am
First of all, never ever name anything like Form1, Form2. Those are designer-generated names, they all violate (good) Microsoft naming conventions. The designer simply cannot act better. You are supposed to rename all those names to something semantic.

So, if you ever need to use some form again, you never should close it, otherwise it will be disposed and not usable again. The usual technique is to handle FormClosing and using Cancel if this is by the user, hiding the form instead or not allowing to close it otherwise.

Now, you are triggering something in one form in the code of the code class of another form. It means, one form instance should "know" another form instance. If you pass the instance of the form to another one, it could work, but you violate encapsulation. That's while you can implement some interface and pass interface reference instead of "whole" form reference. It that clear?

Prathap Gangireddy 14-Feb-13 21:03pm
hi Sergey, Just for a clear explanation I have used Form1 and Form2. I have used Delegates to solve the issue without closing the form.All well.Thanks.
Sergey Alexandrovich Kryukov 14-Feb-13 22:23pm
Why delegate? (Well, you can use delegates...)
Did it work or not?
Will you accept my answer or not (green button)? This is quite a robust technique, I tell you.
Prathap Gangireddy 15-Feb-13 14:04pm
Yeah..used delegates and it works fine.May be i will use this robust technique anytime later.thanks
Sergey Alexandrovich Kryukov 15-Feb-13 14:24pm
Great. Wish you the best of luck.

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

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 14 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