WinForms DataBinding 是否需要 BindingSource 和 BindingList?
我想在 Windows 窗体应用程序的 DataGridView
中显示人员列表。我希望我的服务层返回 Person 对象的列表(例如,IList
)。我希望列表中的更改反映在 DataGridView 中,反之亦然。我的理解是,使用 BindingSource
有助于使用 DataGridView
。我的问题是为了使双向数据绑定工作,我是否需要:
//pseudo code
BindingSource.DataSource = IBindingList<Person>
或者我可以做什么:
BindingSource.DataSource = IList<Person>
有什么区别?如果我对列表进行更改,DataGridView
是否会以任何方式更新?如果我必须使用 BindingList
,从我的服务层返回 BindingList
似乎有点奇怪(因为创建依赖项),有没有办法解决这个问题?
Microsoft 谈到 BindingList
(在“备注”部分) http://msdn.microsoft.com/en-us/library/ms132679.aspx :
“但是,典型的解决方案 程序员将使用一个类 提供数据绑定功能, 例如
BindingSource
,而不是 直接使用BindingList
。”
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 asBindingSource
, instead of
directly usingBindingList<T>
."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
绑定到
IList
只会为您提供单向绑定;对列表或列表项的更改不会反映在DataGridView
中。您可以使用BindingList
或BindingSource
来获取此功能,但您的Person
类仍需要支持INotifyPropertyChanged
否则,您只会在向列表添加项目或从列表中删除项目时获得同步,而不是在列表项目本身发生更改时获得同步。如果您想避免对 System.Windows.Forms 的依赖,可以使用 ObservableCollection来代替;这支持必要的更改通知,因此可以用作双向绑定源。
Binding to an
IList<Person>
will only give you one-way binding; changes to the list or list items will not be reflected in theDataGridView
. You can use aBindingList
orBindingSource
to get this functionality instead, but yourPerson
class will still need to supportINotifyPropertyChanged
or else you will only get synchronisation when items are added/removed to/from the list, not when the list items themselves change.If you want to avoid a dependency on
System.Windows.Forms
, you could useObservableCollection<Person>
instead; this supports the necessary change notifications and can therefore be used as a two-way binding source.如果您使用
BindingList
,那么您通过基础列表所做的更改将反映在数据绑定控件中,因为 BindingList 在列表更改时会引发一个事件。大多数其他集合都没有。如果您使用普通集合作为数据源,那么您通过其他数据绑定控件(或通过 BindingSource)所做的更改仍将被反映,但直接对基础集合所做的更改将不会反映。
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.