Click on a row to see all people the reports to this person




Self-referencing SQL table is a table that has ID -> ParentID relation to itself. One such example is the Employees table from the Northwind sample database (EmployeeID -> ReportsTo). You can have any number of nested self-referencing relations using only two grids.

Just set the HierarchySettings.HierarchyMode property of the parent grid to "Parent" and to "ParentAndChild" for the second grid.

Then, hook the client-side ClientSideEvents.OnClientSubGridRowExpanded event of both grids and call a special javascript function, which is autogenerated by child grids and is called showSubGrid_{GridID}, where {GridID} is the ID of the child grid, e.g. showSubGrid_JQGrid2

It's as simple as that, this will in essense trigger the sever-side OnDataRequesting of the grid and you can get the ID of the parent row and bind the child grid accordingly.

Complete hierarchy is achieved in this example with just 2 lines of code - 1 javascript and 1 C# (check the ASPX and C# tabs for details).
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="JQSuiteASPNETExample.examples.hierarchy.self_hierarchy._default" %>
<%@ Register Assembly="Trirand.Web" TagPrefix="trirand" Namespace="Trirand.Web.UI.WebControls" %>

<!DOCTYPE html>
<html lang="en-us">
<head id="Head1" runat="server">
    <meta charset="utf-8">
    <title>jqGrid ASP.NET Webforms - hierarchy with self-referencing table</title>
    <!-- The jQuery UI theme that will be used by the grid -->
    <link rel="stylesheet" type="text/css" media="screen" href="http://code.jquery.com/ui/1.12.1/themes/redmond/jquery-ui.css" />
    <!-- The jQuery UI theme extension jqGrid needs -->
    <link rel="stylesheet" type="text/css" media="screen" href="/themes/ui.jqgrid.css" />
    <!-- jQuery runtime minified -->
    <script src="/js/jquery-3.2.1.min.js" type="text/javascript"></script>
    <!-- The localization file we need, English in this case -->
    <script src="/js/trirand/i18n/grid.locale-en.js" type="text/javascript"></script>
    <!-- The jqGrid client-side javascript -->
    <script src="/js/trirand/jquery.jqGrid.min.js" type="text/javascript"></script>    
    
    <style type="text/css">
        body, html
        {
            font-size: 80%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager>    <asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:SQL2008_661086_trirandEntities %>"        
        SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [Title] FROM [Employees]">
    </asp:SqlDataSource>
    <asp:SqlDataSource runat="server" ID="SqlDataSource2" ConnectionString="<%$ ConnectionStrings:SQL2008_661086_trirandEntities %>"        
        SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [Title], [ReportsTo] FROM [Employees] WHERE ([ReportsTo] = @ReportsTo)">
        <SelectParameters>
            <asp:Parameter Name="ReportsTo" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>


    <span style="font-size: 135%; font-family: Verdana; font-weight: bold">Click on a row to see all people the reports to this person</span>
    
    <trirand:JQGrid runat="server" ID="JQGrid1" DataSourceID="SqlDataSource1" Width="650px" Height="300px">
        <Columns>
            <trirand:JQGridColumn DataField="EmployeeID" PrimaryKey="True" />
            <trirand:JQGridColumn DataField="LastName" />
            <trirand:JQGridColumn DataField="FirstName" />
            <trirand:JQGridColumn DataField="Title" />
        </Columns>        
        <HierarchySettings HierarchyMode="Parent" />
        <ClientSideEvents SubGridRowExpanded="showSubGrid" />
    </trirand:JQGrid>
    
    <br />
    <br />    
        
    <trirand:JQGrid runat="server" ID="JQGrid2" DataSourceID="SqlDataSource2" Width="550px" Height="100%"        
        OnDataRequesting="JQGrid2_DataRequesting">
        <Columns>
            <trirand:JQGridColumn DataField="EmployeeID" PrimaryKey="True" />
            <trirand:JQGridColumn DataField="LastName" />
            <trirand:JQGridColumn DataField="FirstName" />
            <trirand:JQGridColumn DataField="Title" />            
        </Columns>
        <ToolBarSettings ShowRefreshButton="true" ShowSearchButton="true" />
        <HierarchySettings HierarchyMode="ParentAndChild" />        
        <ClientSideEvents SubGridRowExpanded="showSubGrid" />
    </trirand:JQGrid>
    
    <script type="text/javascript">
    
        function showSubGrid(subgrid_id, row_id) 
        {
            // the "showSubGrid_JQGrid2" function is autogenerated and available globally on the page by the second child grid. 
            // Calling it will place the child grid below the parent expanded row and will call the OnDataRequesting event 
            // of the child grid, with ID equal to the ID of the parent expanded row
            showSubGrid_JQGrid2(subgrid_id, row_id);
        }

    
    </script>

    <br />
    <br />
    <trirand:codetabs runat="server" ID="tabs"></trirand:codetabs>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Trirand.Web.UI.WebControls;

namespace JQSuiteASPNETExample.examples.hierarchy.self_hierarchy
{
    public partial class _default : System.Web.UI.Page
    {
        public void JQGrid2_DataRequesting(object sender, JQGridDataRequestEventArgs e)
        {
            SqlDataSource2.SelectParameters["ReportsTo"].DefaultValue = e.ParentRowKey;
        }        
    }
}
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" />