从子窗体Datagridview所选行更新父窗体文本框

时间:2013-02-13 18:35:13

标签: c# winforms datagridview

我有一个Parent Form QuotationDetails,其中我有一个名为ChooseCustomer的按钮,当我点击它时会打开一个名为CustomerSearchForm的子窗体。现在有一个Datagridview CustomerSearchForm有两个按钮OK和CANCEL.So当我从中选择一个行时DGV和点击确定该行中的所有细节都填写在QuotationDetails(大约20个文本框)中,其中包含所选客户的客户数据。我已经编写了所有代码并且工作正常。但问题是我这样做是通过关闭父表单QuotationDetails并打开它的新实例。但是要求是我需要显示Parent表单并从Childform更新Textbox。

以下是从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];
            this.Close();
            objQM.tabQuotationManagement.SelectedIndex = 1;
            objQM.Show();            

        }

我已经搜索了一个解决方案并获得了使用Event和Delegate来触发它的想法。

但我不确定如何在我的方案中实现相同的目标。

请告知。

谢谢。

2 个答案:

答案 0 :(得分:0)

以下是返回所选Gridview行的子窗体的示例。

*** *主要表格

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {   //call the form
            MyChildForm cForm = new MyChildForm();
            if (cForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {   //get the selected row object
                DataGridViewRow dgvRow = cForm.selectedRow;
            }
        }
    }
}

*** *儿童表格

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class MyChildForm : Form
    {
        public DataGridViewRow selectedRow;

        public MyChildForm()
        {
            InitializeComponent();
        }

        private void MyChildForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Forms closing so lets get the results
            selectedRow = dataGridView1.SelectedRows[0];
        }



    }
}

答案 1 :(得分:0)

感谢您的帮助.Below是我使用构造函数实现的代码。

CustomerSearch OK按钮的代码

public void btnLoadCustomerDetails_Click(object sender, EventArgs e)
{
selectedRow = gvCustomerDetails.SelectedRows[0];
QuotationManagement objQM = new QuotationManagement(selectedRow);
objQM.tabQuotationManagement.SelectedIndex = 1;
objQM.Show();
this.Close();


}

QuotationDetails代码表格,用于加载CustomerDetails

public QuotationManagement(DataGridViewRow SelectedRow)
{
InitializeComponent();
CusRow = SelectedRow;
LoadSelectedCustomerDetails(CusRow);

}


private void LoadSelectedCustomerDetails(DataGridViewRow CusRow)
{
txtCustomerdetails.Text = CusRow.Cells[3].Value.ToString() + Environment.NewLine + CusRow.Cells[4].Value.ToString() + "," + CusRow.Cells[5].Value.ToString() + "," + CusRow.Cells[6].Value.ToString() + "," + CusRow.Cells[7].Value.ToString();
txtcustContact.Text = CusRow.Cells[3].Value.ToString();
txtCustPhoneno.Text = CusRow.Cells[10].Value.ToString();
txtfaxNo.Text = CusRow.Cells[12].Value.ToString();
txtCustMobile.Text = CusRow.Cells[15].Value.ToString();
txtcustemail.Text = CusRow.Cells[14].Value.ToString();
txtCustWeb.Text = CusRow.Cells[16].Value.ToString();
txtcustsource.Text = CusRow.Cells[29].Value.ToString();
txtCustActivestatus.Text = CusRow.Cells[27].Value.ToString();
txtCustomerType.Text = CusRow.Cells[44].Value.ToString();
txtCustNomAccType.Text = "Customer Quotations";
txtCustAccStatus.Text = CusRow.Cells[25].Value.ToString();
txtTerms.Text = CusRow.Cells[31].Value.ToString();
txtCurrency.Text = CusRow.Cells[33].Value.ToString();
txtcountryname.Text = CusRow.Cells[9].Value.ToString();
lblCustomermasterId.Text = CusRow.Cells[0].Value.ToString();     

}

除了使用CustomerDetails加载Quotation表单中的文本框。还有其他文本框,用户可以手动输入。但是当用户输入数据然后选择客户数据时,文本框(手动输入)中的所有数据都将丢失。原因是我们再次加载表单。我可以使用Static来存储输入的数据。除此之外还有更好的方法来做同样的事情。