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

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

问题描述

我想在 Windows 窗体应用程序的 DataGridView 中显示人员列表.我希望我的服务层返回一个 Person 对象列表(例如,IList).我希望列表中的更改反映在 DataGridView 中,反之亦然.我的理解是使用 BindingSource 有助于使用 DataGridView.我的问题是让双向数据绑定工作,我需要:

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>

或者我可以这样做:

BindingSource.DataSource = IList<Person>

有什么区别?如果我对列表进行更改,DataGridView 会以任何一种方式更新吗?如果我必须使用 BindingList,从我的服务层返回 BindingList 似乎有点不稳定(因为创建依赖项),有没有办法解决这个问题?

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 谈到 BindingList(在备注部分)http://msdn.microsoft.com/en-us/library/ms132679.aspx:

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

"但是,典型的解决方案程序员将使用一个类提供数据绑定功能,比如BindingSource,而不是直接使用BindingList."

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

推荐答案

如果您使用 BindingList 那么您通过底层列表所做的更改将反映在数据绑定控件中,因为 BindingList当列表更改时引发事件.大多数其他集合没有.

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.

如果您使用普通集合作为数据源,那么您通过其他数据绑定控件(或通过 BindingSource)所做的更改仍将反映,但不会直接反映对基础集合的更改.

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 DataBinding 的 BindingList 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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