简体   繁体   English

Datagrid不会刷新已更改的数据

[英]Datagrid doesn't refresh on changed data

Is there any way to have a datagrid listen to the database and automatically update the data if the database data is changed? 有没有办法让datagrid监听数据库并在数据库数据发生变化时自动更新数据? I use a SQL Server database. 我使用SQL Server数据库。

I'd like to use Linq-2-SQL if possible 如果可能的话,我想使用Linq-2-SQL

Because @Slaggg asked: there are fairly straightforward ways of doing this, but they're almost certainly going to involve a lot of coding, it'll impact performance pretty significantly, and my strong suspicion is that it'll be more trouble than it's worth. 因为@Slaggg问:有这么简单的方法可以做到这一点,但它们几乎肯定会涉及大量编码,它会对性能产生相当大的影响,而且我强烈怀疑它会比它更麻烦。价值。

That said, for a typical n-tier application, at a very high level you'll need: 也就是说,对于典型的n层应用程序,您需要处于非常高的级别:

(1) A way to notify the middle tier when the data changes. (1)数据发生变化时通知中间层的方法。 You could use custom-code triggers inside each the table that fire off some sort of notification (perhaps using WCF and CLR stored procedures), or you could use a SqlDependency object. 您可以在每个表中使用自定义代码触发器来触发某种通知(可能使用WCF和CLR存储过程),或者您可以使用SqlDependency对象。 Probably the second would work better. 可能第二个会更好。

(2) A way to notify each client connected to that middle tier. (2)通知连接到该中间层的每个客户端的方法。 Assuming that you're using WCF, you'll need to use one of the duplex bindings that are available, such as Net.TCP or HttpPollingDuplex (for Silverlight). 假设您正在使用WCF,则需要使用其中一个可用的双工绑定,例如Net.TCP或HttpPollingDuplex(适用于Silverlight)。 You'll need to make sure this is configured correctly on both the client and the server. 您需要确保在客户端和服务器上都正确配置了此项。 You'll also need to manually keep track of which clients might be interested in the update, so that you can know which ones to update, and you'll need to be able to remove them from that list when they go away or timeout. 您还需要手动跟踪哪些客户端可能对更新感兴趣,以便您可以知道要更新哪些客户端,并且当它们消失或超时时您需要能够从该列表中删除它们。 Tomek, from the MS WCF team, has some pretty good examples on his blog that you might want to investigate. 来自MS WCF团队的Tomek在他的博客上有一些很好的例子,你可能想调查一下。

(3) A mechanism to update the local client's model and/or viewmodel and/or UI once you get the notification from the middle tier that something has changed. (3)一种机制,一旦您从中间层获得某些更改的通知,就会更新本地客户端的模型和/或视图模型和/或UI。 This is more complicated than you'd think: it's difficult enough to keep your UI in sync with your data model under normal circumstances, but it gets dramatically more complicated when that data model can be changing under you from the other direction as well. 这比你想象的要复杂得多:在正常情况下让你的UI与你的数据模型保持同步是很困难的,但是当数据模型也可以从你的另一个方向改变时,它会变得非常复杂。

The idea behind these sorts of notifications is straightforward enough: but getting all the details right is likely to keep you debugging way into the night. 这些通知背后的想法很简单:但是正确地获取所有细节可能会让你调试到深夜。 And the guy who has to support all this two years from now will curse your name. 从现在开始这两年不得不支持的人会诅咒你的名字。

Hope this helps. 希望这可以帮助。

It depends from where you are updating the database: 这取决于您更新数据库的位置:

  • From the same context (in Silverlight, are you adding, deleting, editing on the same page) 从相同的上下文(在Silverlight中,您是否在同一页面上添加,删除,编辑)
  • From a ChildWindow in your Silverlight application 来自Silverlight应用程序中的ChildWindow
  • From an external, non-related tool, outside of your Silverlight application 来自Silverlight应用程序之外的外部非相关工具

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

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