从 wpf c# 中的不同形式刷新数据网格

发布于 2024-10-08 20:43:47 字数 119 浏览 1 评论 0原文

我目前正在使用 WPF 开发 C# 应用程序。我有两种形式。 Form1 有 DataGrid,我需要做的是从 form2 更新数据库,然后重新加载 form1 上的数据网格中的数据。

我该怎么办呢。非常感谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅黛梨妆こ 2024-10-15 20:43:47

您可以使用中间类通过事件交换通知。

示例:

public static class ApplicationEvents
{
    public static event EventHandler DataChanged;

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

现在,在 Form1Load 事件中,您可以注册到 DataChanged 事件。

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

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

这样,每当引发该事件时,Form1 就知道更新其 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 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 Loadevent, 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();
}
不回头走下去 2024-10-15 20:43:47

你有很多选择。

一种选择是在 Form1 上的集合中拥有内部/公共属性,该集合具有绑定到数据网格的数据。当您将记录存储到 Form2 上的数据库时,调用 Form1 上的属性并添加/删除记录,如果您有 ObservableCollection,那么它将自动从集合中添加或删除。如果更新记录,则需要在集合中找到它并更新值,如果每个属性都有 INotifyProperty,则 Form1 上的记录将被更新。

另一种选择是在 Form1 上使用公共/内部方法 UpdateCustomer,因此当您在 Form2 上保存记录时,您可以调用 Form1.UpdateCustomer(newCustomer) 并且 Form1 将处理新客户。

我个人喜欢在 Window1 UpateRecord(Customer UpdatedCustomer) 上有委托。这样,您将从任何窗口调用委托并将新值传递给 Window1,而不是调用属性或方法。这样任何表单都可以调用委托并将新记录传递给表单。

顺便说一句,如果您使用 MVVM,则委托/方法/属性应该位于 VM 上。

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.

少跟Wǒ拽 2024-10-15 20:43:47

一种方法是使用 MVP,

该模型保存数据库中的数据及其与数据库的通信。

将模型数据映射到视图的呈现器。

视图 - 数据的 UI 表示(即您的 form1 和 form2)

这两个视图应该共享相同的模型,该模型具有更新的数据(当 form 2 更新数据时,它实际上更新模型,模型然后更新数据库中的数据) ,两个视图的演示者(使用自定义绑定/事件观察模型数据的不同演示者)都会收到更新数据的通知,然后他们可以更新其视图(使用数据绑定)。

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 (i.e. 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文