This example shows how to configure Telerik Grid for ASP.NET MVC to use custom server binding. By default the Grid is using the built-in Linq expression engine. However in some cases you may want to perform the paging, sorting, filtering and grouping by yourself. This examples shows how.

The required steps are:

  1. Configure the grid to use custom binding
    <%= Html.Telerik().Grid(Model)
            .Name("Grid")
            .EnableCustomBinding(true)
    %>
    
  2. If using grouping you will need to set BindTo method to the data from which the grid will be populated and explicitly declare the item's type.
    <%= Html.Telerik().Grid<Order>()
            .Name("Grid")
            .BindTo(Model)
            .EnableCustomBinding(true)
    %>
    
  3. If using paging you need to set the Total specifying the total number of records the grid is going to display. This is required for the proper pager configuration.
    <%= Html.Telerik().Grid(Model)
            .Name("Grid")
            .EnableCustomBinding(true)
            .Pageable(paging => paging.Total((int)ViewData["total"]))
    %>
    
  4. The action method which returns data for the grid accepts an argument of type GridCommand. The command object contains all required information to data bind the grid - current page, sorted columns, filter and groups. The action method must be decorated with the GridAction attribute.
    [GridAction]
    public ActionResult _CustomBinding(GridCommand command)
    {
        IEnumerable data = GetData(command);
        var dataContext = new NorthwindDataContext();
        ViewData["total"] = dataContext.Orders.Count();
        return View(data);
    }
    
  5. The GetData method from the previous step performs the actual paging and sorting based on the data in the command argument. Check the controller code for the implementation.