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

Accessing Filtered Data in Grid

6 Answers 1236 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Cameron
Top achievements
Rank 1
Cameron asked on 17 Jul 2020, 03:00 PM
Is there currently a way to access filtered data from the grid? I want users to be able to filter data in the grid and then display it in a chart. The only way I can see to do it is by rolling my own filtering code with the OnRead event.

6 Answers, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 17 Jul 2020, 04:07 PM

Hi Cameron,

Indeed, that's the most straightforward way.

An alternative is to get the current grid state (see the GridRef.GetState() method), and create a new DataSourceRequest object with the current filters, then use the .ToDataSourceResult() extension method of ours with that request - this would let you perform the same operation on the data as the grid does internally so you can fetch the same data. Ultimately, it's almost the same as using the OnRead event. If you already have all the data in the view model, the .ToDataSourceResult() method can let you do that filtering for the grid too, pretty easily.

 

Regards,
Marin Bratanov
Progress Telerik

0
App
Top achievements
Rank 1
answered on 07 Aug 2020, 10:02 PM
[quote]Marin Bratanov said:

Hi Cameron,

Indeed, that's the most straightforward way.

An alternative is to get the current grid state (see the GridRef.GetState() method), and create a new DataSourceRequest object with the current filters, then use the .ToDataSourceResult() extension method of ours with that request - this would let you perform the same operation on the data as the grid does internally so you can fetch the same data. Ultimately, it's almost the same as using the OnRead event. If you already have all the data in the view model, the .ToDataSourceResult() method can let you do that filtering for the grid too, pretty easily.

 

Regards,
Marin Bratanov
Progress Telerik

[/quote]

 

Hi Marin!

 

Do you have a sample code of this?

0
Accepted
Cameron
Top achievements
Rank 1
answered on 07 Aug 2020, 10:17 PM

Following Marin's suggestions I was able accomplish my goal. Thanks Marin btw! Here is a snipped of my code: 

public List<ExpandoObject> GetFilteredData()
        {
            if (Data == null || !Data.Any())
            {
                // Return empty list if no data is available
                return new List<ExpandoObject>();
            }
            // Apply filters from grid
            var req = new DataSourceRequest();
            var gridData = Grid.GetState();
            var FilteredData = new List<ExpandoObject>();
            req.Filters = new List<IFilterDescriptor>();
            req.Filters.AddRange(gridData.FilterDescriptors);
            FilteredData.AddRange((IEnumerable<ExpandoObject>)Data.ToDataSourceResult(req).Data);
 
            return FilteredData;
        }
0
Marin Bratanov
Telerik team
answered on 08 Aug 2020, 06:41 AM

Thank you for sharing, Cameron!

 

Regards,
Marin Bratanov
Progress Telerik

0
App
Top achievements
Rank 1
answered on 10 Aug 2020, 02:58 PM
[quote]Cameron said:

Following Marin's suggestions I was able accomplish my goal. Thanks Marin btw! Here is a snipped of my code: 

public List<ExpandoObject> GetFilteredData()
        {
            if (Data == null || !Data.Any())
            {
                // Return empty list if no data is available
                return new List<ExpandoObject>();
            }
            // Apply filters from grid
            var req = new DataSourceRequest();
            var gridData = Grid.GetState();
            var FilteredData = new List<ExpandoObject>();
            req.Filters = new List<IFilterDescriptor>();
            req.Filters.AddRange(gridData.FilterDescriptors);
            FilteredData.AddRange((IEnumerable<ExpandoObject>)Data.ToDataSourceResult(req).Data);
 
            return FilteredData;
        }

[/quote]

 

Thanks! It worked!

0
Greg
Top achievements
Rank 1
Iron
answered on 11 May 2022, 06:49 PM

I simplified this code and turned it into a generic extension method because its something I need often. ie: for selecting all rows matching the current filters. Works very easily this way.


public static List<T> FilteredData<T>(this TelerikGrid<T> grid)
{
	var req = new DataSourceRequest { Filters = new List<IFilterDescriptor>(grid.GetState().FilterDescriptors) };
	var filteredData = new List<T>((IEnumerable<T>)grid.Data.ToDataSourceResult(req).Data);
	return filteredData;
}

Tags
Grid
Asked by
Cameron
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
App
Top achievements
Rank 1
Cameron
Top achievements
Rank 1
Greg
Top achievements
Rank 1
Iron
Share this question
or