简体   繁体   English

如何刷新另一个表格?

[英]How to refresh a form from another?

I'm working with windows form, in one form a list the data of a table and in the other one, I add the data. 我正在使用Windows窗体,在一种窗体中列出表的数据,在另一种窗体中添加数据。 In the form that I list, I have the the form load to set the values of the table in a datagridview. 在我列出的表单中,我有表单负载来设置datagridview中的表的值。 So I want that when I click save in the saving form, I reload the other form where I list the data. 因此,我希望在单击保存表单中的“保存”时,重新加载列出数据的其他表单。 I've tried something like: 我已经尝试过类似的东西:

form.refresh

but doesn't work. 但不起作用。 I tried closing the list form when clicking add, and then when I clicked save it would show up again, that worked, but is there other way I can do it? 我尝试在单击“添加”时关闭列表表单,然后在单击“保存”时再次显示该列表表单,这种方法有效,但是还有其他方法可以实现吗?

Here is my code: 这是我的代码:

List form: 清单形式:

 private void ListadoExpedientes_Load(object sender, EventArgs e)
 {
      dgvExpedientes.AutoGenerateColumns = false;
      Exp = ExpedienteNG.GetExpedientes();
      bExpedientes = new BindingList<Expediente>(Exp);
      dgvExpedientes.DataSource = bExpedientes;
 }

and here is the save form: 这是保存表单:

private void btnGuardar_Click(object sender, EventArgs e)
{
      ListadoExpedientes listexp = new ListadoExpedientes();
      listexp.Refresh();
}

Class(Form) - Form2 (ex.) that holds the method that requires update in base form - Form1 Class(Form)-Form2(例如),其中包含需要以基本形式进行更新的方法-Form1

In Form2 define constructor and a variable that holds a Form1 object 在Form2中定义构造函数和一个保存Form1对象的变量

  Form1 frm1;
    public Form2(Form1 _frm1){ 
       InitializeComponent();
       ...
       this.frm1 = _frm1;
       this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.RefreshDatagrid);
    }

In Form1 在Form1中

     public void RefreshDataGrid()
            {
                dgvExpedientes.DataSource = null;
                dgvExpedientes.DataSource = bExpedientes;
            }

private void OpenForm2_Click(object sender, EventArgs e)
        {
            Form2 = new Form2 (this);
            t.ShowDialog();
        }

This code will update the dgvExpedientes in form1 , when you close the form2, so the event is triggered after you finish uploading records and close the form2. 当您关闭form2时,此代码将更新form1中的dgvExpedientes,因此在您完成上载记录并关闭form2后会触发该事件。 I hope this will help in your situation 希望这对您有帮助

You can try to use Application.OpenForms collection to find your open forms. 您可以尝试使用Application.OpenForms集合查找打开的表单。 then call refresh from there. 然后从那里致电刷新。 Example: 例:

var listForm = Application.OpenForms.Cast<Form>().Where(x => x.Name == "ListadoExpedientes").FirstOrDefault();
if (listForm != null)
{
    listForm.Refresh();
}

You might need to tweek some things, because I didn't test this. 您可能需要花一些时间,因为我没有对此进行测试。

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

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