Sam
Sam

Reputation: 301

GridView sorting ascending is not working

I have a GridView with sorting enabled. For a single page result both the ascending and descending works fine. But when there are multiple pages, descending alone works well. the asceding also works but when i click on the next pages, it becomes unsorted again. I don't know whether the problem is because of the sort direction or paging. Kindly Help. Below are the codes:

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    GridView grid = sender as GridView;
    //Retrieve the table from the session object.
    DataTable dt = Session["List"] as DataTable;

    if (dt != null)
    {
        //Sort the data.
        dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
        grid.DataSource = Session["List"];
        grid.DataBind();
    }
}

private string GetSortDirection(string column)
{
    string sortDirection = "ASC";      
    string sortExpression = ViewState["SortExpression"] as string;
    if (sortExpression != null)
    {
        if (sortExpression == column)
        {
            string lastDirection = ViewState["SortDirection"] as string;
            if ((lastDirection != null) && (lastDirection == "ASC"))
            {
                sortDirection = "DESC";
            }
        }
    }
    // Save new values in ViewState.
    ViewState["SortDirection"] = sortDirection;
    ViewState["SortExpression"] = column;
    return sortDirection;
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataSource = bindGridView();
    GridView1.DataBind();
}

Upvotes: 2

Views: 1364

Answers (2)

Kanwar Singh
Kanwar Singh

Reputation: 908

You can use a session variable to store the latest Sort Expression and when you sort the grid next time compare the sort expression of the grid with the Session variable which stores last sort expression. If the columns are equal then check the direction of the previous sort and sort in the opposite direction.

DataTable sourceTable = GridAttendence.DataSource as DataTable;
DataView view = new DataView(sourceTable);
string[] sortData = Session["sortExpression"].ToString().Trim().Split(' ');
if (e.SortExpression == sortData[0])
 {
if (sortData[1] == "ASC")
{
    view.Sort = e.SortExpression + " " + "DESC";
    this.ViewState["sortExpression"] = e.SortExpression + " " + "DESC";
}
else
{
    view.Sort = e.SortExpression + " " + "ASC";
    this.ViewState["sortExpression"] = e.SortExpression + " " + "ASC";
}
 }
 else
{
view.Sort = e.SortExpression + " " + "ASC";
this.ViewState["sortExpression"] = e.SortExpression + " " + "ASC";
 }

Upvotes: 1

Sam
Sam

Reputation: 301

After so many research, I found the correct answer for my own question. This would probably help someone in future. Add the following code to the bindGridView()

if (ViewState["sortExpr"] != null)
{
     dv = new DataView(ds.Tables[0]);
     dv.Sort = (string)ViewState["sortExpr"];
}
else
    dv = ds.Tables[0].DefaultView;

and

#region Sorting
    protected void Gridview1_Sort(object sender, GridViewSortEventArgs e)
    {
        ViewState["sortExpr"] = e.SortExpression + " " + GetSortDirection(e.SortExpression); 
        Gridview1.DataSource = bindGridView();
        Gridview1.DataBind();
    }

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

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

        if (sortExpression != null)
        {
            // Check if the same column is being sorted.
            // Otherwise, the default value can be returned.
            if (sortExpression == column)
            {
                string lastDirection = ViewState["SortDirection"] as string;
                if ((lastDirection != null) && (lastDirection == "ASC"))
                {
                    sortDirection = "DESC";
                }
            }
        }
        // Save new values in ViewState.
        ViewState["SortDirection"] = sortDirection;
        ViewState["SortExpression"] = column;

        return sortDirection;
    }
    #endregion

Upvotes: 0

Related Questions