从 wpf c# 中的不同形式刷新数据网格
我目前正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用中间类通过事件交换通知。
示例:
现在,在
Form1
的Load
事件中,您可以注册到DataChanged
事件。这样,每当引发该事件时,
Form1
就知道更新其DataGrid
:You could use an intermediate class to exchange notifications using events.
Example:
Now, in
Form1
'sLoad
event, you could register toDataChanged
event.So that whenever that event is raised,
Form1
knows to update itsDataGrid
:你有很多选择。
一种选择是在 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.
一种方法是使用 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).