简体   繁体   English

从其他表单上的按钮单击事件处理程序刷新控件或重新加载表单

[英]Refresh the control or reload form from button click event handler on other form

I have a GridView in Liste_Form and button in Close_Form. 我在Liste_Form中有一个GridView在Close_Form中有一个按钮。

How can I refresh the Gridview in Liste_Form, when i click the button on Close_Form? 当我单击Close_Form上的按钮时,如何在Liste_Form中刷新Gridview

Or reload the Liste_Form. 或重新加载Liste_Form。

You have a few ways this can be done. 您有几种方法可以完成此操作。 One way I might suggest is passing a delegate to the second form in the constructor: 我可能建议的一种方法是将委托传递给构造函数中的第二种形式:

Form2 myForm2 = new Form2(RefreshGrid); // assign to a Form2 local variable

...where in Form1 (the grid owner) you have defined the RefreshGrid method: ...在Form1(网格所有者)中的RefreshGrid定义了RefreshGrid方法:

void RefreshGrid(){
   // perform grid refresh
}

...so that in myForm2 you can execute the action when the button is clicked: ...以便在myForm2中单击按钮时可以执行操作:

void OnButtonClicked(object sender, EventArgs e){
   refreshAction();
}

Define event on Close_Form (of course consider better name for event): 在Close_Form上定义事件(当然,考虑使用更好的事件名称):

public event EventHandler SomethingHappened; 

Raise this event inside button click event handler: 在按钮单击事件处理程序中引发此事件:

private void Button_Click(object sender, EventArgs e)
{
    if (SomethingHappened != null)
        SomethingHappened(this, EventArgs.Empty);
}

Subscribe to this event on Liste_Form: 在Liste_Form上订阅此事件:

Close_Form closeForm = new Close_Form();
closeForm.SomethingHappened += Close_Form_SomethingHappened;

Refresh your list in this event handler: 在此事件处理程序中刷新列表:

private void Close_Form_SomethingHappened(object sender, EventArgs e)
{
    // refresh GridView
}

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

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