简体   繁体   English

C#用另一种形式刷新DataGridView

[英]C# Refresh DataGridView with another Form

I have a problem with not being able to refresh my form that has a DataGridView . 我无法刷新具有DataGridView表单时遇到问题。 I open a form called MaintenanceForm. 我打开一个名为MaintenanceForm的表单。 Here I will choose a car, give the amount of km, and the option to add products. 在这里,我将选择一辆车,给出公里数,并添加产品。 If I click on the add products button, this form will stay open while another will open as well called AddProducts. 如果我单击添加产品按钮,则此表单将保持打开状态,而另一个表单将被称为AddProducts。 In this form I will choose from a list of products that I will add to my final listbox. 在此表单中,我将从将添加到最终列表框中的产品列表中进行选择。 If I click Save, these items will go to my BindingList and populate my grid. 如果单击“保存”,这些项目将转到我的BindingList并填充我的网格。 I have tested this with closing my first form first and reopening it with my second form. 我已经通过先关闭第一个表单并用第二个表单重新打开进行了测试。 The grid was populated. 网格已填充。 How do I populate my grid without having to close my first form? 如何在不关闭第一个表单的情况下填充网格? Here are the methods I'm using 这是我正在使用的方法

Save button on my second form: 在第二个表单上保存按钮:

private void btnOpslaan_Click(object sender, EventArgs e)
{
    lstTotal = new BindingList<Product>();
    foreach (object product in listBtotal.Items)
    {
        lstTotal.Add((Product)product);

    }
    MaintenanceForm maintenanceForm = new MaintenanceForm();
    maintenanceForm.FillDataGridView(lstTotal);
    this.Close();
}

Method to populate my grid: 填充网格的方法:

public void FillDataGridView(BindingList<Product> products)
{
    dGvProducts.DataSource = null;
    dGvProducts.AutoGenerateColumns = false;
    dGvProducts.AllowUserToAddRows = false;
    dGvProducts.DataSource = products;
    dGvProducts.Refresh();

}

Again the MaintenanceForm is still open while AddProductsForm is open? 再次打开AddProductsForm时,MaintenanceForm仍然打开吗?

Thanks in advance 提前致谢

Thanks to the user JDB i have fixed my problem. 感谢用户JDB,我解决了我的问题。

Instead of making a new instance (Thank you Junaith for correcting me for it) I'm now making a LogicalParent method. 而不是创建一个新实例(感谢Junaith对其进行纠正),我现在正在制作一个LogicalParent方法。

public AdminForm LogicalParent { get; 公共AdminForm LogicalParent {get; set; 组; } }

With this i have access to al methods and functions of my parent Form and no problems with refreshing. 这样,我就可以访问父窗体的所有方法和功能,并且刷新没有问题。

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

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