Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

gridview paging and sorting - lose sorting when paging

Status
Not open for further replies.

jrenae

Programmer
Jan 18, 2006
142
US
Hello,

I have a gridview which I allow the users to page and sort. They both work fine, except if I sort a column, then click a different page, it doesn't keep the sort expression I originally chose (I choose Zip as the sort column, it sorts fine, then I click a different page and it's not sorted by Zip anymore). I'm thinking I need to call the SortGridView method after the paging, but I don't know how to get the current sortExpression.

Any suggestions would be greatly appreciated.

Thanks in advance.

Code:
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GetFormVars();
        BindReport();
     }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;

        if (GridViewSortDirection == SortDirection.Ascending)
        {

            GridViewSortDirection = SortDirection.Descending;

            SortGridView(sortExpression, " DESC");

        }

        else
        {
            GridViewSortDirection = SortDirection.Ascending;
            SortGridView(sortExpression, " ASC");
        }
    }

    private void SortGridView(string sortExpression, string direction)
    {

        // You can cache the DataTable for improving performance
        GetFormVars();
        BindReport();

        //DataTable dt = GetData().Tables[0];
        DataTable dt = GridView1.DataSource as DataTable;


        DataView dv = new DataView(dt);

        dv.Sort = sortExpression + direction;

        GridView1.DataSource = dv;

        GridView1.DataBind();

    }
 
Try storing the sort expression and direction in session variables. Then, in the PageIndexChanging event, call the SortGridView method with those session variable s.
 
Thanks jbenson... I ended up using viewstate...and changing the code I was using...if you're interested here's the code I ended up using.

Code:
    public String sortExpression
    {
        get
        {
            if (ViewState["sortExpression"] == null)
            {
                ViewState["sortExpression"] = Convert.ToString(GridView1.Columns[0].SortExpression);
            }
            return Convert.ToString(ViewState["sortExpression"]);
        }
        set
        {
            ViewState["sortExpression"] = value;
        }
    }

    public String sortDirection
    {
        get
        {

            if (ViewState["sortDirection"] == null)
            {
                ViewState["sortDirection"] = SortDirection.Descending;
            }
            if (Convert.ToString(ViewState["sortDirection"]) == Convert.ToString(SortDirection.Descending))
            {
                return "DESC";
            }
            {
                return "ASC";
            }

        }
        set
        {
            if (value == "ASC")
            {
                ViewState["sortDirection"] = SortDirection.Ascending;
            }
            else
            {
                ViewState["sortDirection"] = SortDirection.Descending;
            }


        }

    }
     protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        SortGridView(sortExpression, sortDirection);
    }


    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (sortExpression != e.SortExpression)
        {
            //its a new sort expression so default it to ascending
            sortDirection = "";
        }

        sortExpression = e.SortExpression;

        if (sortDirection == "ASC")
        {
            sortDirection = "DESC";
        }
        else
        {
            sortDirection = "ASC";
        }
        SortGridView(sortExpression, sortDirection);


    }

    private void SortGridView(string sortExpression, string direction)
    {

        //GetFormVars();
        BindUsers();

        DataTable dt = GridView1.DataSource as DataTable;
        DataView dv = new DataView(dt);

        dv.Sort = sortExpression + " " + direction;

        GridView1.DataSource = dv;

        GridView1.DataBind();


    }

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top