How do you filter a view of a DataTable in .Net 3.5 sp1 using WPF c# and xaml?

Posted by Tony on Stack Overflow See other posts from Stack Overflow or by Tony
Published on 2010-04-28T19:55:56Z Indexed on 2010/04/28 20:27 UTC
Read the original article Hit count: 303

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about datatable