Telerik RadGrid Custom Paging Filter and Sorting Not Working

2.2k Views Asked by At

I am using telerik radgrid and want to perform server side paging. I am refreing to this demo by telerik.

Here is the code that shows how my grid is configured to handle this.

 <telerik:RadGrid ID="radGridMyWorksheet" AllowMultiColumnSorting="true" runat="server" AutoGenerateColumns="false" ShowStatusBar="True"
        AllowSorting="True" ClientSettings-Scrolling-EnableVirtualScrollPaging="false"
        ClientSettings-Scrolling-AllowScroll="false" AllowCustomPaging="true"
        OnNeedDataSource="radGridMyWorksheet_NeedDataSource" AllowFilteringByColumn="True" OnGroupsChanging="radGridMyWorksheet_GroupsChanging"
        EnableLinqExpressions="false"
        OnItemDataBound="radGridMyWorksheety_ItemDataBound" OnInsertCommand="radGridMyWorksheet_InsertCommand">            
        <MasterTableView DataKeyNames="ID" AllowMultiColumnSorting="true"
            Width="100%" CommandItemDisplay="Top" Name="radGridMyWorksheet" AllowPaging="true">
            <AlternatingItemStyle BackColor="WhiteSmoke" />

and here is my code which provides data to gridview.

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            radGridMyWorksheet.VirtualItemCount = calling method that returns total record count
        }
    }


protected void radGridMyWorksheet_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        try
        {

            List<Expression<Func<RouteWorksheet, object>>> include = new List<Expression<Func<RouteWorksheet, object>>>();
            Expression<Func<RouteWorksheet, object>> routeInclude = (route) => route.Route;
            include.Add(routeInclude);

            int CurrentPageIndex = radGridMyWorksheet.CurrentPageIndex == 0 ? 1 : radGridMyWorksheet.CurrentPageIndex;
            int startRowIndex = (ShouldApplySortFilterOrGroup()) ?
                1 : CurrentPageIndex * radGridMyWorksheet.PageSize;

            int maximumRows = (ShouldApplySortFilterOrGroup()) ?
                radGridMyWorksheet.VirtualItemCount : radGridMyWorksheet.PageSize;

            radGridMyWorksheet.AllowCustomPaging = !ShouldApplySortFilterOrGroup();

            var routeWorksheet = Repository<RouteWorksheet>.GetEntityListForQuery(r => routeIDs.Contains(r.RouteID) && r.IsDeleted == false
                , x => x.OrderByDescending(y => y.RouteDate), include,
                startRowIndex, maximumRows);

            radGridMyWorksheet.DataSource = routeWorksheet;
        }
        catch (Exception ex)
        {
            Utility.WalkException(this.Master, ex, "There was an error while processing the myworksheets record.");
        }
    }


 public bool ShouldApplySortFilterOrGroup()
    {
        return radGridMyWorksheet.MasterTableView.FilterExpression != "" ||
            (radGridMyWorksheet.MasterTableView.GroupByExpressions.Count > 0 || isGrouping) ||
            radGridMyWorksheet.MasterTableView.SortExpressions.Count > 0;
    }

Here custom paging is working fine. but filter and sorting is not working. Can anyone point out what am i doing wrong here ?

2

There are 2 best solutions below

0
On

If you are going to use custom paging with filtering and sorting, you need to disable the custom paging when there are filters (or sort expressions) applied and you need to provide the entire data set to RadGrid's DataSource, so it could apply its internal paging functionality as in the demo that you are referring to.

1
On
Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As GridNeedDataSourceEventArgs) 'Handles RadGrid1.NeedDataSource
    Dim RadGrid1 As RadGrid = CType(Page.Master.FindControl("ContentPlaceHolder1").FindControl("RadGrid1"), RadGrid)

    Dim gridSortString As String = RadGrid1.MasterTableView.SortExpressions.GetSortString()

    Dim args As New DataSourceSelectArguments(gridSortString)

    If gridSortString Is Nothing Then
        RadGrid1.DataSource = GetDataTable("SELECT کالا.شناسه, کالا.عنوان, کالا.پوشه, بارگیری, کالا.گروه_شناسه, گروه.عنوان AS گروه FROM کالا LEFT JOIN گروه ON کالا.گروه_شناسه = گروه.شناسه")
    Else
        RadGrid1.DataSource = GetDataTable("SELECT کالا.شناسه, کالا.عنوان, کالا.پوشه, بارگیری, کالا.گروه_شناسه, گروه.عنوان AS گروه FROM کالا LEFT JOIN گروه ON کالا.گروه_شناسه = گروه.شناسه ORDER BY " & gridSortString)
    End If

End Sub