如何关闭子窗体时刷新datagridview的? [英] How to refresh datagridview when closing child form?
本文介绍了如何关闭子窗体时刷新datagridview的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在我的主要形式DGV,有它开创了另一种形式的插入一些数据到一定到DGV数据源的按钮。我想,当子窗体关闭DGV自动刷新。我尝试添加该儿童的形式关闭事件,但它并不刷新:
私人无效frmNew_FormClosing(对象发件人,FormClosingEventArgs E)
{
frmMain frmm =新frmMain();
frmm.itemCategoryBindingSource.EndEdit();
frmm.itemsTableAdapter.Fill(myDatabaseDataSet.Items);
frmm.dataGridView1.Refresh();
}
然而,当我在父窗体上的按钮添加此code,它实际上是卓有成效的:
this.itemCategoryBindingSource.EndEdit();
this.itemsTableAdapter.Fill(myDatabaseDataSet.Items);
this.dataGridView1.Refresh();
解决方案
有很多方法可以做到这一点,但下面是simpliest,它会做你想要什么,让你开始。
- 在主窗体创建一个公共方法。
- 的第二种形式修改构造采取一种主要形式。
- 创建第二种形式传递的主要形式对象的实例。
- 当关闭第二种形式,调用主窗体对象的公共方法。
Form1中
公共部分Form1类:表格{
公共Form1中(){
//添加一个标签和一个BUTTOM形成
的InitializeComponent();
}
私人无效的button1_Click(对象发件人,EventArgs的){
窗体2 oForm =新的窗体2(本);
oForm.Show();
}
公共无效PerformRefresh(){
this.label1.Text = DateTime.Now.ToLongTimeString();
}
}
窗体2
公共类窗体2:表格{
Form1中_owner;
公共窗体2(Form1的所有者){
_owner =所有者;
this.FormClosing + =新System.Windows.Forms.FormClosingEventHandler(this.Form2_FormClosing);
}
私人无效Form2_FormClosing(对象发件人,FormClosingEventArgs E){
_owner.PerformRefresh();
}
}
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.
- 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();
}
}
Form2
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屋!
查看全文