在Windows窗体刷新的DataGridView窗体、Windows、DataGridView

2023-09-02 21:39:10 作者:青年你好帅!

我有两种形式让它成为A型和B,当我点击保存按钮表BI希望A型的DataGridView的刷新。

I have two forms let it be Form A and Form B. When I click save button on Form B I want the DataGridView of Form A to refresh.

我应该使用哪种方法?

推荐答案

使用的事件是这样的一种方式。下面是另一种方式是更多的面向对象的。

Using a event is one way of doing this. Below is another way which is more object oriented.

在形式上加入公共Refresh方法。

Add public Refresh method in FormA.

public void RefreshDataGrid()     
{       
   //Do refresh    
}

传递备考的实例构造FormB时FormB。你必须创建FormB构造器采取形式上的实例。

Pass the instance of FormA to FormB when constructing FormB. You have to create FormB contructor to take FormA instance.

    private FormA myFormA;        
    public FormB(FormA formA)        
    {        
        myFormA = formA;        
    }

现在你可以从FormB调用FormA.ResfreshGrid()方法。

Now you can call FormA.ResfreshGrid() method from FormB.

myFormA.RefreshGrid();