0

How can I filter an ObservableCollection bound to a WPF DataGrid when I enter data in the filter_textboxes over the DataGrid`s columns?

Is there an easy solution without using codeproject libs... ?

msfanboy
  • 5,273
  • 13
  • 69
  • 120

2 Answers2

0

I had a very similar problem and there is a fairly simple solution. In short:

To apply multiple filters to a collection bound to a WPF DataGrid you should instantiate a CollectionViewSource object as a proxy between the view and the collection (this will also work with other collection controls). Doing so will allow you to subscribe multiple filter event handlers to it's Filter event. Filters are applied in the order in which they are subscribed and can be removed by unsubscribing them.

If you used the CollectionViewSource.GetDefaultView() static method in your codebehind or ViewModel, this will return an instance of an ICollectionView which will only support a single filter with a Filter property.

Your can find an example with source code here http://www.codeproject.com/Articles/442498/Multi-filtered-WPF-DataGrid-with-MVVM

BgRva
  • 1,521
  • 12
  • 26
0

I asked a similar question some time ago which you might find useful: Filter WPF TreeView using MVVM

It should be a very similar approach for DataGrid. Basically, you want to create a CollectionViewSource using your ObservableCollection and bind your DataGrid to that instead of binding directly to the ObservableCollection and then it's simply a matter of setting the CollectionViewSource's Filter when the user types.

Community
  • 1
  • 1
alimbada
  • 1,392
  • 1
  • 12
  • 27
  • The problem is not to filter ONE column but SEVERAL columns. – msfanboy Feb 25 '11 at 14:46
  • because when I have filtered one value and return true to the Predicate I enter another value in another column. but this new value is not applied to the origin collection but to the filtered collection...therefore I can not filter multiple values... – msfanboy Feb 25 '11 at 19:53
  • Ganesh Mohanrao's comment: "Could you please share how you achieved Tree Filtering using Observable Collection please? Code Sample would be very useful. I am trying to filter my Tree wherein even if the Root Node fails the criteria and the child node satisfies, I can't filter the Parent Node of the child. Since the child node satisfied the criteria, it should show with the whole Tree Path." – Peter O. Feb 17 '12 at 16:31