30605

Hierarchical (Multi-column) Sorting for the .net GridView?

Question:

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.

Answer1:

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 <a href="https://stackoverflow.com/questions/149380/dynamic-sorting-within-sql-stored-procedures" rel="nofollow">Dynamic Sorting within SQL Stored Procedures</a>, 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:

<ul><li>

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 <a href="http://ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref4/html/P_System_Data_DataView_Sort.htm" rel="nofollow">System.Data.DataView</a>).

</li> <li>

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

</li> <li>

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

</li> </ul>

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.

Answer2:

In order to get an ASP.NET GridView to correctly sort when multiple columns are listed in a BoundField's Sort specification, you need to bind the grid's OnSorting event to this function:

protected void gridViewSorting(object sender, GridViewSortEventArgs e) { // // This odd function permits GridView objects to sort on multiple columns // Without this function, a GridView object does not sort correctly when multiple // columns are named in its sort specification. // if (!(sender is GridView)) { return; } if (!e.SortExpression.Contains(',')) { return; } GridView gv = sender as GridView; // // Find the column that is to become the basis of the sort // foreach (DataControlField dc in gv.Columns) { String fieldSortExprClean = dc.SortExpression.Replace(" DESC", ""); fieldSortExprClean = fieldSortExprClean.Replace(" ASC", ""); String eventSortExprClean = e.SortExpression.Replace(" DESC", ""); eventSortExprClean = eventSortExprClean.Replace(" ASC", ""); if (fieldSortExprClean == eventSortExprClean) { if (e.SortDirection == SortDirection.Ascending) { dc.SortExpression = fieldSortExprClean.Replace(",", " ASC,"); e.SortExpression = fieldSortExprClean.Replace(",", " ASC,"); } else { dc.SortExpression = fieldSortExprClean.Replace(",", " DESC,"); e.SortExpression = fieldSortExprClean.Replace(",", " DESC,"); } } } }

Answer3:

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(); }

Recommend

  • Can Django use “external” python scripts linked to other libraries (NumPy, RPy2…)
  • Why is parser combinator “seq” defined with “bind” and “return”?
  • python pandas- AttributeError: 'Series' object has no attribute 'columns'?
  • State management in ReactJS
  • Console.log double logging from web workers
  • How can I find a style trigger-embedded element by name in WPF?
  • Why my input is showing one thread executing after another thread, not at the same time?
  • Stop a background process in flask without creating zombie processes
  • Android: Button background XML sometimes loses alpha when setting color filter
  • IE7 stops making network requests after a while
  • youtube video insert “onBehalfOfContentOwner” parameter value
  • Client JS + Django Rest Framework
  • How will Roslyn help me in avoiding a recompile to deploy changes to my ASP.NET website?
  • Control a pager (like the less program) from an extenal (parent) script
  • Ignore “The” when sorting a View in Drupal
  • How do I manage org and space users in bluemix using cf command line?
  • How to upload file on another domain?
  • Capturing HTML Text Input Key press after key has been applied?
  • C#: Import/Export Settings into/from a File
  • Syntax error on tokens, AnnotationName expected instead - error on query
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • How can I display the parent menu item's description using Wordpress walkers?
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • Authentication in Play! and RestEasy
  • Spring Cloud Microservice Architecture Confusion
  • Groovy: Unexpected token “:”
  • Transactional Create with Validation in ServiceStack Redis Client
  • How to use carriage return with multiple line?
  • Replace value with Factor in r data.table
  • Cancel a live stream “fast motion” catch-up in Flash
  • Handling un-mapped Rest path
  • How to access EntityManager inside Entity class in EJB3
  • Repeat a vertical line on every page in Report Builder / SSRS
  • QLineEdit password safety
  • PHP - How to update data to MySQL when click a radio button
  • How can I use Kendo UI with Razor?
  • embed rChart in Markdown
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app