Refreshing Datagridview when a child form is closed
2016-03-09 22:21
351 查看
Problem:
I've a dgv on my main form, there is a button that opens up another form to insert some data into the datasource bounded to the dgv. I want when child form closes the dgv auto refresh. I tried to add this in child form closing event, but it doesn't refresh:
Solution:
There are many ways to do this, but the following is the simpliest and it will do what you want and get you started.
Create a public method on your main form.
Modified constructor of second form to take a main form.
Create an instance of the second form passing the main form object.
When closing second form, call the public method of the main form object.
[/code]
private void Form2_Disposed(object sender, EventArgs e)
{
_owner.PerformRefersh();
}
Reference : http://stackoverflow.com/questions/2395624/how-to-refresh-datagridview-when-closing-child-form[/code]
I've a dgv on my main form, there is a button that opens up another form to insert some data into the datasource bounded to the dgv. I want when child form closes the dgv auto refresh. I tried to add this in child form closing event, but it doesn't refresh:
Solution:
There are many ways to do this, but the following is the simpliest and it will do what you want and get you started.
Create a public method on your main form.
Modified constructor of second form to take a main form.
Create an instance of the second form passing the main form object.
When closing second form, call the public method of the main form object.
Form1
public partial class Form1 : Form { public Form1() { //'add a label and a buttom to form InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 oForm = new Form2(this); oForm.Show(); } public void PerformRefresh() { this.label1.Text = DateTime.Now.ToLongTimeString(); } }
[/code]
Form2
public class Form2 : Form { Form1 _owner; public Form2(Form1 owner) { _owner = owner; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form2_FormClosing); // When I run this line, it shows error. this.Disposed += new EventHandler(Form2_Disposed);// This line is used to replace above line }
private void Form2_Disposed(object sender, EventArgs e)
{
_owner.PerformRefersh();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e) { _owner.PerformRefresh(); } }
Reference : http://stackoverflow.com/questions/2395624/how-to-refresh-datagridview-when-closing-child-form[/code]
相关文章推荐
- 宣誓我的主权
- 自考C++程序设计试题2009年1月
- 新浪sae安装wordpress方法(第一方法)
- 随机生成小学四则运算练习题小程序
- EXTJS 5 学习笔记2 - Components
- SiteServer CMS 标签总结
- 字符串转成整数
- unity3D应用随笔,StartCoroutine,协程的概念
- 简单理解java容器类
- 公钥,私钥和数字签名这样最好理解
- php的学习笔记之面向对象(四)继承
- 两级标题栏带下拉选择框的页面刷新
- Spring进阶之路(7)-Bean的生命周期以及创建、销毁执行流程
- 第二周项目3——小试循环(5)
- HDU 1276 士兵队列训练问题
- 数组中超过一半(N/2)的数)进阶 (找到出现次数超过N/K的数)
- Python IDLE 清屏插件
- dfs2016.3.9
- 使用 Docker 容器应该避免的 10 个事情
- 第一次试验2-1