如何让 Windows 窗体 DataGridView 在绑定到 EntityCollection 时显示新记录

发布于 2024-10-14 07:11:33 字数 242 浏览 3 评论 0原文

尝试在运行时向 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 技术交流群。

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

发布评论

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

评论(3

叫嚣ゝ 2024-10-21 07:11:33

尝试一下:

bindingSource.DataSource = null;
bindingSource.DataSource = theCollection;

或者,您可以在 BindingList 中维护数据的内存副本。将 DataGridView 绑定到 BindingList,当您将实体添加到 ObjectSet 时,也将其添加到 BindingList 中。

Try that:

bindingSource.DataSource = null;
bindingSource.DataSource = theCollection;

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 the ObjectSet, add it to the BindingList too.

杀お生予夺 2024-10-21 07:11:33

我遇到了同样的问题。 Microsoft 应该关心使用其技术的人,而 EF 应该关心数据绑定。 Jaime,如果您找到更好的方法,请更新此列表。对我来说,重新创建上下文实例对我来说效果很好。有趣的是调试器显示实体上下文和实体上下文。绑定源有最新更新,但 datagridview 仍然不刷新。谢谢

这里到目前为止我找到的最好的解决方案 -

基本上你需要做

bindingSource.DataSource = EntityContext.Collection
                               .Execute(MergeOption.AppendOnly);

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

bindingSource.DataSource = EntityContext.Collection
                               .Execute(MergeOption.AppendOnly);
捶死心动 2024-10-21 07:11:33

我希望还不算太晚=)我有一些东西可以在这里使用......

// Entity Data Model
private ManagerEntities context = new ManagerEntities();

// declare private member    
private BindingList<Currency> lstCurrencies = null;

// on form load, load data and bind to DataGridView's DataSource

private void Form1_Load(object sender, EventArgs e) {

     lstCurrencies = new BindingList<Currency>();

     ObjectResult or = ((ObjectQuery)currencies).Execute(MergeOption.AppendOnly);
     foreach (Currency c in or)
         lstCurrencies.Add(c);

     // dgMain is my DataGridView
     dgMain.DataSource = lstCurrencies;
}

// this will save objects that have changed. You might want to add logic for newly created and deleted objects.
private void btnSave_Click(object sender, EventArgs e) {
    context.SaveChanges();
}

I hope it's not too late =) I have something that works here...

// Entity Data Model
private ManagerEntities context = new ManagerEntities();

// declare private member    
private BindingList<Currency> lstCurrencies = null;

// on form load, load data and bind to DataGridView's DataSource

private void Form1_Load(object sender, EventArgs e) {

     lstCurrencies = new BindingList<Currency>();

     ObjectResult or = ((ObjectQuery)currencies).Execute(MergeOption.AppendOnly);
     foreach (Currency c in or)
         lstCurrencies.Add(c);

     // dgMain is my DataGridView
     dgMain.DataSource = lstCurrencies;
}

// this will save objects that have changed. You might want to add logic for newly created and deleted objects.
private void btnSave_Click(object sender, EventArgs e) {
    context.SaveChanges();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文