jqGrid Sorting in asp .net mvc


This article is about sorting and mostly similar to the jqGrid Paging article explained earlier.


The sorting from the client side need not be explained as it is quite easier to find and it works automatically with tableToGrid if a value for sortname is specified. Let us check how we can sort from server side which helps in reducing the amount of data transmitted.

Setup jqGrid in Project:

We will reuse the same project which we have done in the jqGrid Paging article. Also for setting up the basic jqGrid project, please refer to the jqGrid Display article. Visit the current jqgrid download page to download the jqgrid code. You can download everything or atleast Make sure you download the following.

  • Base
  • Table to Grid

The rest of the modules may be useful in later articles.

Server Side Sorting with Paging on Ajax Loaded data

This method avoids the loading of the entire data at once thus removing the bottleneck on load time. This is suitable for loading large number of rows and the pages will load the sorted data faster.

The code snippet at the html side remains the same except that the loadonce attribute will be made false.

The server side code will be supplied with the current sorting field and sorting order using the options of jqGrid and the Controller/action should return the correct data.

public String GetAjaxPagedSortedGridData(int page, int rows, string sidx, string sord) 
    List persons = PersonRepository.GetAll(); 
    //Using a Extension method from stackoverflow 
    var sortedQuery = persons.AsQueryable().OrderBy(sidx, sord).ToList(); 
    ArrayList al = new ArrayList(); 
    int i = 1; int rowstart = (page * rows + 1) - rows; 
    int rowend = page * rows; 
    for (int j = rowstart; (j <= rowend && sortedQuery.Count > j); j++) 
       Person p = sortedQuery[j]; 
       var rowdata = new { id = i++, cell = p.GetGridString() }; 
       al.Add(rowdata); } 
       var griddata = new { total = persons.Count % rows > 0 ? (persons.Count / rows) + 1 : (persons.Count / rows), page = page, records = al.Count, rows = al.ToArray() }; JavaScriptSerializer js = new JavaScriptSerializer(); 
       string Serializeddata = js.Serialize(griddata); 
       return Serializeddata; 


Click the sample project for jqGrid sorting for downloading the sample project for Visual Studio 2010.