// 父窗体Form1
// 父窗体的方法
private void btnShow_Click(object sender, EventArgs e)
{
ProductAddWindow addWindow = new ProductAddWindow();
// ShowDialog要将父类对象传入
addWindow.ShowDialog(this);
}
// 子窗体 ProductAddWindow
// 子窗体方法
private void btnSave_Click(object sender, EventArgs e)
{
using(var conn = DapperHelper.GetConn())
{
Product product = new Product();
product.Name ="产品1";
product.ProductType = "金属";
conn.Insert(product);
Form1 form1= this.Owner as Form1;
// 新增成功,调用父窗体InitData方法(刷新父页面DataGridView的数据)
form1.InitData();
// 关闭的窗体
this.Close();
}
}
// 子窗体调用父窗体的控件
// 子窗体调用父窗体前提,要将父窗体的控件Modifiers属性设置为public
private void ProductEdit_Load(object sender, EventArgs e)
{
Form1 form1 = this.Owner as Form1;
var cells = form1.dgvList.SelectedRows[0].Cells;
}
网友评论