C#: Custom sort of DataGridView

Take a look at this MSDN page and this blog post. In principle, you need to configure the sorting at the data source (whether its an ObjectDataSource or a SqlDataSource) not at the GridView.

As far as I can tell the DataView class doesn’t support anything other than a simple ascending/decending sort. Without seeing the code where you load and bind the data it’s hard to make a specific recommendation, but you could either:

  1. Load your data into a List instead of a DataTable, call the Sort method passing in your comparison method and then bind to that list.
  2. Create an ObjectDataSource in your aspx code that gets the data directly from a class, and configure that ObjectDataSource to use your IComparer.

Leave a Comment