Introduction

Today, we have numerous Grid Controls used in MVC applications. Here, in this article, I will share with you a new control named jqxTreeGrid that allow us to create Nested Grid with the data provided from the database.

Create Table

You will find the table used in our application, here.

Table

After creating the table, you can fill it using data, as shown below:

Table

Create your MVC application

First, open Visual Studio. Then, click on file > New > project and name your project, as follows:

 MVC application

 MVC application

Creating ADO.NET Entity Data Model

You need to follow the necessary steps, as described below, for having the same result.

Right click on project name. Then, click Add > Add New Item. In the dialog box displayed, select Data > click on Add button.

ADO.NET Entity Data Model

ADO.NET Entity Data Model

Click on next button. You will have the dialog box, as shown below. You need to choose your server name and select your database. Finally, click on OK.

server name

server name

Here, we select the table (in our case, Employees) that we have created and click on finish button.

Finally, we see that the EDMX model will generate Employees class.

Employees

Create a controller

Now, we proceed to create a Controller. Right click on Controller folder > Add > Controller > Enter Controller name (in our case ‘HomeController’).

HomeController.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace TreeGridMVC4.Controllers
  7. {
  8. public class HomeController : Controller
  9. {
  10. // DbContext
  11. private Db_PersonEntities db = new Db_PersonEntities();
  12. //
  13. // GET: /Home/
  14. public ActionResult Index()
  15. {
  16. return View();
  17. }
  18. public JsonResult GetEmployees()
  19. {
  20. var employees = db.Employees.ToList();
  21. return Json(employees, JsonRequestBehavior.AllowGet);
  22. }
  23. }
  24. }
As you can see, I am creating GetEmployees() action that should be used to retrieve the data from Employees table, and return output as json format.

Create a strongly typed view

Now, we are going to create a strongly typed view.

view

When our view has been created, we need to add some references, first, as shown below:
  1. <!-- CSS Link -->
  2. <link rel="stylesheet" href="http://jqwidgets.com/jquery-widgets-demo/jqwidgets/styles/jqx.base.css" type="text/css" />
  3. <!-- Js Scripts -->
  4. <script type="text/javascript" src="http://jqwidgets.com/jquery-widgets-demo/scripts/jquery-1.11.1.min.js"></script>
  5. <script type="text/javascript" src="http://jqwidgets.com/jquery-widgets-demo/jqwidgets/jqxcore.js"></script>
  6. <script type="text/javascript" src="http://jqwidgets.com/jquery-widgets-demo/jqwidgets/jqxdata.js"></script>
  7. <script type="text/javascript" src="http://jqwidgets.com/jquery-widgets-demo/jqwidgets/jqxbuttons.js"></script>
  8. <script type="text/javascript" src="http://jqwidgets.com/jquery-widgets-demo/jqwidgets/jqxscrollbar.js"></script>
  9. <script type="text/javascript" src="http://jqwidgets.com/jquery-widgets-demo/jqwidgets/jqxdatatable.js"></script>
  10. <script type="text/javascript" src="http://jqwidgets.com/jquery-widgets-demo/jqwidgets/jqxtreegrid.js"></script>
  11. jqxTreeGrid implementation
  12. <script type="text/javascript">
  13. $(document).ready(function () {
  14. var source =
  15. {
  16. dataType: "json",
  17. dataFields: [
  18. { name: 'EmployeeID', type: 'number' },
  19. { name: 'FirstName', type: 'string' },
  20. { name: 'LastName', type: 'string' },
  21. { name: 'Title', type: 'string' },
  22. { name: 'ReportTo', type: 'number' },
  23. { name: 'Country', type: 'string' },
  24. { name: 'City', type: 'string' }
  25. ],
  26. hierarchy:
  27. {
  28. keyDataField: { name: 'EmployeeID' },
  29. parentDataField: { name: 'ReportTo' }
  30. },
  31. id: 'EmployeeID',
  32. url: "Home/GetEmployees"
  33. };
  34. var dataAdapter = new $.jqx.dataAdapter(source);
  35. // call jqxTreeGrid
  36. $("#treeGrid").jqxTreeGrid(
  37. {
  38. width: 700,
  39. source: dataAdapter,
  40. pageable: true,
  41. sortable: true,
  42. ready: function () {
  43. $("#treeGrid").jqxTreeGrid('expandRow', '2');
  44. },
  45. //choose the columns you wish to display on screen
  46. columns: [
  47. { text: 'First Name', dataField: 'FirstName', width: 150 },
  48. { text: 'Last Name', dataField: 'LastName', width: 150 },
  49. { text: 'Title', dataField: 'Title', width: 200 },
  50. { text: 'Country', dataField: 'Country', width: 200 }
  51. ]
  52. });
  53. });
  54. </script>
