wpffilterdatagridicollectionview

How to use IcollectionView Filter without any specific class for the data


Here is the whole query-

I have a WPF application which has a datagrid. Datagrid is binding to IcollectionView after getting the data from database with Autogenerate column as True. While binding the data or in autogeneratingcolumn method, i am creating a header template with filter popup like excel.

When user clicks on any of those column header , a popup will come for the user same as excel filter. Once user check/uncheck a value and click OK, it should filter the data based on the user selection.

Thats where i thought to use the IcollectionView filter capability. But every solution of filter is taking a class to filter the data. like in the below URL( TraceDataItem class is getting used)-

Filtering ObservableCollection with ICollectionView

I can't have a fixed class as datagrid autogenerate column is true and data can be anything. Is there any way to use this filter capability with data having no fixed class as mentioned in all the examples ?

Do let me know for any query you have instead of down voting if need explanation.

Adding the code used- This is what i am using to get the checked/unchecked value from user in filter popup-

var tempCollection = (from c in CurrentDistictValues where c.IsChecked == true select c);
                    var val = from c in tempCollection select c.Name;
                    List<object> itemsChecked = new List<object>();
                    itemsChecked = val.ToList();
                    string s = String.Join("','", itemsChecked);
                    StringBuilder t = new StringBuilder(s);
                    t.Append("'");
                    t.Insert(0, "'");
                    s = t.ToString();

s will have all the values selected by user separated by comma so that the same can be applied for dataview rowfilter:-

itemSource.RowFilter = "[" + columnName + "]" + " " + " IN " + "(" + s + ")";

Note- itemSource is DataView in present code & column name is the property on which we are applying filter.

So now If we use IcollectionView as ItemSource , what should be the approach ?


Solution

  • If you don't know the type of the elements displayed in the ICollectionView, you can still filter them using reflection. Something like this:

    string propertyName = "Name"; //property to filter by
    string valueToCompareAgainst = "x";
    ICollectionView view = ...;
    view.Filter = (obj) =>
    {
        Type type = obj.GetType();
        System.Reflection.PropertyInfo pi = type.GetProperty(propertyName);
        if(pi != null)
        {
            object value = pi.GetValue(obj);
            return value.Equals(valueToCompareAgainst);
        }
        return false;
    };