[]
A nested dataset represents JSON or XML data as an hierarchical structure. A nested JSON or XML dataset is most commonly used in a bound data region, where you can partially use the dataset nodes for different data regions of your report.
Follow these steps to create a report with a JSON nested dataset that contains data on Movies and the movie information on Roles, created from attached file:
In Visual Studio 2022, create a new Windows Forms project.
Right-click the project in the Solution Explorer and select Manage NuGet Packages for Solution.
On the Browse tab of the opened NuGet - Solution window, search and install the MESCIUS.ActiveReports and MESCIUS.ActiveReports.Design.Win NuGet packages.
On the Form.cs, double-click the title bar to create the Form1_Load event.
At the top of the Form1 code view, add these using directives.
using GrapeCity.ActiveReports.Design.ReportExplorer;
using GrapeCity.ActiveReports.Design;
using GrapeCity.ActiveReports.PageReportModel;
using GrapeCity.ActiveReports;
using System.Xml;
using TextBox = GrapeCity.ActiveReports.PageReportModel.TextBox;
using DataSet = GrapeCity.ActiveReports.PageReportModel.DataSet;
using DataSource = GrapeCity.ActiveReports.PageReportModel.DataSource;
using Field = GrapeCity.ActiveReports.PageReportModel.Field;
In the existing code, add the following code.
namespace NestedDataSetSample
{
public partial class Form1 : Form
{
Designer designer;
PropertyGrid propertyGrid;
ReportExplorer reportExplorer;
PageReport report;
ReportSection section;
public Form1()
{
InitializeComponent();
}
Add the following code to the Form1_Load event.
{
CreateLayout();
CreateReport();
CreateDataRegions();
CreateDataSource();
designer.LoadReport(XmlReader.Create(new StringReader(report.ToRdlString())), DesignerReportType.Page);
}
In the existing code, add the following code to create the Designer, PropertyGrid and ReportExplorer objects.
private void CreateLayout()
{
designer = new Designer() { Dock = DockStyle.Fill };
propertyGrid = new PropertyGrid() { Dock = DockStyle.Right };
designer.PropertyGrid = propertyGrid;
reportExplorer = new ReportExplorer()
{
Dock = DockStyle.Left,
ReportDesigner = designer
};
Controls.Add(designer);
Controls.Add(propertyGrid);
Controls.Add(reportExplorer);
}
Add the following code to create the PageReport and ReportSection objects.
private void CreateReport()
{
report = new PageReport();
section = new ReportSection();
section.Name = "Section1";
section.Width = "15cm";
section.Body.Height = "20cm";
report.Report.ReportSections.Add(section);
}
Add the following code to create data regions that will contain report data.
```csharp
private void CreateDataRegions()
{
List listMovies = new List()
{
Name = "List_Movies",
Top = "0.8cm",
Left = "0.6cm",
Width = "11cm",
Height = "5cm",
Style = new Style() { FontFamily = "Arial" },
DataSetName = "Movies",
ZIndex = 1
};
listMovies.ReportItems.Add(new TextBox()
{
Name = "TextBox_Title",
Top = "0.6cm",
Left = "1cm",
Width = "2.5cm",
Height = "0.75cm",
Style = new Style()
{
FontFamily = "Arial",
PaddingBottom = "2pt",
PaddingLeft = "2pt",
PaddingRight = "2pt",
PaddingTop = "2pt"
},
Value = "=Fields!Title.Value",
DataElementName = "Title",
ZIndex = 1
});
List listCast = new List()
{
Name = "List_Cast",
Top = "1.8cm",
Left = "1.4cm",
Width = "5cm",
Height = "2.2cm",
Style = new Style() { FontFamily = "Arial" },
ZIndex = 2,
DataSetName = "Cast"
};
listCast.ReportItems.Add(new TextBox()
{
Name = "TextBox_Name",
Top = "0.6cm",
Left = "0.6cm",
Width = "2.5cm",
Height = "0.75cm",
Style = new Style()
{
FontFamily = "Arial",
PaddingBottom = "2pt",
PaddingLeft = "2pt",
PaddingRight = "2pt",
PaddingTop = "2pt"
},
DataElementName = "Name",
Value = "=Fields!Name.Value",
});
listMovies.ReportItems.Add(listCast);
section.Body.ReportItems.Add(listMovies);
}
11. Add the following code to create a JSON data source, add a dataset and a nested dataset, and add fields to both datasets.
```auto
```csharp
private void CreateDataSource()
{
DataSource dataSource = new DataSource();
dataSource.Name = "JSONDataSource";
dataSource.ConnectionProperties = new ConnectionProperties()
{
DataProvider = "JSON",
ConnectString = "jsondoc={path to file}\\JSON With Nested Data.json"
};
report.Report.DataSources.Add(dataSource);
report.Report.DataSets.Add(CreateDataSet(dataSource));
report.Report.DataSets.Add(CreateSecondDataSet());
}
private DataSet CreateDataSet(DataSource _DataSource)
{
Query query = new Query
{
Timeout = 30,
CommandText = "$.Movie[*]",
DataSourceName = _DataSource.Name,
};
DataSet dataSet = new DataSet
{
Query = query,
Name = "Movies",
};
CreateFieldsToDataSet(dataSet);
return dataSet;
}
private DataSet CreateSecondDataSet()
{
Query query = new Query
{
Timeout = 30,
CommandText = "$",
DataSourceName = "$dataset:Movies/Cast",
};
DataSet dataSet = new DataSet
{
Query = query,
Name = "Cast",
};
CreateFieldsToSecondDataSet(dataSet);
return dataSet;
}
private void CreateFieldsToDataSet(DataSet dataSet)
{
Field field = new Field
{
DataField = "Title",
Name = "Title"
};
dataSet.Fields.Add(field);
}
private void CreateFieldsToSecondDataSet(DataSet dataSet)
{
Field field = new Field
{
DataField = "Name",
Name = "Name"
};
dataSet.Fields.Add(field);
}
12. Add the following code to load the report to the Designer in Form1\_Load event.
```auto
```csharp
designer.LoadReport(XmlReader.Create(new StringReader(report.ToRdlString())), DesignerReportType.Page);
13. Run the project and preview the report.
## See Also
[Quick Start](gcdocsite__documentlink?toc-item-id=a589d793-861b-4106-96d8-d2f05c417002)
#### Report Authors
[Nested Datasets (JSON and XML)](gcdocsite__documentlink?toc-item-id=e02a6082-b971-499e-8da6-4a3dc47ad8cd)