How to change the default sort order to descending?

10.5k Views Asked by At

How do I change the defaultSort of my webGrid to be in the opposite/descending order? If it were SQL, I'd be adding a DESC somewhere. Here's my working line of code for an Ascending sort:

var grid = new WebGrid(dq, rowsPerPage: 50, defaultSort: "UWDate", ajaxUpdateContainerId: "grid" );

It correctly sorts on the UWDate column in Ascending order, but I would like it to sort the opposite/descending order.

4

There are 4 best solutions below

0
On BEST ANSWER

Another option is after your grid initialization:

    grid.SortDirection = SortDirection.Descending;
0
On
@{

    WebGrid grid = new WebGrid(Model, null, null, 10, true, true, ajaxUpdateContainerId: "container-grid2");
}
0
On

You can change the sort order within the WebGrid initialiser by putting a space followed by DESC within the defaultSort parameter string.

var grid = new WebGrid(Model, defaultSort: "UWDate DESC" ... );

Source: http://forums.asp.net/post/4962796.aspx

0
On

If you are using entity framework then you can use this syntax for ordering to descending order.

WebGrid grid = new WebGrid(Model.OrderByDescending(o=>o.Id),canPage: true);

This is the design view syntax for the grid print of the model.

@grid.Gethtml()

This will print the webgrid in descending order directly in the view of the page MVC.