Paging Grid

Id First Name Last Name
370PatrickAdams
493KarenAdams
570NancyAdams
599EricAdams
635ChristineAdams
663BonnieAdams
778RonaldAdams
820SeanAdams
897JudithAdams
857MichaelAlexander
Showing 1 to 10 of 1000 entries

Code

Inside MVCGridConfig.cs
MVCGridDefinitionTable.Add("PagingGrid", new MVCGridBuilder<Person>(colDefauls)
    .WithAuthorizationType(AuthorizationType.AllowAnonymous)
    .AddColumns(cols =>
    {
        cols.Add("Id").WithSorting(false)
            .WithValueExpression(p => p.Id.ToString());
        cols.Add("FirstName").WithHeaderText("First Name")
            .WithValueExpression(p => p.FirstName);
        cols.Add("LastName").WithHeaderText("Last Name")
            .WithValueExpression(p => p.LastName);
    })
    .WithSorting(true, "LastName")
    .WithPaging(true, 10)
    .WithRetrieveDataMethod((context) =>
    {
        var options = context.QueryOptions;
        var result = new QueryResult<Person>();
        using (var db = new SampleDatabaseEntities())
        {
            var query = db.People.AsQueryable();
            result.TotalRecords = query.Count();
            if (!String.IsNullOrWhiteSpace(options.SortColumnName))
            {
                switch (options.SortColumnName.ToLower())
                {
                    case "firstname":
                        query = query.OrderBy(p => p.FirstName, options.SortDirection);
                        break;
                    case "lastname":
                        query = query.OrderBy(p => p.LastName, options.SortDirection);
                        break;
                }
            }
            if (options.GetLimitOffset().HasValue)
            {
                query = query.Skip(options.GetLimitOffset().Value).Take(options.GetLimitRowcount().Value);
            }
            result.Items = query.ToList();
        }
        return result;
    })
);
View
@Html.MVCGrid("PagingGrid")