DataGridView的过滤一个BindingSource的与对象的列表作为数据源 [英] DataGridView Filter a BindingSource with a List of object as DataSource

查看:371
本文介绍了DataGridView的过滤一个BindingSource的与对象的列表作为数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想筛选的BindingSource用的BindingList作为数据源。我试着BindingSource.Filter ='文本条件'但它没有工作,没有任何反应,屏幕上的数据保持不变。但是,如果使用一个DataSet作为数据源它的作品。是否有可能来过滤与BindingSource.Filter财产的对象列表

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?

我有以下类:

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; }
        }

这是我如何使用它:

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.

PD:我们的想法是能够使用BindingSource.Filter如果它是可能

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

推荐答案

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

只有基本实现了 IBindingListView 接口支持过滤列表。

Only underlying lists that implement the IBindingListView interface support filtering.

的BindingList< T> 似乎没有落实 IBindingListView - 而且,因为它是在基本列表后,您的集合将不过滤

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

BindingSource 类,而不是通用的,确实实现了这个接口,所以尽量使用这个作为你的角色集合。我得到的只是分配新的BindingSource的数据源到的BindingList是不够的,因为它不改变基本的列表中的感觉。尝试:

BindingSource class, 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 personas = new BindingSource { new Person{ ... }, ... };

这篇关于DataGridView的过滤一个BindingSource的与对象的列表作为数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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