Sample Code:

View (cshtml):
@model IEnumerable<ASPRazorWebGridSample.Models.Person>
@using ASPRazorWebGridSample.UI;

@{
    ViewBag.Title = "ASPRazorWebGrid";
}

<h2>ASPRazorWebGrid Sample</h2>

@{
    var grid = new ASPRazorWebGrid(source: Model);
}

@grid.GetHtml(columns: new ASPRazorWebGridColumn[]{
                            grid.CreateColumn(dataField: "ID", canSort: true, headerHorizontalAlignment: "center", itemHorizontalAlignment: "center"),
                            grid.CreateColumn(dataField: "Name", canSort: true),
                            grid.CreateColumn(dataFieldFormat: (item) => item.Birthday.ToString("MMM dd, yyyy"), canSort: true, headerText: "Birthday"),
                            grid.CreateColumn(dataFieldFormat: @<text> @Html.ActionLink("Action","ProcessAction")</text>)
                        },
                  pager: new Pager { Alignment = PagerAlign.Center, TotalRows = ViewBag.GridRecordCount, RowsPerPage = 5, CssClass="pager" },
                  tableStyle: "table",
                  headerStyle: "head",
                  alternatingRowStyle: "alt")


Controller:
public class PeopleListController : Controller
    {
        //sample data source
        public List<Person> Persons 
        { 
            get {
                return new List<Person> { 
                    new Person {ID=1, Birthday=DateTime.Now, Name="John"},
                    new Person {ID=2, Birthday=DateTime.Now, Name="Paul"},
                    new Person {ID=3, Birthday=DateTime.Now, Name="Jane"},
                    new Person {ID=4, Birthday=DateTime.Now, Name="Trisha"},
                    new Person {ID=5, Birthday=DateTime.Now, Name="Kevin"},
                    new Person {ID=6, Birthday=DateTime.Now, Name="Janice"},
                    new Person {ID=7, Birthday=DateTime.Now, Name="Dennis"},
                    new Person {ID=8, Birthday=DateTime.Now, Name="Jack"},
                    new Person {ID=9, Birthday=DateTime.Now, Name="Trevor"},
                    new Person {ID=10, Birthday=DateTime.Now, Name="Michael"},
                    new Person {ID=11, Birthday=DateTime.Now, Name="Sean"},
                    new Person {ID=12, Birthday=DateTime.Now, Name="Emerson"},
                    new Person {ID=13, Birthday=DateTime.Now, Name="Jay"},
                    new Person {ID=14, Birthday=DateTime.Now, Name="Henry"},
                    new Person {ID=15, Birthday=DateTime.Now, Name="Luke"}
                };
            }
        }

        public ActionResult Index()
        {
            //get grid parameters from URL/POST (if any)
            var gridParameters = GridParameters.GetGridParameters();

            //get data
            var data = GetDataUsingLINQ(gridParameters.Sort,            //order by column
                                        gridParameters.SortDirection,   //order by direction
                                        gridParameters.Page ?? 1,       //returned page
                                        5);                             //displayed rows per page

            //set record count for use in view
            ViewBag.GridRecordCount = Persons.Count;

            return View(data);
        }

        //get data from datasource using LINQ (sample data access layer)
        private IEnumerable<Person> GetDataUsingLINQ(string sort, string sortDir, int page, int numRows)
        {
            if (!string.IsNullOrEmpty(sort))
                return Persons.AsQueryable().OrderBy(sort + " " + sortDir).Skip((page - 1) * numRows).Take(numRows).ToList();
            else
                return Persons.AsQueryable().Skip((page - 1) * numRows).Take(numRows).ToList();
        }

        //get data from datasource using SPROC (sample data access layer)
        private IEnumerable<Person> GetDataUsingSPROC(string sort, string sortDir, int? page, int numRows)
        {
            //note: I did not use EF nor LINQ to SQL in the example below as I do 
            //      not know any clean/elegant way of calling stored using those two

            //note: server side paging and sorting SP implementation is not included, there are lots of articles on the web that discuss about it.
            //      You can google "sql server side paging sorting"

            var sqlCon = new SqlConnection("<insert your connection string here>");
            sqlCon.Open();

            var sqlCmd = new SqlCommand();
            sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
            sqlCmd.CommandText = "<insert your SP name here>"; 
            sqlCmd.Connection = sqlCon;

            sqlCmd.Parameters.AddWithValue("@Sort", sort);
            sqlCmd.Parameters.AddWithValue("@SortDirection", sortDir);
            sqlCmd.Parameters.AddWithValue("@Page", page);
            sqlCmd.Parameters.AddWithValue("@NumRows", numRows);

            var list = new List<Person>();
            using(SqlDataReader reader = sqlCmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    var p = new Person();
                    p.ID = Convert.ToInt32(reader["ID"]);
                    p.Name = reader["Name"].ToString();
                    p.Birthday = Convert.ToDateTime(reader["Birthday"]);
                    list.Add(p);
                }
            }

            sqlCon.Close();

            return list;
        }
}


Go to downloads page to get the full working sample solution.

Last edited Mar 14, 2011 at 5:35 AM by jpdelmundo, version 2

Comments

No comments yet.