0

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.

Tony
  • 953
  • 1
  • 10
  • 22

2 Answers2

1

BindingListCollectionView does not support filtering nor sorting. Use CollectionViewSource to create an instance of ListCollectionView instead which supports both.

wpfwannabe
  • 14,587
  • 16
  • 78
  • 129
  • Can you give an example where the collection behind the view is a member property? The only examples I've found of using CollectionViewSource are in the XAML, and the view is for a collection which is a static resource, not a property. – Tony Apr 28 '10 at 21:10
  • Please check [my recent answer](http://stackoverflow.com/questions/2702434/entity-framework-4-0-databinding-with-sorting-not-working/2720540#2720540) where you can find exactly the code you are looking for (just remove sorting, add filtering). Other than this, you shouldn't give up on XAML just because your collection is a resource. You can still happily put it all together in XAML. But if you prefer code you have both options. – wpfwannabe Apr 28 '10 at 21:47
  • The referenced posts did not help. I gave up on WPF's filtering, at least until we switch to .NET 4.0. See my edit. – Tony Jul 08 '10 at 19:40
0

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.

Tony
  • 953
  • 1
  • 10
  • 22