C# DataGridView 使用对象列表作为数据源过滤 BindingSource

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10074032/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:21:09  来源:igfitidea点击:

DataGridView Filter a BindingSource with a List of object as DataSource

c#winforms

提问by Müsli

I'm trying to filter a BindingSource with a BindingList as Datasource. I tried BindingSource.Filter = 'Text Condition' But it didn't work, nothing happens, the data on screen remains the same. But if i use a DataSet as the datasource it works. Is It possible to filter a list of objects with the BindingSource.Filter property?

我正在尝试使用 BindingList 作为数据源过滤 BindingSource。我试过 BindingSource.Filter = 'Text Condition' 但它没有用,没有任何反应,屏幕上的数据保持不变。但是如果我使用 DataSet 作为数据源,它就可以工作。是否可以使用 BindingSource.Filter 属性过滤对象列表?

I have the following class:

我有以下课程:

class Person
        {
            public String Nombre { get; set; }
            public String Apellido { get; set; }
            public int DNI { get; set; }
            public int Edad { get; set; }
            public Decimal Tamano { get; set; }
        }

This is how i use it:

这是我如何使用它:

BindingList<Person> personas = new BindingList<Person> { 
                new Person{ Apellido = "App1", DNI = 3011, Edad = 20, Nombre ="Name1", Tamano = new decimal(1.7)}
                ,new Person{ Apellido = "App2", DNI = 1520, Edad = 30, Nombre ="Name2", Tamano = new decimal(1.5)}
                ,new Person{ Apellido = "App3", DNI = 5654, Edad = 21, Nombre ="Name3", Tamano = new decimal(1.6)}
                ,new Person{ Apellido = "App4", DNI = 778, Edad = 40, Nombre ="Name4", Tamano = new decimal(1.68)}
            };

            BindingSource bs = new BindingSource();
            bs.DataSource = personas;
            grid.DataSource = bs;

            bs.Filter = "Apellido like 'App1'";

This is just an example the idea is to test if a can filter a data source like that. I will use the knowledge inside a new project.

这只是一个示例,其想法是测试 a 是否可以像这样过滤数据源。我将在一个新项目中使用这些知识。

pd: The idea is to be able to use BindingSource.Filter if it is possible.

pd:如果可能的话,我们的想法是能够使用 BindingSource.Filter。

采纳答案by Alain

As per http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.filter.aspx

根据http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.filter.aspx

Only underlying lists that implement the IBindingListViewinterface support filtering.

只有实现该IBindingListView接口的底层列表才支持过滤。

BindingList<T>does not appear to implement IBindingListView- and since it is the underlying list, your collection will not filter.

BindingList<T>似乎没有实现IBindingListView- 由于它是基础列表,您的收藏将不会过滤。

BindingSourceclass, while not generic, does implement this Interface, so try using this as your personas collection. I get the feeling that simply assigning a new BindingSource's datasource to a BindingList won't suffice, since it doesn't change the underlying list. Try:

BindingSource类虽然不是通用的,但确实实现了此接口,因此请尝试将其用作您的角色集合。我觉得简单地将新的 BindingSource 数据源分配给 BindingList 是不够的,因为它不会更改基础列表。尝试:

BindingSource personas = new BindingSource { new Person{ ... }, ... };

回答by iefpw

I think it is because the BindingSource doesn't know what type of data it is filtering. Once a data is converted to dataset into columns and rows, filter can run. Because your datasource is a class, it can't do the automatic filtering.

我认为这是因为 BindingSource 不知道它正在过滤什么类型的数据。一旦数据被转换为数据集成列和行,过滤器就可以运行了。因为你的数据源是一个类,所以它不能做自动过滤。

回答by Brad Rem

As an alternative to implementing IBindingListView which can be pretty involved you can try this type of filtering:

作为实现 IBindingListView 的替代方案,您可以尝试这种类型的过滤:

BindingList<Person> personas = new BindingList<Person> {  
new Person{ Apellido = "App1", DNI = 3011, Edad = 20, Nombre ="Name1", Tamano = new decimal(1.7)} 
,new Person{ Apellido = "App2", DNI = 1520, Edad = 30, Nombre ="Name2", Tamano = new decimal(1.5)} 
,new Person{ Apellido = "App3", DNI = 5654, Edad = 21, Nombre ="Name3", Tamano = new decimal(1.6)} 
,new Person{ Apellido = "App4", DNI = 778, Edad = 40, Nombre ="Name4", Tamano = new decimal(1.68)} 
};

BindingList<Person> filtered = new BindingList<Person>(personas.Where(
                                 p => p.Apellido.Contains("App1")).ToList());
grid.DataSource = filtered;