In the source part, we need to prepare 4 things:

Index.cshtml

  1. @model IEnumerable<TreeGridMVC4.Employees>
  2. @{
  3. ViewBag.Title = "Index";
  4. }
  5. @section scripts {
  6. <!-- CSS Link -->
  7. <link rel="stylesheet" href="http://jqwidgets.com/jquery-widgets-demo/jqwidgets/styles/jqx.base.css" type="text/css" />
  8. <!-- Js Scripts -->
  9. <script type="text/javascript" src="http://jqwidgets.com/jquery-widgets-demo/scripts/jquery-1.11.1.min.js"></script>
  10. <script type="text/javascript" src="http://jqwidgets.com/jquery-widgets-demo/jqwidgets/jqxcore.js"></script>
  11. <script type="text/javascript" src="http://jqwidgets.com/jquery-widgets-demo/jqwidgets/jqxdata.js"></script>
  12. <script type="text/javascript" src="http://jqwidgets.com/jquery-widgets-demo/jqwidgets/jqxbuttons.js"></script>
  13. <script type="text/javascript" src="http://jqwidgets.com/jquery-widgets-demo/jqwidgets/jqxscrollbar.js"></script>
  14. <script type="text/javascript" src="http://jqwidgets.com/jquery-widgets-demo/jqwidgets/jqxdatatable.js"></script>
  15. <script type="text/javascript" src="http://jqwidgets.com/jquery-widgets-demo/jqwidgets/jqxtreegrid.js"></script>
  16. <script type="text/javascript">
  17. $(document).ready(function () {
  18. var source =
  19. {
  20. dataType: "json",
  21. dataFields: [
  22. { name: 'EmployeeID', type: 'number' },
  23. { name: 'FirstName', type: 'string' },
  24. { name: 'LastName', type: 'string' },
  25. { name: 'Title', type: 'string' },
  26. { name: 'ReportTo', type: 'number' },
  27. { name: 'Country', type: 'string' },
  28. { name: 'City', type: 'string' }
  29. ],
  30. hierarchy:
  31. {
  32. keyDataField: { name: 'EmployeeID' },
  33. parentDataField: { name: 'ReportTo' }
  34. },
  35. id: 'EmployeeID',
  36. url: "Home/GetEmployees"
  37. };
  38. var dataAdapter = new $.jqx.dataAdapter(source);
  39. // call jqxTreeGrid
  40. $("#treeGrid").jqxTreeGrid(
  41. {
  42. width: 700,
  43. source: dataAdapter,
  44. pageable: true,
  45. sortable: true,
  46. ready: function () {
  47. $("#treeGrid").jqxTreeGrid('expandRow', '2');
  48. },
  49. //choose the columns you wish to display on screen
  50. columns: [
  51. { text: 'First Name', dataField: 'FirstName', width: 150 },
  52. { text: 'Last Name', dataField: 'LastName', width: 150 },
  53. { text: 'Title', dataField: 'Title', width: 200 },
  54. { text: 'Country', dataField: 'Country', width: 200 }
  55. ]
  56. });
  57. });
  58. </script>
  59. }
  60. <h2>TreeGrid - MVC 4</h2>
  61. <div id="treeGrid"></div>
Output

Output