Header vs Details Grid

I have a class data source that loads the data in the parent grid correctly. I am programming Razor page using c# and ASP.NET

The class looks like below

Header Class:

  • string Name
  • string City
  • List<details> familymembers

Detail Class:

  • string relationToHeader
  • bool isAdult

Can you please advise and guide me in the right direction to have the details grid work correctly. Right now, the details are not loading.

Thank you


3 Replies

RS Rajapandiyan Settu Syncfusion Team December 10, 2021 10:56 AM UTC

Hi Sejal Sheth, 

Thanks for contacting Syncfusion support. 
 
We have prepared a simple sample with your exact scenario. In that sample, we have nested detail data (used for child-grid dataSource) in the parent-grid dataSource. By using detailDataBound event we dynamically load this nested data as child-Grid’s dataSource. Please refer the below code example and sample for more information.  



<div> 
@{  
       var ChildGrid = new Syncfusion.EJ2.Grids.Grid() { 
           QueryString = "EmployeeID",            
           Columns = new List<Syncfusion.EJ2.Grids.GridColumn> { 
                ---- 
        } 
       }; 
} 
         
<ejs-grid id="Grid1" dataSource=Model.Datasource2 childGrid="ChildGrid" detailDataBound="detailDataBound" allowPaging="true">             
    ----- 
</ejs-grid> 
</div> 
 
<script> 
    function detailDataBound(args){ 
                  // bind the nested data from the parent grid datasource directly to the childGrid 
        args.childGrid.dataSource = args.data.FamilyMembers; 
    } 
</script> 



We also suggest you to use separate dataSource for childGrid in your data base. Find the below code example for your reference. 
 
 
<div> 
@{  
       var ChildGrid2 = new Syncfusion.EJ2.Grids.Grid() { 
           DataSource = Model.ChildDataSource, 
           QueryString = "EmployeeID",            
           Columns = new List<Syncfusion.EJ2.Grids.GridColumn> { 
                --- 
        } 
       }; 
   } 
         
<ejs-grid id="Grid2" dataSource=Model.Datasource2 childGrid="ChildGrid2" allowPaging="true">             
    ---- 
</ejs-grid> 
</div> 
 

Refer to the below documentation to know about the hierarchy binding. 


Please get back to us if you need further assistance with this. 

Regards, 
Rajapandiyan S 



SS Sejal Sheth December 11, 2021 05:31 AM UTC

Thank you for the example. That helped a lot. 

One last question - Is there a way to set it up so that the child grid will open expanded?


Thank you



RS Rajapandiyan Settu Syncfusion Team December 13, 2021 11:03 AM UTC

Hi Sejal Sheth, 

Thanks for your update. 

By default, the Hierarchy Grid is rendered in the collapsed state. This is the intended behavior of Grid. However, you can render the Hierarchy Grid in expanded state by invoking the expandAll method in the dataBound event of Grid. 



[index.cshtml] 

<ejs-grid id="Grid" dataSource=Model.Datasource childGrid="ChildGrid" dataBound="dataBound" allowPaging="true">             
    --- 
</ejs-grid> 
</div> 
 
<script> 
    function dataBound(args) { 
        var gridIns = document.getElementById("Grid").ej2_instances[0]; // get the Grid instances 
        gridIns.detailRowModule.expandAll(); // expand all the child-Grids 
    } 
</script> 


Please get back to us if you need further assistance. 

Regards, 
Rajapandiyan S 


Loader.
Up arrow icon