关闭子窗体时如何刷新datagridview? [英] How to refresh datagridview when closing child form?
本文介绍了关闭子窗体时如何刷新datagridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的主表单上有一个 dgv,有一个按钮可以打开另一个表单以将一些数据插入到绑定到 dgv 的数据源中.我想当子窗体关闭 dgv 自动刷新时.我试图在子窗体关闭事件中添加它,但它没有刷新:
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:
private void frmNew_FormClosing(object sender, FormClosingEventArgs e)
{
frmMain frmm = new frmMain();
frmm.itemCategoryBindingSource.EndEdit();
frmm.itemsTableAdapter.Fill(myDatabaseDataSet.Items);
frmm.dataGridView1.Refresh();
}
但是,当我在父窗体的按钮中添加此代码时,它实际上起到了作用:
However, when I add this code in a button on the parent form, it actually does the trick:
this.itemCategoryBindingSource.EndEdit();
this.itemsTableAdapter.Fill(myDatabaseDataSet.Items);
this.dataGridView1.Refresh();
推荐答案
有很多方法可以做到这一点,但以下是最简单的,它会做你想做的事情并让你开始.
There are many ways to do this, but the following is the simpliest and it will do what you want and get you started.
- 在主窗体上创建一个公共方法.
- 修改了第二种形式的构造函数以采用主形式.
- 创建传递主表单对象的第二个表单的实例.
- 关闭第二个表单时,调用主表单对象的公共方法.
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();
}
}
表格 2
public class Form2 : Form {
Form1 _owner;
public Form2(Form1 owner) {
_owner = owner;
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form2_FormClosing);
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e) {
_owner.PerformRefresh();
}
}
这篇关于关闭子窗体时如何刷新datagridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文