Monday, July 18, 2011

MVC3 Webgrid HTML helper Initial Descending Sorting

Monday, July 18, 2011 Posted by Unknown , , , ,
There is no option in the webgrid constructor to set the initial sort order. By default it wil sort ascending. When you have a list of dates and want to sort this in descending order you can do it the following way:

Add the following lines of razor code just behind the creation of the webgrid helper:
if (Request.QueryString[prigrid.SortDirectionFieldName].IsEmpty()) {
  prigrid.SortDirection = SortDirection.Descending;

When no sorting is selected it will set the order to descending.