正在从子窗体数据网格更新父窗体文本框以查看所选行
本文关键字:窗体 文本 更新 数据 数据网 网格 | 更新日期: 2023-09-27 17:59:04
我有一个Parent Form QuotationDetails,其中有一个名为ChooseCustomer的按钮,当我单击它时,会打开名为CustomerSearchForm的子窗体。现在有一个Datagridview CustomerSearchForm,它有两个按钮OK和CANCEL。因此,当我从DGV中选择一行并单击"确定"时,该行中的所有详细信息都会用选定的客户客户数据填充在QuotationDetails(大约20个文本框)中。我已经编写了所有的代码,并且运行良好。但问题是,我是通过关闭父窗体QuotationDetails并打开它的一个新实例来完成这项工作的。但要求是我需要显示父窗体并从Childform更新文本框。
以下是从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();
}
我已经搜索了一个解决方案,并得到了一个使用事件和代理来触发它的想法。
但我不确定如何在我的场景中实现同样的功能。
请告知。
谢谢。
下面是返回所选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];
}
}
}
感谢您的帮助。下面是我使用构造函数实现的代码。
Customer Search 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();
}
用于加载CustomerDetails 的QuotationDetails表单的代码
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的文本框之外。还有其他额外的文本框,用户可以在其中手动输入。但是,当用户输入数据,然后选择客户数据时,文本框中的所有数据(手动输入)都会丢失。原因是我们正在重新加载表单。我可以使用Static来存储输入的数据。除此之外,还有更好的方法可以做到这一点。