jqGrid supports tree-grid mode, where you can display rows of data of the same structure (column definition)
as hierarchial tree structure. In your model, just set the TreeGridSettings.Enabled property to true
and the grid will be assuming tree-mode response from the server (see the Model code tab for details).
This means that in addition to the row data, you need to return data that will help jqGrid determine the hierarchy of the rows.

When selecting rows, you need to specify some or all of the following reserved fields

  • tree_level - the level of the row in the tree hierarchy. 0 is for root row.

  • tree_leaf - if the row is leaf (no child rows). This will make it non-expandable and will have an alternative icon.

  • tree_expanded - if the row is expanded by default or not.

  • tree_parent - the primary key (ID) of the parent row (only applicable for child rows)

  • tree_loaded - true/false - if the tree nodes are already loaded. Can be used to show several levels expanded by default.


You can also control the different images per grid rows - in expanded, collapsed and leaf states. This can be achieved with the following properties available from the TreeGridSettings collection - ExpandedIcon, CollapsedIcon, LeafIcon
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Trirand.Web.Mvc;
using System.Web.UI.WebControls;

namespace MVCDemo.Models
{
    public class SimpleTreeJqGridModel
    {   
        public JQGrid SimpleTreeDataGrid { get; set; }

        public SimpleTreeJqGridModel()
        {
            SimpleTreeDataGrid = new JQGrid
            {
                Columns = new List<JQGridColumn>()
                                 {
                                     new JQGridColumn { DataField = "ID",
                                                        PrimaryKey = true,
                                                        Visible = false,
                                                      },
                                     new JQGridColumn { DataField = "Name", Width = 200 }
                                 },
                Width = Unit.Pixel(500),
                Height = Unit.Percentage(300),
                TreeGridSettings = new TreeGridSettings
                {
                    Enabled = true
                }
            };
        }
    }
}
@model MVCDemo.Models.SimpleTreeJqGridModel
@using Trirand.Web.Mvc
@using MVCDemo.Models

<!DOCTYPE html>
<html lang="en-us">
<head>
    <meta charset="utf-8">
    <title>jqGrid for ASP.NET MVC - Tree Grid - Simple Tree</title>
    <!-- jQuery runtime minified -->
    <script src="~/Scripts/jquery-3.0.0.min.js" type="text/javascript"></script>
    <!-- The jqGrid localization file we need, English in this case -->
    <script type="text/javascript" src="../../Scripts/trirand/i18n/grid.locale-en.js"></script>
    <!-- The jqGrid client-side javascript -->
    <script type="text/javascript" src="../../Scripts/trirand/jquery.jqGrid.min.js"></script>

    <!-- The jQuery UI theme that will be used by the grid. -->
    <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.11.4/themes/redmond/jquery-ui.css" />
    <!-- The jQuery UI theme extension jqGrid needs -->    
    <link rel="stylesheet" type="text/css" href="../../Content/themes/ui.jqgrid.css" />   
    
    <style type="text">
         /* change the default CSS classes of jqGrid to emulate simple tree */ 
        .ui-jqgrid tr.jqgrow td  { border: 0px none;} 
        .ui-jqgrid-htable,
        .ui-jqgrid-hbox { visibility: hidden }
    </style>
</head>    
    
<body>
    <div>           
        @Html.Trirand().JQGrid(Model.SimpleTreeDataGrid, "SimpleTreeView")
    </div>
    <br /><br />
    <div>
                  
    </div>   
    
    <script type="text/javascript">
    
    function formatGrid()
    {
        $('.ui-jqgrid-htable','.ui-jqgrid-hbox').hide();
        $('tr.jqgrow td').css({'border':'0px none'});
    }
    
    </script> 
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MVCDemo.Models;
using System.Web.UI.WebControls;
using Trirand.Web.Mvc;

