sorting and paging with gridview asp.net

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

        if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            SortGridView(sortExpression, DESCENDING);
        }
        else
        {
            GridViewSortDirection = SortDirection.Ascending;
            SortGridView(sortExpression, ASCENDING);
        }
    }
    public SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;

            return (SortDirection)ViewState["sortDirection"];
        }
        set { ViewState["sortDirection"] = value; }
    }
    private void SortGridView(string sortExpression, string sortDirection)
    {
        try
        {
            DataView dv = new DataView((DataTable)ViewState["gvDetails"]);
            dv.Sort = sortExpression + " " + sortDirection;
            ViewState["gvDetails "] = dv.ToTable();
            gv.DataSource = dv;
            gv.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Comments