简体   繁体   中英

Refreshing datagrid from a different form in wpf c#

I am currently developing a C# application using WPF. I have 2 forms. Form1 has the DataGrid and what I need to be able to do is from form2 update the database and then reload the data in the datagrid on form1.

How can I do this. Thanks very much

You could use an intermediate class to exchange notifications using events.

Example:

public static class ApplicationEvents
{
    public static event EventHandler DataChanged;

    public static void NotifyDataChanged()
    {
        EventHandler temp = DataChanged;
        if (temp != null)
        {
            temp(null, EventArgs.Empty);
        }
    }
}

Now, in Form1 's Load event, you could register to DataChanged event.

void Form1_Load()
{
    ApplicationEvents.DataChanged += new EventHandler(ApplicationEvents_DataChanged);
}

void ApplicationEvents_DataChanged(object sender, EventArgs e)
{
    // Write code to update DataGrid
}

So that whenever that event is raised, Form1 knows to update its DataGrid :

// Suppose in Form2, on a button click you want Form1 to update its DataGrid
// You just need to call NotifyDataChanged() method
void Form2_Button1_Click()
{
    ApplicationEvents.NotifyDataChanged();
}

You have many options.

One option would be to have internal/public property with your collection on Form1 that has data that bound to the datagrid. When you store record to the database on Form2, call property on Form1 and add/remove record and if you have ObservableCollection then it will be automatically added or removed from collection. If you update record, you will need to find it in a collection and update values and if you have INotifyProperty on each property then record on your Form1 will be updated.

Another option is to have public/internal method UpdateCustomer on Form1, so when you save record on Form2 you call Form1.UpdateCustomer(newCustomer) and Form1 will take care of new Customer.

I personally like to have delegate on Window1 UpateRecord(Customer updatedCustomer). This way instead of calling property or method you will call delegate from any window and pass new value to the Window1. This way any Form can call delegate and pass new record to the form.

BTW if you are using MVVM that delegate/method/property should be on VM.

One way would be if you go by MVP,

The model which holds the data from the database and its communication with database.

The presenter which maps the model data to the view.

The view - UI representation of the data (ie ur form1 and form2)

Both these views should share the same model, which has the updated data (when form 2 updates the data it actually updates the model and model then updates the data in database), the presenters for both the view (different presenters which observe model data using custom binding/events) are notified of the data updated and then they can update their views (using databinding).

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