简体   繁体   中英

C# Refresh DataGridView with another Form

I have a problem with not being able to refresh my form that has a DataGridView . I open a form called 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. 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. 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?

Thanks in advance

Thanks to the user JDB i have fixed my problem.

Instead of making a new instance (Thank you Junaith for correcting me for it) I'm now making a LogicalParent method.

public AdminForm LogicalParent { get; set; }

With this i have access to al methods and functions of my parent Form and no problems with refreshing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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