views:

543

answers:

4

Is there a simple ".Net" way to do hierarchical sorting

Table...

A|B|C
-----
1|2|5
2|8|4
2|4|3
3|7|2
4|4|1

clicking on A, then B would get you..

A|B|C
-----
1|2|5
2|4|3
2|8|4
3|7|2
4|4|1

The change being that I'm sorting (B) in context of (A) and so forth.

Obviously this could be managed in the datasource, but was wondering if someone has a elegant and scalable solution.. thanks.

+1  A: 

ASPX page

<asp:GridView id="MyGridView" runat="server" AllowSorting="true" OnSorting="MyGridView_OnSorting">
    <asp:BoundField DataField="ColumnA" SortExpression="A" />
    <asp:BoundField DataField="ColumnB" SortExpression="B" />
    <asp:BoundField DataField="ColumnC" SortExpression="C" />
</asp:GridView>

Code Behind

protected void MyGridView_OnSorting(object sender, GridViewSortEventArgs e)
{
    List<MyEntity> data = MyBLL.GetDataSource();

    data.Sort(delegate(MyEntity x, MyEntity y) {
        switch(e.SortExpression)
        {
            case "ColumnA":
               return String.Compare(x.ColumnA, y.ColumnA);
               break;
            case "ColumnB":
               return String.Compare(x.ColumnB, y.ColumnB);
               break;
            case "ColumnC":
               return String.Compare(x.ColumnC, y.ColumnC);
               break;
        }
    }
    );

    MyGridView.DataSource = data;
    MyGridView.DataBind();
}
Michael Kniskern
Unless I'm going crazy, this will only sort on one column, right?
Jeff Sternal
yes...at least 15 characters
Michael Kniskern
Ah.. yes.. won't work then.. needs to sort on multiple columns..
madcolor
So if you click column A, you need to sort in the following order A,B,C? If the user clicks B, then the sort order is B,A,C? If this the way you expect the sorting to function?
Michael Kniskern
Here's another example in this scenario.. A user clicks A then C. Grid should be sorted A first, then C.
madcolor
+1  A: 

If you're asking about doing it in conjunction with paging, there's no simple and scalable solution. In fact, that is kind of a holy grail of business application web development. See, for example, the StackOverflow question Dynamic Sorting within SQL Stored Procedures, which concerns the same thing. After all, if we had dynamic sorting on our database servers, we would only have to code the mechanism for managing the user's sort choices.

You really only have three options for multi-column sorts:

  • Do it in the client, letting your data container do the heavy lifting (when you're using a data container that has this functionality built in, like System.Data.DataView).

  • Write your own algorithm and sort the data yourself before binding.

  • Do it at the database server via one of the solutions discussed in the link above.

Neither of the client-side solutions are really scalable since they involve pulling and delivering all your data when you may only need a subset.

Jeff Sternal
+1  A: 

Please see below link for sorting when paging is enable and sorting is done across multiple pages of the gridview control http://asimsajjad.blogspot.com/2009/05/gridview-paging-and-sorting.html

In the code in above link what you have to do is to change the SortExpresion view state of the page.

 private string GridViewSortExpression
    {
        get
        {
            return ViewState["SortExpression"] as string ?? string.Empty;
        }
        set
        {
            if (ViewState["SortExpression"] == null)
                ViewState["SortExpression"] = value;
            else if (ViewState["SortExpression"].ToString().Contains(value) == false)
                ViewState["SortExpression"] += "," + value;

        }
    }

here you have to save the multiple column name by which you want the sorting of the grid view record sorting. what you have to do is to save the column name separated by comma(,) which is requirement of the sorting. Hope this will help you.

Asim Sajjad