简体   繁体   English

绑定列表 <T> 在C#中使用datagridview

[英]Binding List<T> with datagridview in C#

I would like to know what is the difference between binding list directly to datagridview datasource and binding Bindingsource to datagridview. 我想知道绑定列表直接与datagridview数据源和绑定Bindingsource到datagridview有什么区别。 Here is example: 这是一个例子:

Ex1: EX1:

List<Employee> list = this.GetEmployees();
dgvEmployees.Datasource = list;

Ex2: EX2:

List<Employee> list = this.GetEmployees();
BindingSource soure = new BindingSource();
soure.Datasource = list;
dgvEmployees.Datasource = soure;

So, which one should be the good practice? 那么,哪一个应该是好的做法?

In general, using Datasource directly is for simple cases and using a BindingSource is for more complex cases. 通常,直接使用Datasource是为了简单的情况,使用BindingSource是为了更复杂的情况。 When all you want to do is display data and don't really care about modifying it before it's displayed, feel free to use Datasource 当您想要做的只是显示数据并且在显示数据之前并不真正关心修改数据时,请随意使用Datasource

BindingSource on the other hand, allows you to (not an exhaustive list): 另一方面, BindingSource允许您(不是详尽的列表):

  • Specify a Format method to transform the list data before the user sees it, and a Parse method to transform the list data back after the user edits it 指定Format方法以在用户看到列表数据之前对其进行转换,并使用Parse方法在用户编辑列表数据后将其转换回来
  • Track the Current (selected) item in the list 跟踪列表中的当前 (选定)项目
  • Customize the way a new element is added 自定义添加新元素的方式
  • Prevent edits to the current item 防止编辑当前项目
  • Be notified when an element is added or removed 在添加或删除元素时收到通知

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM