关于c#:在Windows窗体中刷新DataGridView | 珊瑚贝

Refresh DataGridView in Windows form


我有两个表单,分别是表单 A 和表单 B。当我单击表单 B 上的保存按钮时,我希望表单 A 的 DataGridView 刷新。

我应该使用哪种方法?

  • 您无法调用任何单一方法来实现此目的,但其想法是触发一个事件,该事件将在表单 A 中执行必要的操作。


使用事件是执行此操作的一种方式。下面是另一种更面向对象的方式。

在FormA中添加公共刷新方法。

1
2
3
4
public void RefreshDataGrid()    
{      
   //Do refresh    
}

在构造 FormB 时将 FormA 的实例传递给 FormB。您必须创建 FormB 构造函数来获取 FormA 实例。

1
2
3
4
5
    private FormA myFormA;        
    public FormB(FormA formA)        
    {        
        myFormA = formA;        
    }

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

1
myFormA.RefreshGrid();

在Form A中实现代码如下:

1
2
3
4
5
private delegate void DEmpty();
public void RefreshDataGrid()
{
   this.Invoke(new DEmpty(datagrid.Refresh));
}

然后在 B 上单击按钮时调用它

  • 我在调用时拼错了 DEmpty,现在它已修复
  • REfreshDataGrid 没有出现在 B 中。为什么?
  • 因为你必须获得对A的引用,然后调用函数 RefreshDataGrid 像这样 A _aref; _aref.RefreshDataGrid(); 更改B的构造函数,使其参数之一是A类型的var,然后在ctor内部将值分配给本地var


创建一个绑定gridview的方法,在表单A的表单加载时调用该方法,如果表单已经打开,则必须使用它的实例(表单A),并调用与表单A相同的绑定方法用于 gridview 绑定。


来源:https://www.codenong.com/6621615/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9174.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?