DataGridView过滤 [英] DataGridView filtering
问题描述
我正在创建一个能够采取任何类型的列表的控件。基本上如下代码:
I'm creating a control that should be able to take any kind of list. Essentially the following code:
void BindData(IList list)
{
BindingSource bs = new BindindSource();
bs.DataSource = list;
this.DataGridView.DataSource = bs;
}
现在我有一个我想用来过滤我的数据的文本框格。我认为它可以像设置bs.Filter属性一样简单,但显然不是。 bs.SupportsFiltering也返回false。
Now I have a textbox that I want to use to filter the data in my grid. I figured it'd be as simple as setting the bs.Filter property but apparently not. The bs.SupportsFiltering returns false as well.
这是使用IList的问题吗?如果是这样,是否有另一个收集类/接口,我可以使用来实现相同的效果? (再次,我不知道列表中的对象的类型是什么。
Is this an issue with me using the IList? If so, is there another collection class / interface that I can use to achieve the same effect? (Again, I'm not sure what the type is of the objects in the list.
推荐答案
不知道我的类型我通过手工过滤数据
这是我的代码片段,它的工作原理很好,希望它不会被证明是太慢的数据量较大:: ::手指交叉::
Not knowing the type I'm getting passed, I resulted in filtering the data by hand. Here's my code snippet. It works well. Hopefully it doesn't prove to be too slow with larger amounts of data. :: Fingers Crossed ::
List<object> filteredData = new List<object>();
foreach (object data in this.DataSource)
{
foreach (var column in this.Columns)
{
var value = data.GetType().GetProperty(column.Field).GetValue(data,null)
.ToString();
if (value.Contains(this.ddFind.Text))
{
filteredData.Add(data);
break;
}
}
}
this.ddGrid.DataSource = filteredData;
这篇关于DataGridView过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!