C# 使用带有 bindinglist<business obj> 的 bindingsource 作为数据源有什么好处?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10266611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the benefits of using a bindingsource with bindinglist<business obj> as datasource?
提问by user774062
I can directly bind my DataGridView control to a bindinglist of my business objects by setting the DataSource property. My business object implements INotifyPropertyChanged, so the DGV gets updated when a new item is added to the Binding List or existing one is updated.
我可以通过设置 DataSource 属性将我的 DataGridView 控件直接绑定到我的业务对象的绑定列表。我的业务对象实现了 INotifyPropertyChanged,因此当将新项目添加到绑定列表或更新现有项目时,DGV 会得到更新。
With respect to dealing with single records, I can bind my business object to textboxes and other relevant controls.
关于处理单个记录,我可以将我的业务对象绑定到文本框和其他相关控件。
I can also derive from BindingList and create a CustomBindingList class to implement required methods of IBindable, as explained in the link below : http://msdn.microsoft.com/en-us/library/aa480736.aspx
我还可以从 BindingList 派生并创建一个 CustomBindingList 类来实现 IBindable 所需的方法,如下面的链接中所述:http: //msdn.microsoft.com/en-us/library/aa480736.aspx
Alternatively, I have seen people recommend using a BindingSource. BindingSource's Datasource is the business object and the DGV's DataSource is the BindingSource.
或者,我看到有人推荐使用 BindingSource。BindingSource 的 Datasource 是业务对象,而 DGV 的 DataSource 是 BindingSource。
In any case, basing it on a BindingSource does not offer me:
无论如何,基于 BindingSource 不会为我提供:
- Filtering (Filter does not work). Implementation needs to be provided by me.
- Sort and Search does not work. Implementation needs to be provided by me.
- 过滤(过滤器不起作用)。实现需要由我提供。
- 排序和搜索不起作用。实现需要由我提供。
So, Why is the BindingSource approach recommended?
那么,为什么推荐 BindingSource 方法?
Broader Picture: Am new to OOPS concepts and C#. Working with Database applications. Winforms. So far have only used DataSet / DataTable approach. Now trying to create and use my own custom classes.
更广泛的画面:我是 OOPS 概念和 C# 的新手。使用数据库应用程序。Winforms。到目前为止只使用过DataSet/DataTable 的方法。现在尝试创建和使用我自己的自定义类。
Typically have a Master/Detail form. When I click on a Detail row in the DGV, I want to edit that record in a separate window. So I need to get a handle on the list item represented by that row in the DGV. Trying to find a solution for that has brought me to this point and this doubt.
通常有一个主/明细表。当我单击 DGV 中的详细信息行时,我想在单独的窗口中编辑该记录。因此,我需要获取 DGV 中该行表示的列表项的句柄。试图为此找到解决方案让我想到了这一点和这个疑问。
Given what I want to do, which approach is better and why?
鉴于我想做什么,哪种方法更好,为什么?
Some pointers here would really help as I am very new to this.
这里的一些指针真的会有所帮助,因为我对此很陌生。
回答by Martin
It is recommended to use a BindingSource when multiple controls on the form use the same datasource (Behind the Scenes: Improvements to Windows Forms Data Binding)
当窗体上的多个控件使用相同的数据源时,建议使用 BindingSource(幕后:Windows 窗体数据绑定的改进)
Design-time: I personally find the BindingSource very helpfull when choosing the properties from my business object when databinding to controls.
设计时:当数据绑定到控件时,从我的业务对象中选择属性时,我个人发现 BindingSource 非常有用。
To get a handle to the currently selected row, try bindingSource1.Current as MyBusinessObject;
要获取当前选定行的句柄,请尝试 bindingSource1.Current as MyBusinessObject;
As for filtering and searching: I use a third party dll for grids that have that implemented. So can't help you with that, sorry.
至于过滤和搜索:我将第三方 dll 用于已实现的网格。所以帮不了你了,抱歉。
When you work with lists of different types of business objects, don't use the list directly
当您使用不同类型业务对象的列表时,不要直接使用列表
List<IAnimal> animals = new List<IAnimal>();
animals.Add(new Cat());
animals.Add(new Dog());
bindingSource1.DataSource = animals;
Instead use a BindingList like this:
而是使用像这样的 BindingList:
bindingSource1.DataSource = new BindingList<IAnimal>(animals);
That will make sure all accessed objects in the list are of type IAnimal and saves you some exceptions.
这将确保列表中所有访问的对象都是 IAnimal 类型,并为您节省一些例外。
回答by Dirk Bester
Binding to a DataSource could give you benefits when dealing with a large set only a part of which gets displayed. For instance if you look at the Telerik ListView here http://www.telerik.com/help/winforms/listview-databinding.html(there are many of these component packages, this is just the latest one I am using bits and pieces from).
在处理仅显示一部分的大型集合时,绑定到 DataSource 可为您带来好处。例如,如果您在此处查看Telerik ListView http://www.telerik.com/help/winforms/listview-databinding.html(有许多这些组件包,这只是我正在使用的最新一个从)。
The view is very lightweight and lets your scroll position determine which objects need to be actually displayed. So if you only look at the first 10 objects and never scroll down only 10 get bound and displayed. This potentially avoids a lot of unneeded data access.
该视图非常轻巧,可以让您的滚动位置确定需要实际显示哪些对象。因此,如果您只查看前 10 个对象并且从不向下滚动,则只有 10 个会被绑定并显示。这可能避免了大量不必要的数据访问。
Their GridView functions in the same manner. There is the displayed part of the grid which is separate from the potentially huge underlying grid.
它们的 GridView 以相同的方式运行。网格的显示部分与潜在的巨大底层网格分开。
As a bonus, you get filtering, sorting, grouping.
作为奖励,您可以获得过滤、排序和分组。
回答by Alejandro del Río
As far as I know, if you are working with a database, you use a bindingSourcein the middle in order to establish a bilateral bridge between the database and your control. Otherwiseyou can just use a bindingListas source for your control.
据我所知,如果您使用的是数据库,则在中间使用bindingSource以在数据库和控件之间建立双边桥梁。否则,您可以只使用bindingList作为控件的源。

