Convert generic list to BindingList<T>

点点圈 提交于 2020-06-27 07:49:11

问题


I have BindingList object which is attched with DataGridView.

BindingList<FilesToProcessDataModels> Listfiles = new BindingList<FilesToProcessDataModels>();
dataGridFiles.DataSource = Listfiles;

I want to filter items list by given condition in Where. like following:

dataGridSheets.DataSource = Listfiles.Where(i => i.Status == FileStatus.NotProcessed).ToList();

Above code is working fine but I want to assign filter resut to the same type object [Listfiles] instead datagrid,

When I am doing this:

Listfiles = Listfiles.Where(i => i.Status == FileStatus.NotProcessed).ToList();

it gives following erorr

[Cannot implicitly convert type 'System.Collections.Generic.List to BindingList']

How can I convert generic list to BindingList<FilesToProcessDataModels>?


回答1:


below is the standard way - tho google should show this up easily -even on SO:

Convert IList<T> to BindingList<T>

var listFiles= new List<FilesToProcessDataModels>();
var listBindingFiles = new BindingList<FilesToProcessDataModels>(listFiles);



回答2:


Like this:

var yourList = new List<FilesToProcessDataModels>();
var listBinding = new BindingList<FilesToProcessDataModels>(yourList);



回答3:


Listfiles = Listfiles.Where(i => i.Status == FileStatus.NotProcessed).ToList();

I think you had defined ListFiles as BindingList<> bit now you are trying to assign a List<> to it.

That is the cause of that error



来源:https://stackoverflow.com/questions/20398832/convert-generic-list-to-bindinglistt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!