Multiple child tables at the same level

In some scenarios you may need to display several child tables at the same level. This may include subgrids, custom html, etc. Let's consider the common scenario, where we list all Orders. We also want to drill-down and expand each Order, showing both the Employee handling it and the Customer the order should be shipped to at the same time. Here is how this scenario looks in jqGrid for ASP.NET

The approach to achieve that is very similar to what is needed in a classic 2 or 3-level hierarchy. The key here is to set the HierarchySettings.HierarchyMode of the parent grid to Parent and to hook its client-side event SubGridRowExpanded. This can be achieved in the following way:

<trirand:JQGrid runat="server" ID="JQGrid1" DataSourceID="SqlDataSource1" Width="650px" Height="100%"> <Columns> <trirand:JQGridColumn DataField="OrderID" HeaderText="Order ID" PrimaryKey="True" Width="50" /> <trirand:JQGridColumn DataField="CustomerID" Width="100" /> <trirand:JQGridColumn DataField="EmployeeID" Width="100" /> <trirand:JQGridColumn DataField="ShipCity" Width="100" /> <trirand:JQGridColumn DataField="Freight" Width="100" /> </Columns> <ClientSideEvents SubGridRowExpanded="showSubGrids" /> <HierarchySettings HierarchyMode="Parent" /> </trirand:JQGrid> <asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:SQL2008_449777_fhsConnectionString %>" SelectCommand="SELECT [OrderID], [CustomerID], [EmployeeID], [RequiredDate], [ShipCity], [Freight] FROM [Orders]"> </asp:SqlDataSource>

The showSubGrid javascript event handler is responsible for showing the child grid. Similar to what you would do in a 2 or 3-level scenario, just call the autogenerated showSubGrid_JQGrid2 and showSubGrid_JQGrid2 functions. The difference however is that you need to pass two additional parameters - the message to be shown next to the grid and the ID of the grid (must be unique).

<script type="text/javascript"> function showSubGrids(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, "<br><b>Employee responsible for this order</b><br><br>", "JQGrid2"); showSubGrid_JQGrid3(subgrid_id, row_id, "<br><b>This order should be shipped to thie customer</b><br><br>", "JQGrid3"); } </script>

The second and third grid - Employee and Customer - should be at the same level (children of the parent Orders grid). Therefore, we need to set their HierarchySettings.HierarchyMode to Child. Note how we form the SQL query - we need a join clause to find all employees and customers responsible for the selected parent order.

<trirand:JQGrid runat="server" ID="JQGrid2" DataSourceID="SqlDataSource2" Width="100%" 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> <HierarchySettings HierarchyMode="Child" /> </trirand:JQGrid> <trirand:JQGrid runat="server" ID="JQGrid3" DataSourceID="SqlDataSource3" Width="100%" Height="100%" OnDataRequesting="JQGrid3_DataRequesting"> <Columns> <trirand:JQGridColumn DataField="CustomerID" HeaderText="ID" PrimaryKey="True" Width="50" /> <trirand:JQGridColumn DataField="ContactName" /> <trirand:JQGridColumn DataField="Phone" /> <trirand:JQGridColumn DataField="City" /> </Columns> <HierarchySettings HierarchyMode="Child" /> </trirand:JQGrid> <asp:SqlDataSource runat="server" ID="SqlDataSource2" ConnectionString="<%$ ConnectionStrings:SQL2008_449777_fhsConnectionString %>" SelectCommand="SELECT Employees.EmployeeID,[LastName],[FirstName],[Title],[ReportsTo] FROM [Employees],[Orders] WHERE (Employees.EmployeeID = Orders.EmployeeID AND Orders.OrderID = @OrderID)"> <SelectParameters> <asp:Parameter Name="OrderID" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> <asp:SqlDataSource runat="server" ID="SqlDataSource3" ConnectionString="<%$ ConnectionStrings:SQL2008_449777_fhsConnectionString %>" SelectCommand="SELECT Customers.CustomerID, [CompanyName], [ContactName], [Phone], [City] FROM [Customers],[Orders] WHERE (Customers.CustomerID = Orders.CustomerID AND Orders.OrderID = @OrderID)"> <SelectParameters> <asp:Parameter Name="OrderID" Type="Int32" /> </SelectParameters> </asp:SqlDataSource>

Finally, just pass the parameters in the DataRequesting handlers of the grid:

public void JQGrid2_DataRequesting(object sender, JQGridDataRequestEventArgs e) { SqlDataSource2.SelectParameters["OrderID"].DefaultValue = e.ParentRowKey; } public void JQGrid3_DataRequesting(object sender, JQGridDataRequestEventArgs e) { SqlDataSource3.SelectParameters["OrderID"].DefaultValue = e.ParentRowKey; }


  Last Updated: 11/12/2009 | © Trirand, 2009