Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 863
Buddy Lee Avatar asked Apr 13 '26 03:04

Buddy Lee


1 Answers

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;
like image 142
Buddy Lee Avatar answered Apr 15 '26 17:04

Buddy Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!