从另一个表单刷新一个dataGridView [英] refresh a dataGridView from another form

查看:170
本文介绍了从另一个表单刷新一个dataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Form1和Form2。



Form1有一个dataGridView和一个用于打开Form2的按钮。
我在Form1中编写了一个方法,可以完美地刷新dataGridView:

  public void RefreshGrid()
{
dataGridView1.DataSource = empControl.SelectAll(); //很好的





在Form2中,我插入表格并使用下面的代码用于调用上述方法。
当我追踪代码时,我发现它一直实现,但dataGridView没有刷新!

  private void btnInsert_Click(object sender,EventArgs e)
{
//插入代码(完全插入)

Form1 frm = new Form1();
frm.RefreshGrid();
this.Close();
}
}

我也尝试了Form2的FormClosing事件,不要做伎俩。



帮我解决问题!

解决方案

你的问题是你正在创建一个 Form1 的新实例。您需要将现有的 Form1 的实例传递给新的Form1 c $ c> Form2


I have Form1 and Form2.

Form1 has a dataGridView and a button for opening Form2. I have written a method in Form1 as below that perfectly refreshes the dataGridView:

public void RefreshGrid()
    {
        dataGridView1.DataSource = empControl.SelectAll(); //Works great
    }

In Form2 I insert into the table and use the below Code for Calling the above method. When I traced the code I saw that it implements all the way but the dataGridView isn't refreshed!

private void btnInsert_Click(object sender, EventArgs e)
    {
            //Insert Code (Inserts perfectly)

            Form1 frm = new Form1();
            frm.RefreshGrid();
            this.Close();
        }
    }

I also tried the FormClosing Event of Form2 but it didn't do the trick.

Help me out plz!

解决方案

Your problem is that you're creating a new instance of Form1. Instead of new Form1 you need to pass an instance of the existing Form1 to Form2

这篇关于从另一个表单刷新一个dataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