Using the Telerik Kendo UI, we can populate a grid using jQeury. For this, we must first create an Employee Model class as below:

  1. public class EmployeeInfo
  2. {
  3. [DisplayName("Employee Name")]
  4. public string EmployeeName { get; set; }
  5. [DisplayName("Present City")]
  6. public string City { get; set; }
  7. [DisplayName("Current State")]
  8. public string State { get; set; }
  9. [DisplayName("Phone Number")]
  10. public string PhoneNo { get; set; }
  11. }
To populate data, we provide the following method:

  1. public List<EmployeeInfo> EmployeeData()
  2. {
  3. List<EmployeeInfo> lstEmp = new List<EmployeeInfo>();
  4. lstEmp.Add(new EmployeeInfo { EmployeeName = "Ashok Sharma", City = "Kolkata", State = "West Bengal", PhoneNo = "03320203030" });
  5. lstEmp.Add(new EmployeeInfo { EmployeeName = "Ashok Sharma1", City = "Kolkata1", State = "West Bengal1", PhoneNo = "03320203031" });
  6. lstEmp.Add(new EmployeeInfo { EmployeeName = "Ashok Sharma2", City = "Kolkata2", State = "West Bengal2", PhoneNo = "03320203032" });
  7. lstEmp.Add(new EmployeeInfo { EmployeeName = "Ashok Sharma3", City = "Kolkata3", State = "West Bengal3", PhoneNo = "03320203033" });
  8. lstEmp.Add(new EmployeeInfo { EmployeeName = "Ashok Sharma4", City = "Kolkata4", State = "West Bengal4", PhoneNo = "03320203034" });
  9. lstEmp.Add(new EmployeeInfo { EmployeeName = "Ashok Sharma5", City = "Kolkata5", State = "West Bengal5", PhoneNo = "03320203035" });
  10. lstEmp.Add(new EmployeeInfo { EmployeeName = "Ashok Sharma6", City = "Kolkata6", State = "West Bengal6", PhoneNo = "03320203036" });
  11. lstEmp.Add(new EmployeeInfo { EmployeeName = "Ashok Sharma7", City = "Kolkata7", State = "West Bengal7", PhoneNo = "03320203037" });
  12. lstEmp.Add(new EmployeeInfo { EmployeeName = "Ashok Sharma8", City = "Kolkata8", State = "West Bengal8", PhoneNo = "03320203038" });
  13. lstEmp.Add(new EmployeeInfo { EmployeeName = "Ashok Sharma9", City = "Kolkata9", State = "West Bengal9", PhoneNo = "03320203039" });
  14. lstEmp.Add(new EmployeeInfo { EmployeeName = "Ashok Sharma10", City = "Kolkata10", State = "West Bengal10", PhoneNo = "033202030310" });
  15. lstEmp.Add(new EmployeeInfo { EmployeeName = "Ashok Sharma11", City = "Kolkata11", State = "West Bengal11", PhoneNo = "033202030311" });
  16. return lstEmp;
  17. }
Then provide the cotroller method to return the view with the model:
  1. public ActionResult GridWithScript()
  2. {
  3. List<EmployeeInfo> lstEmp = EmployeeData();
  4. return View(lstEmp);
  5. }
Then provide the following code for the view:
  1. @model List<TelerikMvcRndProject.Models.EmployeeInfo>
  2. @{
  3. ViewBag.Title = "GridWithScript";
  4. Layout = "~/Views/Shared/_Layout.cshtml";
  5. }
  6. <h2>Grid With Script</h2>
  7. <div id="grid"></div>
  8. <script>
  9. $(document).ready(function () {
  10. var model =@{@Html.Raw(Json.Encode(Model))};
  11. $('#grid').kendoGrid({
  12. dataSource: {
  13. data : model,
  14. pageSize :7
  15. },
  16. selectable : "single",
  17. schema: {
  18. model: {
  19. fields: {
  20. EmployeeName: { type: "string" },
  21. City: { type: "string" },
  22. State: { type: "string" },
  23. PhoneNo: { type: "string" },
  24. }
  25. }
  26. },
  27. sortable : {
  28. mode :"single",
  29. allowUnsort : "false"
  30. },
  31. serverPaging: true,
  32. height: 300,
  33. pageSize:10,
  34. pageable : {
  35. refresh : false,
  36. pageSizes : true,
  37. buttonCount:10
  38. },
  39. columns: [
  40. { field: "EmployeeName", title: "Employee Name", width: "25%" },
  41. { field: "City", title: "City", width: "150px" },
  42. { field: "State", title: "State", width: "150px" },
  43. { field: "PhoneNo", title: "Phone No", width: "150px" }
  44. ]
  45. });
  46. $("#grid").data("kendoGrid").bind("change", onRowSelection);
  47. var grid=$("#grid");
  48. $('#grid').data('kendoGrid').dataSource.bind('change',function(e){
  49. fnGridSort(e);
  50. });
  51. });
  52. function onRowSelection() {
  53. var a = this.dataItem(this.select());
  54. alert(a.EmployeeName);
  55. alert(a.City);
  56. alert(a.State);
  57. alert(a.PhoneNo);
  58. }
  59. function fnGridSort(e) {
  60. var gridHeader = $('#grid').data('kendoGrid').dataSource.sort();
  61. var columns = gridHeader[0];
  62. var sortOrder = columns.dir;
  63. var sortField=columns.field;
  64. alert(sortOrder + ' ' + sortField);
  65. }
  66. </script>
Also, on the click of a grid row, the data will be shown in an alert.