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 JSON With Nested Data.json file.
Paste at the top of the Form1.cs |
Copy Code
|
---|---|
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; |
Add to Form1.cs |
Copy Code
|
---|---|
namespace NestedDataSetSample { public partial class Form1 : Form { Designer designer; PropertyGrid propertyGrid; ReportExplorer reportExplorer; PageReport report; ReportSection section; public Form1() { InitializeComponent(); } |
C# code. Paste INSIDE the Form1_Load event. |
Copy Code
|
---|---|
{
CreateLayout();
CreateReport();
CreateDataRegions();
CreateDataSource();
designer.LoadReport(XmlReader.Create(new StringReader(report.ToRdlString())), DesignerReportType.Page);
}
|
C# code. Paste AFTER the Form1_Load event. |
Copy Code
|
---|---|
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); } |
C# code. Paste AFTER CreateLayout() |
Copy Code
|
---|---|
private void CreateReport() { report = new PageReport(); section = new ReportSection(); section.Name = "Section1"; section.Width = "15cm"; section.Body.Height = "20cm"; report.Report.ReportSections.Add(section); } |
C# code. Paste AFTER CreateReport() |
Copy Code
|
---|---|
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); } |
C# code. Paste AFTER CreateDataRegions() |
Copy Code
|
---|---|
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); } |
C# code. Paste INSIDE the Form1_Load event. |
Copy Code
|
---|---|
designer.LoadReport(XmlReader.Create(new StringReader(report.ToRdlString())), DesignerReportType.Page);
|