This is a migrated thread and some comments may be shown as answers.

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

15 Answers 1637 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Howell
Top achievements
Rank 1
Howell asked on 28 Mar 2012, 04:18 PM
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

15 Answers, 1 is accepted

Sort by
0
Accepted
Rossen Hristov
Telerik team
answered on 28 Mar 2012, 04:35 PM
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 >>
0
Accepted
Rossen Hristov
Telerik team
answered on 28 Mar 2012, 04:47 PM
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 >>
0
Howell
Top achievements
Rank 1
answered on 03 Apr 2012, 02:48 PM
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
0
Rossen Hristov
Telerik team
answered on 03 Apr 2012, 03:02 PM
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 >>
0
Riku
Top achievements
Rank 1
answered on 12 Feb 2014, 12:54 PM
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!
0
Christian
Top achievements
Rank 1
answered on 13 May 2014, 07:24 AM
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
0
Dimitrina
Telerik team
answered on 13 May 2014, 01:50 PM
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.
 
0
Terry
Top achievements
Rank 1
answered on 29 May 2014, 09:56 PM
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
0
Dimitrina
Telerik team
answered on 02 Jun 2014, 08:04 AM
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.
 
0
Louis
Top achievements
Rank 1
answered on 29 Sep 2014, 07:06 PM
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
0
Nick
Telerik team
answered on 02 Oct 2014, 08:35 AM
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.
 
0
Louis
Top achievements
Rank 1
answered on 02 Oct 2014, 03:05 PM
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
0
Nick
Telerik team
answered on 03 Oct 2014, 08:20 AM
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.
 
0
Jerry
Top achievements
Rank 1
answered on 02 Jan 2018, 08:25 PM

We are using the Telerik RadGridView in our Silverlight application.

We bind an ObservableCollection<MyData> to the RadGridView.

The RadGridView allows each column to be filtered. (IsFilterable="True")

The problem is that when the [Process] button is pressed in the UI, and the ViewModel method wants to process the filtered rows, there is no way to tell which rows that the telerik RadGridView filtered. Instead, the method wants to process all rows.

How can we get the rows that the Telerik control filtered on?

Also, we are using Silverlight.

 


0
Vladimir Stoyanov
Telerik team
answered on 03 Jan 2018, 02:24 PM
Hello Jerry,

I assume that the information in the following post is related to your question here. You can access the filtered collection through the Items property of the RadGridView.

Another option is the approach suggested in this thread of wrapping your ObservableCollection in a QueryableCollectionView, exposing that through a property and then binding it the ItemsSource property of your ViewModel. Then you can access the filtered items through the QueryableCollectionView property in the ViewModel.

If you need any additional assistance, you can also open a new support ticket.

Regards,
Vladimir Stoyanov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
Howell
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Howell
Top achievements
Rank 1
Riku
Top achievements
Rank 1
Christian
Top achievements
Rank 1
Dimitrina
Telerik team
Terry
Top achievements
Rank 1
Louis
Top achievements
Rank 1
Nick
Telerik team
Jerry
Top achievements
Rank 1
Vladimir Stoyanov
Telerik team
Share this question
or