我需要的BindingSource和数据绑定的WinForms一个的BindingList? [英] Do I need a BindingSource AND a BindingList for WinForms DataBinding?
问题描述
我想在 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 theDataGridView
and vice versa. My understanding is that using theBindingSource
facilitates working withDataGridView
. 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 theBindingList
, it seems a little wonky (because of creating a dependency) to return aBindingList
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 usingBindingList<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屋!