how to get datasource of a RadGrid after postback event

4 posts, 0 answers
  1. Sam
    Sam avatar
    27 posts
    Member since:
    Sep 2013

    Posted 22 Nov 2015 Link to this post

    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!

  2. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2357 posts

    Posted 25 Nov 2015 Link to this post

    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
  3. Sam
    Sam avatar
    27 posts
    Member since:
    Sep 2013

    Posted 25 Nov 2015 in reply to Viktor Tachev Link to this post

    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

  4. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2357 posts

    Posted 26 Nov 2015 Link to this post

    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
Back to Top