0
votes

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 ?

1
How do you know what property to filter by? - mm8
@mm8 I am getting the column name from header click. FYI, i have implemented another way as well using dataview.RowFilter which works fine for small data , but the performance is very poor for large data. So i was browsing for solutions when i found some links to IcollectionView filter capability. - Deepak Tiwary
You should be able to filter the ICollectionView using reflection then. Please post the code you have written so far. - mm8
@mm8- I have added the present code in my question which is using DataView as ItemSource and Rowfilter using the string query. - Deepak Tiwary
You could use reflection. See my answer. It should give you the idea. - mm8

1 Answers

0
votes

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