我需要一个BindingSource和一个绑定列表为WinForms DataBinding? [英] Do I need a BindingSource AND a BindingList for WinForms DataBinding?

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

问题描述

我想在Windows窗体应用程序中显示 DataGridView 中的人员列表。我希望我的服务层返回Person对象列表(例如, IList< Person> )。我希望列表中的更改反映在 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?

微软说的 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< T>


推荐答案

如果您使用 BindingList& T> 然后通过底层列表进行的更改将反映在数据绑定控件中,因为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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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