namespace MVCDemo.Controllers.Grid
{
    public partial class GridController : Controller
    {
        // This is the default action for the View. Use it to setup your grid Model.
        public ActionResult TreeSimpleTree()
        {
            // Get the model (setup) of the grid defined in the /Models folder.
            var gridModel = new SimpleTreeJqGridModel();
            var grid = gridModel.SimpleTreeDataGrid;            
            
            // Setting the DataUrl to an action (method) in the controller is required.
            // This action will return the data needed by the grid
            grid.DataUrl = Url.Action("TreeSimpleTree_DataRequested");

            // call a javascript event on grid initialized to format the grid to look like a tree
            grid.ClientSideEvents.GridInitialized = "formatGrid";
            
            // set grid tree mode to on.
            grid.TreeGridSettings.Enabled = true;
            grid.ToolBarSettings.ToolBarPosition = ToolBarPosition.Hidden;
            
            // Pass the custmomized grid model to the View
            return View(gridModel);
        }       

        // This method is called when the grid requests data. You can choose any method to call
        // by setting the JQGrid.DataUrl property
        public JsonResult TreeSimpleTree_DataRequested()
        {
            // Get both the grid Model and the data Model
            // The data model in our case is an autogenerated linq2sql database based on Northwind.
            var northWindModel = new NorthwindDataContext();
            var gridModel = new SimpleTreeJqGridModel();                        
            var grid = gridModel.SimpleTreeDataGrid;            

            // ********************************************************************************************
            // when selecting rows, you need to specify some or all of the following reserved fields
            // tree_level - the level of the row in the tree hierarchy. 0 is for root row.
            // tree_leaf - if the row is leaf (no child rows). This will make it non-expandable and will have an alternative icon
            // tree_expanded - if the row is expanded by default or not
            // tree_parent - the primary key (ID) of the parent row (only applicable for child rows)
            // tree_loaded - true/false - if the tree nodes are already loaded. Can be used to show several levels expanded by default.
            // ********************************************************************************************


            // Get all grid rows in a cached List of the underlying type (RetailData).
            List<RetailData> cachedGridRows = (from product in northWindModel.RetailDatas
                                               select product).ToList();

            // Use the cache to calculate tree_loaded, tree_parent, tree_level, tree_leaf and tree_expanded
            var hierarchyRows = from product in cachedGridRows
                                select new
                                {
                                    ID = product.ID,
                                    Name = product.Name,
                                    tree_parent = product.ParentID,                                    
                                    tree_level = TreeSimpleTree_GetRowLevel(product.ParentID, cachedGridRows),
                                    tree_leaf = TreeSimpleTree_IsLeafRow(product.ID, cachedGridRows),
                                    tree_expanded = true,
                                    tree_loaded = true
                          };

            return grid.DataBind(hierarchyRows.AsQueryable());            
        }

        private int TreeSimpleTree_GetRowLevel(int? parentID, List<RetailData> cachedGridRows)
        {
            int level = 0;
            while (parentID != null)
            {
                level++;
                var result = from product in cachedGridRows
                                 where product.ID == parentID
                                 select product;

                parentID = result.Single<RetailData>().ParentID;
            }
            
            return level;
        }

        private bool TreeSimpleTree_IsLeafRow(int? productID, List<RetailData> cachedGridRows)
        {
            foreach (RetailData retailData in cachedGridRows)
            {
                if (retailData.ParentID == productID)
                {
                    return false;
                }
            }

            return true;
        }
    }
}
Switch theme:


Theming is based on the very popular jQuery ThemeRoller standard. This is the same theming mechanism used by jQuery UI and is now a de-facto standard for jQuery based components. The benefits of using ThemeRoller are huge. We can offer a big set of ready to use themes created by professional designers, including Windows-like themes (Redmond), Apple-like theme (Cupertino), etc. In addition to that any jQuery UI controls on the same page will pick the same theme.

Last, but not least, you can always roll your own ThemeRoller theme, using the superb Theme Editor

To use a theme, simply reference 2 Css files in your Html <head> section - the ThemeRoller theme you wish to use, and the jqGrid own ThemeRoller Css file. For example (Redmond theme):

<link rel="stylesheet" type="text/css" media="screen" href="/themes/redmond/jquery-ui-1.8.2.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="/themes/ui.jqgrid.css" />