views:

145

answers:

1

Hello, I have a GridView that get's its datasource from a complex object. So I'm doing the Sorting & Paging in the code behind.

<asp:GridView ID="SystemsDetailList" runat="server" AllowSorting="true" AllowPaging="true"
AutoGenerateColumns="False" ShowFooter="True" OnPageIndexChanging="gridView_PageIndexChanging" OnSorting="gridView_Sorting">

For an important title column I have a SortExpression with 2 fields:

SortExpression="FunctionalAreaDisplayCode, EswbsDisplayCode"

This in the code behind:

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            BindSystemList(e.SortExpression, sortOrder);
        }

    public string sortOrder
    {
        get
        {
            if (ViewState["sortOrder"].ToString() == "desc")
            {
                ViewState["sortOrder"] = "asc";
            }
            else
            {
                ViewState["sortOrder"] = "desc";
            }

            return ViewState["sortOrder"].ToString();
        }
        set
        {
            ViewState["sortOrder"] = value;
        }
    }

For some reason it will keep "FunctionalAreaDisplayCode" always sorted ASC but the EswbsDisplayCode works fine as it flips between ASC and DESC correctly.

and tips here?

thanks!

A: 

My guess is that it's creating an SQL ORDER BY clause by simply appending the Sort order to the sort expression. So you will have:

FunctionalAreaDisplayCode, EswbsDisplayCode asc

and

FunctionalAreaDisplayCode, EswbsDisplayCode desc

which are interpreted by SQL (or whatever is doing the sorting) as:

FunctionalAreaDisplayCode asc, EswbsDisplayCode asc

and

FunctionalAreaDisplayCode asc, EswbsDisplayCode desc

See this ORDER BY tutorial :

It’s important to remember that whenever you are ordering by more than one column, you need to specify ASC and/or DESC after each column

Hugh

Hugh