I found the MSDN example code for getting the default view of a collection and adding a filter to the view, but most of it is for .Net 4.0. I’m on a team that is not currently switching to 4.0, so I don’t have that option. None of the examples I found used a DataTable as the source, so I had to adapt it a little. I’m using a DataTable because the data is comming from a DB ans it’s easy to populate. After trying to implement the MSDN examples, I get a “NotSupportedException” when I try to set the Filter. This is the c# code I have:
protected DataTable _data = new DataTable();
protected BindingListCollectionView _filteredDataView;
…
private void On_Loaded(Object sender, RoutedEventArgs e)
{
_filteredDataView = (BindingListCollectionView)CollectionViewSource.GetDefaultView(_data);
_filteredDataView.Filter = new Predicate(MatchesCurrentSelections); // throws NotSupportedException
}
…
public bool MatchesCurrentSelections(object o){…}
It seems that either BindingListCollectionView does not support filtering in .Net 3.5, or it just doesn’t work for a DataTable. I looked at setting it up in XAML instead of the C# code, but the XAML examples use collections in resources instead of a collection that is a memberof the class, so I have no idea how to set that up. Does any one know how to filter a view to a DataTable?
Edit
I stopped looking into this a while ago, I thought I would update my question. I could not get the built-in filtering to work. Seems like it would be much easier with .NET 4.0 I resorted to re-querying the data with different conditions each time the desired filtering changes. In my application’s environment, this has worked well and is very quick.
BindingListCollectionViewdoes not support filtering nor sorting. UseCollectionViewSourceto create an instance ofListCollectionViewinstead which supports both.