WinForm中的DataGridView子窗体刷新父窗体备忘

主要是前两天,有个同学问我这个方面的内容,当时利用了委托事件的方法来解决的,感觉效果还是挺好的。于是便记录了下来,以作备忘。

本例中,主要实现的是向Access数据库中添加记录的功能。其中,主窗体负责显示数据,而弹出的子窗体负责添加数据,数据添加完毕,需要刷新主窗体。

父窗体代码如下:

 
  
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 mainFrm : Form
{
public mainFrm()
{
InitializeComponent();
}

childFrm childfrm;

private void mainFrm_Load( object sender, EventArgs e)
{
string sql = " select [userName] as 姓名,[userPass] as 编制,[userSex] as 性别,[userQQ] as QQ,[userAddress] as 住址 from UserData " ;
DataTable dt
= DB.GenDT(sql);
this .dataGridView1.DataSource = dt;
}

private void button1_Click( object sender, EventArgs e)
{
if (childfrm == null || childfrm.IsDisposed == true )
{
childfrm
= new childFrm();
childfrm.raiseCallBackRefreshEvent
+= new raiseCallBackRefreshDelegate(childfrm_raiseCallBackRefreshEvent); // 注册回调事件,也即将子窗体抛出的事件接收,然后处理
childfrm.ShowDialog();
}
else
{
childfrm.raiseCallBackRefreshEvent
+= new raiseCallBackRefreshDelegate(childfrm_raiseCallBackRefreshEvent);
childfrm.ShowDialog();
}
}

private void childfrm_raiseCallBackRefreshEvent()
{
mainFrm_Load(
null , null ); // 刷新主窗体
}
}
}

子窗体代码如下:

 
  
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 delegate void raiseCallBackRefreshDelegate(); // 全局委托申明

public partial class childFrm : Form
{
public childFrm()
{
InitializeComponent();
}

public event raiseCallBackRefreshDelegate raiseCallBackRefreshEvent; // 公开的事件

private void childFrm_Load( object sender, EventArgs e)
{

}

private void button1_Click( object sender, EventArgs e)
{
string sql = @" insert into UserData(userName,userPass,userSex,userQQ,userAddress)
values('
" + textBox1.Text + " ',' " + textBox2.Text + " ',' " + textBox3.Text + " ',' " + textBox4.Text + " ',' " + textBox5.Text + " ') " ;
DB.RunSQL(sql);
raiseCallBackRefreshEvent();
// 将事件抛出去,在主窗体中接收,并作相应处理
this .Close();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值