DataGridView filtering

本小妞迷上赌 提交于 2019-12-11 02:16:04

问题


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

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.

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.


回答1:


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;



回答2:


The IBindingListView interface supplements the data-binding capabilities of the IBindingList interface by adding support for filtering of the list.

A couple of solutions for generic IBindingListView implementations can be found here.



来源:https://stackoverflow.com/questions/1501079/datagridview-filtering

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