我需要的BindingSource和数据绑定的WinForms一个的BindingList? [英] Do I need a BindingSource AND a BindingList for WinForms DataBinding?

查看:178
本文介绍了我需要的BindingSource和数据绑定的WinForms一个的BindingList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 DataGridView的在Windows中显示的人的名单窗体应用程序。我希望我的服务层返回Person对象的列表(例如,的IList<&人GT; )。我想在名单变化反映在 DataGridView的,反之亦然。我的理解是,使用的BindingSource 促进与 DataGridView的工作。我的问题是对于数据绑定的双向工作,我需要:

  //伪代码
的BindingSource。数据源= IBindingList的<&人GT;

或能做些什么:

  BindingSource.DataSource = IList的<&人GT; 



有什么区别?如果我更改了名单将在 DataGridView的可无论哪种方式更新?如果我不得不使用的BindingList ,似乎有点靠不住一个的BindingList 从我的服务层,有没有办法解决?



微软称的的BindingList (在备注部分)
http://msdn.microsoft.com/en-us/library/ms132679。 ASPX :在




然而,典型的解决方案
程序员将使用
提供的数据的类绑定功能,
,如的BindingSource ,而不是
直接使用的BindingList< T>



解决方案

如果您使用的BindingList< T> 然后更改您通过下面的列表中进行将在绑定控件中的数据中反映出来,因为当列表被改变的BindingList引发的事件。大多数其他藏品不知道。



如果您使用的是正常的集合作为数据源,然后更改了您通过绑定控件的其他数据(或通过的BindingSource)仍然会反映,但更改基础集合直接不会的。


I want to display a list of people in a DataGridView in a Windows Forms app. I want my service layer to return a list of Person objects (e.g., IList<Person>). I want changes in the list to be reflected in the DataGridView and vice versa. My understanding is that using the BindingSource facilitates working with DataGridView. My question is for the two-way databinding to work, do I need:

//pseudo code
BindingSource.DataSource = IBindingList<Person>

or can I do:

BindingSource.DataSource = IList<Person>

What's the difference? If my make changes to the list will the DataGridView be updated either way? And if I have to use the BindingList, it seems a little wonky (because of creating a dependency) to return a BindingList from my service layer, is there a way around that?

Microsoft says of the BindingList (in the Remarks section) http://msdn.microsoft.com/en-us/library/ms132679.aspx:

"However, the typical solutions programmer will use a class that provides data binding functionality, such as BindingSource, instead of directly using BindingList<T>."

解决方案

If you use BindingList<T> then changes that you make through the underlying list will be reflected in the data bound controls because BindingList raises an event when the list is changed. Most other collections do not.

If you use a normal collection as the data source then changes that you make through other data bound controls (or through the BindingSource) will still be reflected, but changes to the underlying collection directly will not.

这篇关于我需要的BindingSource和数据绑定的WinForms一个的BindingList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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