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

how to get datasource of a RadGrid after postback event

3 Answers 627 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sam
Top achievements
Rank 1
Sam asked on 22 Nov 2015, 06:13 PM

Hello,

I have a simple RadGrid using OnNeedDataSource to bind data on initial page load. And on a button (outside grid) clicked event I try to get the RadGrid datasource using code below: 

01.private DataTable GetFilteredDataSource()
02.        {
03.            DataTable DT = new DataTable();
04. 
05.            DataTable FilteredDT = new DataTable();
06.            string filterexpression = string.Empty;
07.            filterexpression = RadGrid1.MasterTableView.FilterExpression;          
08.            // DT = (DataTable)RadGrid1.DataSource;
09.            DT = RadGrid1.MasterTableView.DataSource as DataTable;
10. 
11.            FilteredDT = DT.AsEnumerable()
12.            .AsQueryable()
13.            .Where(filterexpression)
14.            .CopyToDataTable();
15.            return FilteredDT;
16.        }

 

line 09 will give DataSource = null.

 

Please help.  Thanks!

3 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 25 Nov 2015, 10:39 AM
Hello Sam,

If you would like to retrieve all data that RadGrid is bound to you can query the data source directly. There is no need to use the RadGrid API in this case.

With that said, would you elaborate in more detail what is the behavior that you would like to implement? Why do you need to retrieve the data from the grid?

Regards,
Viktor Tachev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Sam
Top achievements
Rank 1
answered on 25 Nov 2015, 02:32 PM

Hello Viktor,

 My requirement is to get RadGrid's datasource (that included filtered/sorted) and pass to a report (.rdlc).  I want do this way so that I can save an around trip to database.

 Thank you for your help

0
Viktor Tachev
Telerik team
answered on 26 Nov 2015, 03:29 PM
Hi Sam,

When a button outside RadGrid is clicked it is expected that the DataSource property is null. In this case the grid retrieves the relevant information in the ViewState.

If you would like to limit the roundtrips to the database you can retrieve the data and cache it (e.g. in a Session variable). Then you can use that data to bind the grid. Moreover, you can use that data and apply the filter/sort expressions before passing it to generate a report.


Regards,
Viktor Tachev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Sam
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Sam
Top achievements
Rank 1
Share this question
or