简体   繁体   English

以 Windows 形式刷新 DataGridView

[英]Refresh DataGridView in Windows form

I have two forms let it be Form A and Form B. When I click save button on Form BI want the DataGridView of Form A to refresh.我有两个 forms 让它成为表格 A 和表格 B。当我单击表格 BI 上的保存按钮时,希望表格 A 的 DataGridView 刷新。

Which method should I use?我应该使用哪种方法?

Using a event is one way of doing this.使用事件是执行此操作的一种方式。 Below is another way which is more object oriented.下面是另一种更面向 object 的方式。

Add public Refresh method in FormA.在 FormA 中添加公共 Refresh 方法。

public void RefreshDataGrid()     
{       
   //Do refresh    
}

Pass the instance of FormA to FormB when constructing FormB.构造 FormB 时将 FormA 的实例传递给 FormB。 You have to create FormB contructor to take FormA instance.您必须创建 FormB 构造函数来获取 FormA 实例。

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

Now you can call FormA.ResfreshGrid() method from FormB.现在您可以从 FormB 调用 FormA.ResfreshGrid() 方法。

myFormA.RefreshGrid();

implement code in Form A like this:像这样在 Form A 中实现代码:

private delegate void DEmpty();
public void RefreshDataGrid()
{
   this.Invoke(new DEmpty(datagrid.Refresh));
}

then call this when button is clicked on B然后在 B 上单击按钮时调用它

Create a method for binding the gridview, call this method on form load of form A, and if the form is already opened, you have to use its instance (of form A), and call the same binding method of Form A for gridview binding.创建一个绑定gridview的方法,在表单A的表单加载时调用该方法,如果表单已经打开,则必须使用它的实例(表单A),并调用与表单A相同的绑定方法进行gridview绑定.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM