Click here to Skip to main content
15,888,297 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

Sorting is working properly if implement on First page of Grid but on Subsequent pages it gives result of whole DataTable, I am expecting result for that page only.

Please guide me for the same. Below given code is used.

Property:

C#
public SortDirection dir
        {
            get
            {
                if (ViewState["dirState"] == null)
                {
                    ViewState["dirState"] = SortDirection.Ascending;
                }
                return (SortDirection)ViewState["dirState"];
            }
            set
            {
                ViewState["dirState"] = value;
            }
        }


Method:

C#
protected void gvEmployeeDetails_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortingDirection = string.Empty;
            if (dir == SortDirection.Ascending)
            {
                dir = SortDirection.Descending;
                sortingDirection = "Desc";
            }
            else
            {
                dir = SortDirection.Ascending;
                sortingDirection = "Asc";
            }

            DataView sortedView = new DataView(BindGrid());
            sortedView.Sort = e.SortExpression + " " + sortingDirection;
            gvEmployeeDetails.DataSource = sortedView;
            gvEmployeeDetails.DataBind();
        }
Posted
Updated 4-Feb-16 23:29pm
v2
Comments
[no name] 5-Feb-16 3:42am    
Make sure that you are getting correct value in sortingDirection and e.SortExpression variable. Secondly make sure that e.SortExpression is column name and it should present in your dataview.
Pinkesh Patel 5-Feb-16 6:05am    
It is working on second page but I want sorted data PageWise but the code written is for sorting the data Table wise. What is usually used? Sorting data of page wise or table wise?
Sinisa Hajnal 26-Feb-16 7:53am    
That is normal behaviour - generally, page sorting is useless. Think about it. You are on page 1 sorted by the date (just an example) descending and you see newest entries first (those with max date). You change sort order expecting to see first entries - and instead you see two days history because it is page based sort!?

If you need pagewise sort, either change datasource item recovery to return only single page of data or program your own sorting mechanism. Good luck

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900