如何让 Windows 窗体 DataGridView 在绑定到 EntityCollection 时显示新记录
尝试在运行时向 EntityCollection 添加新记录,并使用新信息更新 DataGridView。
我尝试将 datagridview 直接绑定到实体集合(即 ObjectSet),并通过绑定到同一集合的 BindingSource 进行绑定。
我尝试过 DataGridView.Refresh()、DataGridView.EndEdit() 和 BindSource.ResetBindings() 等,但似乎没有任何效果。
Trying to add new records to the EntityCollection at runtime and have the the DataGridView update with the new information.
I have tried bind the datagridview directly to the entity collection (i.e. ObjectSet) and through a BindingSource that is bound to the same collection.
I have tried DataGridView.Refresh(), DataGridView.EndEdit(), and BindSource.ResetBindings() among other things, but nothing seems to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试一下:
或者,您可以在
BindingList
中维护数据的内存副本。将 DataGridView 绑定到 BindingList,当您将实体添加到ObjectSet
时,也将其添加到BindingList
中。Try that:
Alternatively, you could maintain a in-memory copy of the data in a
BindingList<T>
. Bind the DataGridView to the BindingList, and when you add an entity to theObjectSet
, add it to theBindingList
too.我遇到了同样的问题。 Microsoft 应该关心使用其技术的人,而 EF 应该关心数据绑定。 Jaime,如果您找到更好的方法,请更新此列表。对我来说,重新创建上下文实例对我来说效果很好。有趣的是调试器显示实体上下文和实体上下文。绑定源有最新更新,但 datagridview 仍然不刷新。谢谢
这里到目前为止我找到的最好的解决方案 -
基本上你需要做
I am stuck with same problem. Microsoft should care about the people who use their technology and EF should care about data binding. Jaime, if you find a better way, please update this list. For me, recreating the context instance works fine for me. The funny thing is debugger shows that entity context & binding source has latest updates but datagridview still doesn't refresh. Thanks
Here is the best solution I have found so far -
Basically you need to do
我希望还不算太晚=)我有一些东西可以在这里使用......
I hope it's not too late =) I have something that works here...