Iterate through all items on all pages

8 posts, 0 answers
  1. Kris
    Kris avatar
    47 posts
    Member since:
    Feb 2008

    Posted 05 Apr 2010 Link to this post

    I am doing a custom export to excel where I load the grid, say 10 items on the first page, 3 pages long. The user then filters the grid using the built in filter column headers. That limits the grid to 14 items in 2 pages. The user clicks my export button where the following gets executed:


            /// <summary> 
            /// Exports data in grid to excel 
            /// </summary> 
            public void Export() 
            { 
                //rgdProjectWorksheets.ExportSettings.ExportOnlyData = true; 
                //rgdProjectWorksheets.ExportSettings.IgnorePaging = true; 
                //rgdProjectWorksheets.MasterTableView.ExportToExcel(); 
     
                PWDetailAccess pwa = new PWDetailAccess(); 
                DataTable dt = pwa.GetFlatExportData(Convert.ToInt32(Session[ApplicationConstants.SessionVariables.OrganizationID].ToString())); 
     
                List<int> idlist = GetVisibleIDList(); 
     
                HttpContext context = HttpContext.Current; 
                context.Response.Clear(); 
                foreach (DataColumn column in dt.Columns) 
                { 
                    context.Response.Write(column.ColumnName + ","); 
                } 
                context.Response.Write(Environment.NewLine); 
                foreach (DataRow row in dt.Rows) 
                { 
                    if (!idlist.Contains(Convert.ToInt32(row[PWDetailsSchema.ID].ToString()))) 
                        continue
                    for (int i = 0; i < dt.Columns.Count; i++) 
                    { 
                        context.Response.Write(row[i].ToString().Replace(","string.Empty) + ","); 
                    } 
                    context.Response.Write(Environment.NewLine); 
                } 
     
                context.Response.ContentType = "text/csv"
                context.Response.AppendHeader("Content-Disposition""attachment; filename=SA_FlatExport.csv"); 
                context.Response.End(); 
            } 
     
            private List<int> GetVisibleIDList() 
            { 
                List<int> idlist = new List<int>(); 
                for (int i = 0; i < rgdProjectWorksheets.MasterTableView.Items.Count; i++) 
                { 
                    if (rgdProjectWorksheets.Items[i].Visible == true
                        idlist.Add(Convert.ToInt32(rgdProjectWorksheets.Items[i][PWDetailsSchema.ID].Text)); 
                } 
                return idlist; 
            } 

    I compare my full custom datatable with the items in the grid to decide which items in my datatable I will include in the export. This works great on a grid with no paging. The problem arises when I have more than one page. It appears that the count in
    rgdProjectWorksheets.MasterTableView.Items only holds the items on the current page. How do I iterate through all the items in all pages?


    I've tried running the GetVisibleIDList method in the PreRender event (as this post suggests), but that does not seem to be working.
    Any help would be much appreciated.
    Thanks
    Kris
  2. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 09 Apr 2010 Link to this post

    Hello Kris,

    As you have observed correctly, RadGrid keeps only the current page of data when paging is enabled. It never requests or processes any other data than the current page. You have access to whatever data RadGrid is bound only. This is why, with paging, you cannot get data in other pages.

    If you need to bind RadGrid to all the data in all pages, you need to disable paging (set AllowPaging="false"), Rebind, and then iterate over your items. Still, if you have access to the data, iterating over all your data items yourself may be quicker than disabling paging, binding the grid and then re-enabling paging and rebinding.

    Best wishes,
    Veli
    the Telerik team

    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 Public Issue Tracking system and vote to affect the priority of the items.
  3. Kris
    Kris avatar
    47 posts
    Member since:
    Feb 2008

    Posted 09 Apr 2010 Link to this post

    Ok, thanks for the reply. It would be nice if it cached all the records somewhere to prevent the re-binding of the grid, but this will do for now.

    One note though, when re-binding, you need to rebind the MasterTableView, not the grid object itself.

    For example, this doesn't work
     
                rgdProjectWorksheets.AllowPaging = false
                rgdProjectWorksheets.Rebind(); 

    but this does
     
                rgdProjectWorksheets.MasterTableView.AllowPaging = false
                rgdProjectWorksheets.MasterTableView.Rebind(); 


  4. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 12 Apr 2010 Link to this post

    Hello Kris,

    RadGrid does not cache any data it is bound two. There are many reasons for this, 2 of the most important being:

    1. Where to cache? Caching in the ViewState is a performance nightmare. Caching in Session can be problemmatic for many users and large data sets.

    2. How to provide concurrency? What happens when a piece of data is updated outside of the grid, but the grid keeps rebinding to stale data from the cache?

    Still, if you believe caching data suits your needs, it can be implemented relatively easily by the developer, so I believe this is more or less out of scope for RadGrid.

    As for setting AllowPaging from RadGrid's object, I do not seem to observe any differences. Both approaches seem to work OK. Attaching sample page used for testing.

    Sincerely yours,
    Veli
    the Telerik team

    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 Public Issue Tracking system and vote to affect the priority of the items.
  5. Chanan Zass
    Chanan Zass avatar
    63 posts
    Member since:
    Aug 2012

    Posted 22 Jun 2014 in reply to Veli Link to this post


    Dim pageCount As Integer = RadGrid1.MasterTableView.PageCount
            Dim i As Integer = 0
            While i < pageCount
                RadGrid1.CurrentPageIndex = i
                RadGrid1.Rebind()
     
                For Each r As GridDataItem In RadGrid1.MasterTableView.Items
                      ' do something
                Next
                i += 1
            End While
  6. Justin
    Justin avatar
    11 posts
    Member since:
    Dec 2013

    Posted 29 Jul 2014 in reply to Kris Link to this post

    Thank you so much Kris for your post.  I was binding to the radgrid, not the mastertable.  Your post finally got me over the hump I've been stuck on since yesterday afternoon.
  7. w
    w avatar
    26 posts
    Member since:
    Feb 2009

    Posted 13 Dec 2014 in reply to Veli Link to this post

    May I check with you, the latest RadGrid Q3 2014 version still not cache all the data (ignore paging) at client side?
  8. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2466 posts

    Posted 17 Dec 2014 Link to this post

    Hello,

    For the same reasons that were pointed out in the previous posts in this thread, RadGrid does not have server-side caching.

    Nevertheless, if you are using client-side binding, you can take advantage of the client-side caching functionality, by setting the ClientSettings.DataBinding.EnableCaching property to true. You can find additional information on this matter in the following help article, section Client-Side Caching:

    Best Regards,
    Konstantin Dikov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top