For doing these, we first create the model class as below:

  1. public class DisplayViewEntity
  2. {
  3. public List<Dictionary<string, string>> DisplayData { get; set; }
  4. public int TotalRecord { get; set; }
  5. public DisplayCommand DisplayArg { get; set; }
  6. }
  7. public class DisplayCommand
  8. {
  9. public string DisplayFieldText { get; set; }
  10. public string DisplayFieldName { get; set; }
  11. public string DisplayFieldWidth { get; set; }
  12. }
After it, we create action method in controller which return the specified data which will bind to the grid:
  1. public ActionResult GenericGrid()
  2. {
  3. List<Dictionary<string, string>> asd = new List<Dictionary<string, string>>();
  4. Dictionary<string, string> a = new Dictionary<string, string>();
  5. for (int i = 1; i <= 10; i++)
  6. {
  7. a = new Dictionary<string, string>();
  8. a.Add("A", "A" + i.ToString());
  9. a.Add("B", "B" + i.ToString());
  10. a.Add("C", "C" + i.ToString());
  11. a.Add("D", "D" + i.ToString());
  12. a.Add("E", "E" + i.ToString());
  13. asd.Add(a);
  14. }
  15. DisplayCommand fc = new DisplayCommand();
  16. fc.DisplayFieldName = "First Name,2nd Name,Third Name,Last Col";
  17. fc.DisplayFieldText = "A,B,C,D";
  18. fc.DisplayFieldWidth = "50,20,0,50";
  19. DisplayViewEntity fve = new DisplayViewEntity();
  20. fve.DisplayData = asd;
  21. fve.DisplayArg = fc;
  22. return View(fve);
  23. }
Now below is the code for view:
  1. @model TelerikMvcRndProject.Models.DisplayViewEntity
  2. @{
  3. ViewBag.Title = "GenericGrid";
  4. Layout = "~/Views/Shared/_Layout.cshtml";
  5. }
  6. <h2>Generic Grid</h2>
  7. <div id="grid"></div>
  8. <script>
  9. $(document).ready(function () {
  10. fnCreate_Grid(JSON.parse('@Html.Raw(Json.Encode(Model.FindData))'));
  11. });
  12. function fnCreate_Grid(gridData) {
  13. debugger;
  14. var a = JSON.parse('@Html.Raw(Json.Encode(Model.DisplayArg))');
  15. var DispFieldName = a.DisplayFieldText.split(",");
  16. var DispCaption = a.DisplayFieldName.split(",");
  17. var FieldWidth = a.DisplayFieldWidth.split(",");
  18. var columns = [];
  19. $.each(DispCaption, function (i) {
  20. columns.push({ field: DispFieldName[i], title: DispCaption[i], width: FieldWidth[i] + "%" });
  21. });
  22. var colData = JSON.stringify(columns);
  23. $('#grid').kendoGrid({
  24. dataSource: {
  25. type: 'json',
  26. data: gridData,
  27. pageSize: 7
  28. },
  29. selectable: "single",
  30. serverPaging: true,
  31. height: 300,
  32. pageSize: 10,
  33. pageable: {
  34. refresh: false,
  35. pageSizes: false,
  36. buttonCount: 10
  37. },
  38. columns: columns,
  39. });
  40. }
  41. </script>