2

Its Arun. This time I have a sorting issue in ASP.Net. For the first click, descending is working fine but on the second click ascending is not taken again. Its still in the descending order. I m using Tableadapter to display the gridview content. Please review the code and correct me where I ve missed.

    protected void gv1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sdir = e.SortDirection == SortDirection.Ascending ? "DESC" : "ASC";
        DataView dv = new DataView(ds2.AllocationPending(ClientLoggedIn.Text));
        dv.Sort = e.SortExpression + " " + sdir;
        gv1.DataSource = dv;
        gv1.DataBind();
    }

Also please explain - Is there any other way to apply sorting without Dataview.

Thank you .

1

2 Answers 2

2

I have found the solution for this issue. The reason is e.SortDirection always returns Ascending. So I need to store the e.SortDirection in a ViewState and sort the dataview with that value. Updated coding follows:

    protected void gv1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string SortDirection = "DESC";
        if (ViewState["SortExpression"] != null)
        {
            if (ViewState["SortExpression"].ToString() == e.SortExpression)
            {
                ViewState["SortExpression"] = null;
                SortDirection = "ASC";
            }
            else
            {
                ViewState["SortExpression"] = e.SortExpression;
            }
        }
        else
        {
            ViewState["SortExpression"] = e.SortExpression;
        }

        DataView dv = new DataView(ds2.AllocationPending(ClientLoggedIn.Text));
        dv.Sort = e.SortExpression + " " + SortDirection;
        gv1.DataSource = dv;
        gv1.DataBind();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

I don't get for what e.SortDirection is used then... Microsoft actually says that it should change automatically, if you click the same column header twice...
1
 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {      
        DataTable dtSortTable = GridView1.DataSource as DataTable;

        if (dtSortTable != null)
        {
            DataView dvSortedView = new DataView(dtSortTable);

            dvSortedView.Sort = e.SortExpression + "" + getSortDirectionString(e.SortDirection);

            GridView1.DataSource = dvSortedView;
            GridView1.DataBind();
        }
    }

private string getSortDirectionString(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;
        if(sortDirection== SortDirection.Ascending)
        {
            newSortDirection = "DESC";
        }
        else
        {
            newSortDirection = "ASC";
        }
        return newSortDirection;
}

Try this code for sorting the gridview

4 Comments

I m not using the Radgrid. I m developing the ASP.Net Gridview. The above code is applicable to Radgrid only. So no use for me.
Thank you Chetan for the gridview code. But its the same code logic which I posted in my question. You called the Sortdirection as a separate function whereas I included in the same one. Also a small correction in your code - if(sortDirection== SortDirection.Ascending) then newSortDirection = "DESC"; Anyway its working fine for the first time but not the second time. Please look on that.
I have found the solution for this issue. The reason is e.SortDirection always returns Ascending. So I need to store the e.SortDirection in a ViewState and sort the dataview with that value. Updated coding follows:
string SortDirection = "DESC"; if (ViewState["SortExpression"] != null) { if (ViewState["SortExpression"].ToString() == e.SortExpression) { ViewState["SortExpression"] = null; SortDirection = "ASC"; } else { ViewState["SortExpression"] = e.SortExpression; } } else { ViewState["SortExpression"] = e.SortExpression; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.