gpt4 book ai didi

c# - DataGridView 使用对象列表过滤 BindingSource 作为 DataSource

转载 作者:可可西里 更新时间:2023-11-01 07:57:14 25 4
gpt4 key购买 nike

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

我有以下类(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; }
}

我是这样用的:

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'";

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

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

最佳答案

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

Only underlying lists that implement the IBindingListView interface support filtering.

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

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

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

关于c# - DataGridView 使用对象列表过滤 BindingSource 作为 DataSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10074032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com