Custom server side paging

3 posts, 0 answers
  1. StuartLittle
    StuartLittle avatar
    20 posts
    Member since:
    Apr 2018

    Posted 24 Jul 2018 Link to this post

    I am trying to implement server side custom paging and using https://demos.telerik.com/aspnet-ajax/grid/examples/functionality/paging/custom-paging/defaultcs.aspx as a reference. 

    Please refer to your following code

    protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
            {
                MyBusinessObjectCollection MyBusinessObjectCollection1 = new MyBusinessObjectCollection();
                int startRowIndex = (ShouldApplySortFilterOrGroup()) ?
                    0 : RadGrid1.CurrentPageIndex * RadGrid1.PageSize;
      
                int maximumRows = (ShouldApplySortFilterOrGroup()) ?
                    MyBusinessObjectCollection1.SelectCount() : RadGrid1.PageSize;
      
                RadGrid1.AllowCustomPaging = !ShouldApplySortFilterOrGroup();
      
                RadGrid1.DataSource = MyBusinessObjectCollection1.Select(startRowIndex, maximumRows);
            }

     

    If the user uses filtering, say in the example, searching for Unit Price 9, in this case, your ShouldApplySortFilterOrGroup() would return a true. That means maximumRows will be count of entire dataset. So, MyBusinessObjectCollection1.Select(0, 100000); would be very expensive.

     

    I also noticed, that when filtering, items in pages in the bottom right of the grid doesnt display the correct item count.

     

    Do you have another working example of server side paging which allows filtering?

    Thank you

  2. StuartLittle
    StuartLittle avatar
    20 posts
    Member since:
    Apr 2018

    Posted 24 Jul 2018 in reply to StuartLittle Link to this post

    Items in pages is displaying the correct item count. Please ignore that part from my question.
  3. Attila Antal
    Admin
    Attila Antal avatar
    542 posts

    Posted 30 Jul 2018 Link to this post

    Hi StuartLittle,

    The demo from the link is just one example showing one way of requesting data from the data layer, but it can be further customized as needed. Instead of returning all the rows when Filter/Sort/Group is applied, you can make the logic always return the same amount of data by changing the parameters in MyBusinessObjectCollection1.Select(startRowIndex, maximumRows); method.

    Here is an example that uses custom paging, and will always return the same amount of data whether or not filter is applied.
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        NorthwindEntities db = new NorthwindEntities();
        int startRowIndex = RadGrid1.CurrentPageIndex * RadGrid1.PageSize;
        int maximumRows = RadGrid1.PageSize;
        var source = new List<Order>();
        var SortExpression = RadGrid1.MasterTableView.SortExpressions;
     
        if (RadGrid1.MasterTableView.FilterExpression != string.Empty)
        {
            source = db.Orders.Where(RadGrid1.MasterTableView.FilterExpression).ToList();
            RadGrid1.VirtualItemCount = source.Count();
            source = source.OrderBy((SortExpression.Count > 0) ? SortExpression[0].FieldName + " " + SortExpression[0].SortOrder : "OrderID")
           .Skip(startRowIndex).Take(maximumRows).ToList();
        }
        else
        {
            RadGrid1.VirtualItemCount = db.Orders.Count();
            source = db.Orders.OrderBy((SortExpression.Count > 0) ? SortExpression[0].FieldName + " " + SortExpression[0].SortOrder : "OrderID")
           .Skip(startRowIndex).Take(maximumRows).ToList();
        }
     
        RadGrid1.DataSource = source;
    }

    I've attached a sample project that you can test to see it in action.

    I hope this will prove helpful.

    Kind regards,
    Attila Antal
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top