Default Sort Order

Jan 15, 2014 at 3:45 PM
What is the intended method for controlling the initial sort order?

I understand "SortInitialDirection" can be applied to a column if the model collection is already sorted. But doesn't using it imply that the collection will get sorted twice? Once in the hard coded default order and then again by the "orderers" created by clicking on columns ("_settings.ColumnName") and any "ThenSortBy" methods?

What I'm looking for is a way to set a default value for "_settings.ColumnName". But as far as I've discovered, it only gets set in response to the query string.
Jan 15, 2014 at 5:20 PM
Edited Jan 15, 2014 at 5:27 PM
Initialize the grid as if "?grid-column=WhenModified&grid-dir=0" had been added to query string.
i.e. Add "SortDefaults" as a new grid customization method.
.WithPaging(15).Sortable().SortDefaults("WhenModified", GridSortDirection.Descending).Filterable().WithMultipleFilters()
Code changes:
- internal void UpdateColumnsSorting()
public void UpdateColumnsSorting()
/// <summary>
/// Call to update column IsSorted and Direction properties from SortSettings
/// </summary>
void UpdateColumnsSorting();
public IGridHtmlOptions<T> SortDefaults(string columnName, GridSortDirection direction) {
    var s = _source.Settings.SortSettings;
    if (string.IsNullOrEmpty(s.ColumnName)) {
        s.ColumnName = columnName;
        s.Direction = direction;
    return this;
/// <summary>
/// Provides default sort column and direction to be used when none are provided
/// on the query string.
/// </summary>
/// <param name="columnName"></param>
/// <param name="direction"></param>
/// <returns></returns>
IGridHtmlOptions<T> SortDefaults(string columnName, GridSortDirection direction);
Jan 17, 2014 at 4:35 AM

I understand that you care about twice OrderBy. Now grid does not have an api to setup default order operation, and I guess that easy to implement. But what problems you are having with that? Any performance issues?

Also if you are fetch you data from database and using IQueryable<T> interface, provided by EntityFramework - double order will not be executed on a database side.
Jan 17, 2014 at 5:38 AM
I may not fully understand how it's intended to work but I'm concerned about the performance against large tables.

The sort has to happen on the SQL server so that the paging (skip M, take N) can also happen there.

Otherwise a potentially enormous amount of data needs to be transferred to the web server only to immediately be resorted and mostly discarded.
Jan 17, 2014 at 3:16 PM
The code changes I listed above are working for me.
I was asking to know if I was missing something about how it was meant to work.

In general, looking at the SQL being generated and sent to the server I'm happy with the efficiency.
(looking at the value of itemsToProcess.ToString() at line 98 in GridBase.cs)
Jan 18, 2014 at 4:59 AM
If you use QueryProvider by EntityFramework (DbSet<T>) with the grid you don't need to care about double sorting because expression like:
will be translated to SQL statement like:
SELECT [fields] FROM [table] ORDER BY Field2
And in this case you can initial sort you collection. then pass to the grid and use SortInitialDirection method.

With your code customization you will may have a problems to upgrade the grid to the next versions.
Jan 18, 2014 at 10:44 AM
Thanks for this explanation.

That takes care of my efficiency concerns.