Accessing the filtered collection through MVVM after applying filter to the WPF radgridview

14 posts, 2 answers
  1. Howell
    Howell avatar
    10 posts
    Member since:
    Jul 2012

    Posted 28 Mar 2012 Link to this post

    Hi,

    Is it possible to retrieve the collection of items that is bound to the radgrid after I apply filter and sorting to rad grid. through MVVM pattern?

    Regards,
    Howell
  2. Answer
    Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 28 Mar 2012 Link to this post

    Hi,

    RadGridView stores its current view in its Items property at all times.

    For MVVM:

    If you wrap your original source collection in a QueryableCollectionView, expose this QCV as a property on your view model, and then bind RadGridView.Items to this property, you will have the current view in this QueryableCollectionView inside your view model.

    I hope this helps.

    Regards,
    Ross
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Answer
    Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 28 Mar 2012 Link to this post

    Hi,

    Correction: "bind RadGridView.ItemsSource to this property."

    Kind regards,
    Ross
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  5. Howell
    Howell avatar
    10 posts
    Member since:
    Jul 2012

    Posted 03 Apr 2012 Link to this post

    Hi Ross,

    Thanks for the reponse it works like a charm...

    Additinal inquiry: can the ViewModel be notifiedwhen the filter are in place on the gridview? can we bind a command that will be trigger if the gridview data is filtered?

    Thanks in advance,
    Howell
  6. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 03 Apr 2012 Link to this post

    Hello,

    If you bind your grid to a QueryableCollectionView, when the user filters the grid you will receive these two events:

    QueryableCollectionView.FilterDescriptors.CollectionChanged
    QueryableCollectionView.FilterDescriptors.ItemChanged 

    depending on whether the user is adding/removing a column filter or he is modifying an existing one.

    You can listen for these two events inside the view model and react accordingly.

    All the best,
    Ross
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  7. Riku
    Riku avatar
    1 posts
    Member since:
    Jul 2012

    Posted 12 Feb 2014 Link to this post

    For anyone finding this thread, I have the following follow-up. Personally, I don't like mixing in external components in my view model, and there is another possibility: bind to an ICollectionView instead. Still not "pure" MVVM to the purists, but better.

    My solution is to keep an ObservableCollection<> in my view model, and create a view from it for binding. All actions on the underlying collection I perform on the ObservableCollection<>, and when I need the sorted/filtered collection, I pull that from the ICollectionView view instead:

    ICollectionView MyCollectionView = CollectionViewSource.GetDefaultView(MyCollection); // Bind to this view

    Hope this helps somebody!
  8. Christian
    Christian avatar
    52 posts
    Member since:
    Apr 2011

    Posted 13 May 2014 Link to this post

    Hi,
    I have the same need as Howell, but I also use a RadDataPager to page the grid.
    This means that the Items Property of the Grid does only contain the items on the current page, but I need the filtered items from all pages. Currently I use a QueryableCollectionView and filter its SourceCollection with the help of QueryableCollectionView .Filter predicate.

    Is there an easier way to access the filtered items from all pages?

    Best Regards,
    Christian
  9. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 13 May 2014 Link to this post

    Hi Christian,

    When using the data pager the GridView shows only one page with the loaded data and RadGridView is actually not aware that there are other items to be loaded. Therefore its Items collection represents just the items on the page and there is not a way to have any other information.

    Regards,
    Didie
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  10. Terry
    Terry avatar
    3 posts
    Member since:
    Oct 2012

    Posted 29 May 2014 in reply to Dimitrina Link to this post

    I have wrapped my collection in the QueryableCollectionView to get the list of visible items  in the grid and it works well. I have ran into an issue I was hoping someone could help me with. When the grid is grouped, the QuerableCollectionView items just list the two groups.

    How can I get the visible rows in the grid, once a group is expanded?

    Thanks
  11. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 02 Jun 2014 Link to this post

    Hello,

    You can get all the items, currently visible in RadGridView, through its RadGridView.Items collection. How does this work for you?

    Regards,
    Didie
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  12. Louis
    Louis avatar
    83 posts
    Member since:
    Aug 2013

    Posted 29 Sep 2014 in reply to Riku Link to this post

    Hi,

    I'm trying to use the solution Riku presented, but the DefaultView is never getting updated with the filter information:

    public ICollectionView WellPropertyCollection
    {
        get
        {
            if (_WellPropertyCollection == null && WellProperties != null)
            {
                // This works fine
                //_WellPropertyCollection = new QueryableCollectionView(WellProperties);
     
                // This always contains the full collection, and _WellPropertyCollection.Filter is null.
                _WellPropertyCollection = CollectionViewSource.GetDefaultView(WellProperties);
     
                // Tried this as well as a desperate measure, but this didn't work either.
                //CollectionViewSource newSource = new CollectionViewSource();
                //newSource.Source = WellProperties;
                //_WellPropertyCollection = newSource.View;
            }
            return _WellPropertyCollection;
        }
    }

    If I use the QueryableCollectionView wrapper, then the list correctly contains the filtered items after a filter is applied. However, using the DefaultView, the list always contains all the items in the full list and the Filter property on the ICollectionView is never set.

    Am I doing something wrong here? How can I get the filtered list in the ViewModel without introducing view-specific (Telerik) classes?

    Thanks,
    Louis
  13. Nick
    Admin
    Nick avatar
    593 posts

    Posted 02 Oct 2014 Link to this post

    Hello Louis,

    RadGridView and its data engine work with FilterDescriptors. You can use QueryableCollectionView in your ViewModel as ItemsSource for RadGridView. Its FilterDescriptors collection will be synched with RadGridView's and enumerating it will give you only the items that can be viewed in RadGridView at the current moment(Filtered, sorted, paged, etc.)



    Regards,
    Nik
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  14. Louis
    Louis avatar
    83 posts
    Member since:
    Aug 2013

    Posted 02 Oct 2014 in reply to Nick Link to this post

    Hi Nik,

    Thanks for the response. However, if you read my post and example code, you'll see that I already have it working with QueryableCollectionView. This, however, violates the MVVM design principle. I'm looking for a way to do this without introducing Telerik classes into my ViewModel.

    Louis
  15. Nick
    Admin
    Nick avatar
    593 posts

    Posted 03 Oct 2014 Link to this post

    Hi Louis,

    I am not sure what exactly do you mean by This, however, violates the MVVM design principle. This is not a violation of the pattern as the QCV does not have any reference to a visual element and can be used to work with any items control. 

    As to using solely the DefaultView, this cannot be achieved since the DataTable class does not offer a mechanism that can be synchronized with RadGridViews. 

    Hope this makes sense. 

    Regards,
    Nik
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top
UI for WPF is Visual Studio 2017 Ready