关于数据在两个窗口之间传递的问题
frmMain是主窗口,点击按钮,跳出另一个窗口frmFind后,在frmFind中点击按钮查询结果存于一个dataTable中,怎么把所查询的结果返回给frm中的datagridview
------解决方案--------------------frmFind中写个方法
public void FangFa()
{
frmMain fm=new frmMain();
fm.chuanzhi(dataTable);
fm.show();
}
frmMain页面
public void chuanzhi(DataTable td)
{
this.datagridview.DataSouce=td;
}
------解决方案--------------------
public class Form1
{
private button_click(object sender,eventagrs e)
{
Form2 f2 = new Form2(this.datagridview);
f2.ShowDailog();
}
}
public class Form2
{
private DataGridView dgv ;
public Form2(DataGridView datagridview)
{
dgv = datagridview;
}
private btnQuery_Click(object sender,eventargs e)
{
DataTable dtResult = GetResult();
dgv.DataSource = dtResult;
}
}
===============================================================================
public class form1
{
private bt_click(object sender,eventargs e )
{
form2 f = new form2();
if (f.showdailog() == dialogresult.ok)
{
this.dgv.datasource = f.ReturnTable;
}
}
}
public class form2
{
private datatable tbResult;
public datatatable ReturnTable
{
return tbResult;
}
private void btn_click(object sender, eventargs e)
{
tbResult = GetDataTable();
this.DialogResult = DialogReslt.Ok;
this.close();
}
}
===========================================================================
public class form2
{
public delegate void OnQueryFinished(DataTable tbResult);
public event OnQueryFinished QueryFinished;
private void btnQuery_Cliceked(object sender,eventargs e )
{
DataTable tbResult = GetDataTable();
if ( QueryFinished != null )
QueryFinished( tbResult );
}
}
public class Form1
{
private form2 ;
private void Form1_load(...)
{
form2 f = new form2();
f.QueryFinished += new OnQueryFinished(this.QueryFinished);
}
private void button_click(...)
{
f.ShowDialog();
}
private void QueryFinished(DataTable tbResult)
{
this.dgv.DataSource = tbResult;
}
}
差不多主要就这三种方式吧,代码随手写的,见谅。。