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

[Solved] Grid sorting & paging

3 Answers 96 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 26 Jul 2013, 08:14 PM
Hello,

My sort stops working after the user moves to a different page. So, once I sort a column, it sorts correctly on
the first page, when the user moves to a different page, the sort is lost.  I am using AllowCustomSorting.

Please help.

Here are all my important code snippets:

My grid:

 

 

<telerik:RadGrid ID="gvPolicyResults" runat="server" Visible="false" AllowSorting="true"

 

 

 

AutoGenerateColumns="false" OnItemCommand="gvPolicyResults_RowCommand" OnPageIndexChanged="gvPolicyResults_PageIndexChanged"

 

 

 

OnNeedDataSource="gvPolicyResults_NeedDataSource" OnItemCreated="gvPolicyResults_ItemCreated"

 

 

 

Width="100%" BorderWidth="0" CellPadding="0" CellSpacing="0" HeaderStyle-CssClass="DataColumnHeader1"

 

 

 

HeaderStyle-HorizontalAlign="Left" AllowFilteringByColumn="true"

 

 

 

PageSize="50" PagerStyle-Mode="NextPrevAndNumeric" PagerStyle-Position="TopAndBottom"

 

 

 

OnSortCommand="gvPolicyResults_SortCommand" AllowPaging="true" >

 

 

 

<MasterTableView DataKeyNames="PolicyReference" AllowCustomSorting="true">

 


Methods:

protected void gvPolicyResults_PageIndexChanged(object sender, GridPageChangedEventArgs e)

{

    gvPolicyResults.CurrentPageIndex = e.NewPageIndex;

    gvPolicyResults.Rebind();

}

 

protected void gvPolicyResults_NeedDataSource(object source, GridNeedDataSourceEventArgs e)

{

    gvPolicyResults.DataSource = GetPolicyResults();
}

        protected void gvPolicyResults_SortCommand(object source, GridSortCommandEventArgs e)
        {
            if ("EffectiveDate".Equals(e.CommandArgument))
            {
                DataView dv = GetPolicyResults();
                switch (e.OldSortOrder)
                {
                    case GridSortOrder.None:
                        dv.Sort = "[EffectiveDateSort] DESC";
                        e.Item.OwnerTableView.DataSource = dv;
                        e.Item.OwnerTableView.Rebind();
                        break;
                    case GridSortOrder.Ascending:
                        dv.Sort = "[EffectiveDateSort] ASC";
                        e.Item.OwnerTableView.DataSource = dv;
                        e.Item.OwnerTableView.Rebind();
                        break;
                    case GridSortOrder.Descending:
                        dv.Sort = "[EffectiveDateSort] DESC";
                        e.Item.OwnerTableView.DataSource = dv;
                        e.Item.OwnerTableView.Rebind();
                        break;
                }
            }
            else if ("PolicyReference".Equals(e.CommandArgument))
            {
                DataView dv = GetPolicyResults();
                switch (e.OldSortOrder)
                {
                    case GridSortOrder.None:
                        dv.Sort = "[PolicyReference] DESC";
                        e.Item.OwnerTableView.DataSource = dv;
                        e.Item.OwnerTableView.Rebind();
                        break;
                    case GridSortOrder.Ascending:
                        dv.Sort = "[PolicyReference] ASC";
                        e.Item.OwnerTableView.DataSource = dv;
                        e.Item.OwnerTableView.Rebind();
                        break;
                    case GridSortOrder.Descending:
                        dv.Sort = "[PolicyReference] DESC";
                        e.Item.OwnerTableView.DataSource = dv;
                        e.Item.OwnerTableView.Rebind();
                        break;
                }
            }

        }




3 Answers, 1 is accepted

Sort by
0
Michael
Top achievements
Rank 1
answered on 30 Jul 2013, 01:39 PM
anyone have any suggestions?
0
Andrey
Telerik team
answered on 31 Jul 2013, 12:44 PM
Hello,

Try to remove the logic from the PageIndexChanged event. Most probably the problem comes from the Rebind() method call there. RadGrid is performing these steps automatically and you should not do this manually unless you need it for other functionality.

Are you calling the DataBind() method anywhere in your project? If the answer is yes, please remove it and replace it with Rebind() method calls if needed.

Regards,
Andrey
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Michael
Top achievements
Rank 1
answered on 31 Jul 2013, 12:48 PM
thanks
Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Answers by
Michael
Top achievements
Rank 1
Andrey
Telerik team
Share this question
or