sorting a gridview in class

Posted by user175084 on Stack Overflow See other posts from Stack Overflow or by user175084
Published on 2010-03-12T17:16:01Z Indexed on 2010/03/12 17:17 UTC
Read the original article Hit count: 247

Filed under:
|
|
|
|

ok i have a project which has many gridview in its pages... now i am sorting the fridveiw using the sorting function like this:

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dt = Session["TaskTable2"] as DataTable;

        if (dt != null)
        {

            //Sort the data.
            dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
            GridView1.DataSource = Session["TaskTable2"];
            GridView1.DataBind();
        }

    }

    private string GetSortDirection(string column)
    {
        // By default, set the sort direction to ascending.
        string sortDirection2 = "ASC";

        // Retrieve the last column that was sorted.
        string sortExpression2 = ViewState["SortExpression2"] as string;

        if (sortExpression2 != null)
        {
            // Check if the same column is being sorted.
            // Otherwise, the default value can be returned.
            if (sortExpression2 == column)
            {
                string lastDirection = ViewState["SortDirection2"] as string;
                if ((lastDirection != null) && (lastDirection == "ASC"))
                {
                    sortDirection2 = "DESC";
                }
            }
        }

        // Save new values in ViewState.
        ViewState["SortDirection2"] = sortDirection2;
        ViewState["SortExpression2"] = column;

        return sortDirection2;
    }

but this code is being repeated in many pages so i tried to put this function in a C# class and try to call it but i get errors....

for starters i get the viewstate error saying :|

"viewstate does not exist in the current context"

so how do i go about doing this ....??

thanks

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about c#