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

Custom server side paging

2 Answers 744 Views
Grid
This is a migrated thread and some comments may be shown as answers.
StuartLittle
Top achievements
Rank 1
StuartLittle asked on 24 Jul 2018, 05:56 PM

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 Answers, 1 is accepted

Sort by
0
StuartLittle
Top achievements
Rank 1
answered on 24 Jul 2018, 06:07 PM
Items in pages is displaying the correct item count. Please ignore that part from my question.
0
Attila Antal
Telerik team
answered on 30 Jul 2018, 02:21 PM
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.
Tags
Grid
Asked by
StuartLittle
Top achievements
Rank 1
Answers by
StuartLittle
Top achievements
Rank 1
Attila Antal
Telerik team
Share this question
or