问题如下:现在有两个窗体,一个是物料维护窗体,包含控件是DATAGRID,该控件显示的内容为数据库中物料字典表的内容,现在要新增一条物料,弹出第二个物料卡片窗体,该窗体包含控件为若干文本框,在这个文本框中输入物料的基本信息保存向表中插入记录后,要在物料卡片窗体不关闭的情况下(以便接着增加新的物料),在第一个窗体DATAGRID中同步显示新增加的内容,我在网上找了一下资料,我的问题类似于这里讲的http://www.cnblogs.com/iCeSnaker/archive/2004/08/28/37281.html,但这里要关闭第2个窗体后,才能在第一个窗体上显示更新,请各位多多指教,如果有源码更加感谢
方法二:接口方式
子窗体:
public interface IForm2
{
void GetText(string strText);
string SetButtonText();
}
public class Form2 : System.Windows.Forms.Form
{
private IForm2 mIForm = null;
// 构造函数,传递实例化接口的窗体
public Form2(IForm2 Iform)
{
InitializeComponent();
mIForm = Iform;
}
private void button1_Click(object sender, System.EventArgs e)
{
//IForm2 mIform = new Form1();
this.button1.Text = mIForm.SetButtonText();
mIForm.GetText(this.textBox1.Text);
}
父窗体
public class Form1 : System.Windows.Forms.Form,IForm2
{
private void button1_Click(object sender, System.EventArgs e)
{
Form2 f = new Form2(this);
f.ShowDialog();
}
public void GetText(string strText)
{
this.label1.Text = strText;
}
public string SetButtonText()
{
return "确定";
}
在Form2中设计一个静态方法将Form1类的当前实例传送进来
public static void GetForm1(Form1 fmRef)
{
fmMain=fmRef;
}
在Form1中加入
Form2.GetForm1(this); 再将Form1中的DATAGRID控件改为PUBLIC就行了