在WinForms中更新BindingSource不会更新数据源集合 [英] Updating of BindingSource in WinForms does not update Datasource Collection

查看:574
本文介绍了在WinForms中更新BindingSource不会更新数据源集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Windows Forms应用程式的 DataGridView 中显示自订集合。这个自定义集合实现 ICollection IEnumerable 。我设置了一个 BindingSource ,使用该集合作为.DataSource属性。 DataGridView 设置为使用我的 BindingSource 作为它的DataSource。当我使用 BindingSource.Add()方法向集合添加一个新项目时, DataGridView 新物品。另一方面, BindingSource DataSource不会:

I want to display a custom collection in a DataGridView in a Windows Forms app. This custom collection implements ICollection, and IEnumerable. I have set up a BindingSource, using the collection as the .DataSource property. The DataGridView is set to use my BindingSource as it's DataSource. When I add a new item to the collection using the BindingSource.Add() method, the DataGridView updates correctly with the new item. The BindingSource DataSource, on the other hand, does not:

MyCustomCollection myCollection = new MyCustomCollection();

myCollection.Add(myCustomObject1);
myCollection.Add(myCustomObject2);

myBindingSource.DataSource(myCollection);
myBindingSource.Add(myCustomObject3);

在上面的代码中,myBindingSource的内部List包含正确的记录数(3) code> DataGridView 也包含三个记录,但myCollection只包含两个记录。我知道更改底层的myCollection不会更新 BindingSource DataGridView ,因为它不是一个 BindingList< T> ,但我的印象是直接更新 BindingSource 会确保myCollection同时更新。

In the above code, myBindingSource's internal List contains the right number of records (3), and the DataGridView also contains three records, but myCollection contains only two records. I know that changing the underlying myCollection will NOT update the BindingSource or the DataGridView, as it is not a BindingList<T>, but I was under the impression that updating a BindingSource directly would ensure that myCollection was updated at the same time.

有一种方法可以使用不是 BindingList< T> 的集合,直接与 BindingSource 交互?

Is there a way to use a collection that isn't a BindingList<T> and have it updated when interacting with the BindingSource directly?

更新:一种方式所有部分(Collection,BindingSource,DataGridView)更新的数据如下:

Update: One way I've gotten the data updated across all the parts (Collection, BindingSource, DataGridView) is as follows:

myCollection.Add(myCustomObject3);
myBindingSource.DataSource = null;
myBindingSource.DataSource = myCollection;

我敢肯定有更好的方法来攻击这个问题,但这是唯一的方法生成了我期待的结果。

I'm pretty sure there's a better way to attack this problem, but this is the only method that's generated the results I was expecting.

推荐答案

问题是填充适配器。加载表单时,会为您填写填充
只要确保做一个Refill,然后跟进重置绑定post任何数据更改和网格将刷新。

The problem is Fill Adaptor. When you load your form, the Fill is done for you. Just make sure to do a Refill and then follow up with Reset bindings post any data changes and Grid will get refreshed.

示例:

WorkTableAdapter.Insert(objData.XAttribute, "",
  objData.YAttribute,objLoanData.Amount_IsValid, DateTime.Now, DateTime.Now);
this.WorkTableAdapter.Fill(this.POCDataSet.Work);
this.WorkBindingSource.ResetBindings(false);

这篇关于在WinForms中更新BindingSource不会更新数据源集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