ActiveReports 18 .NET Edition
Developers / Working with Reports / Page/RDLX Report / Bind a Page/RDLX Report to Data / Bind Page/RDLX Report to CSV, JSON and XML Data / Nested Datasets
In This Topic
    Nested Datasets
    In This Topic

    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.
    Report with Nested Dataset at Preview

    1. In Visual Studio 2022, create a new Windows Forms project.
    2. Right-click the project in the Solution Explorer and select Manage NuGet Packages for Solution.
    3. On the Browse tab of the opened NuGet - Solution window, search and install the MESCIUS.ActiveReports and MESCIUS.ActiveReports.Design.Win NuGet packages. 
    4. On the Form.cs, double-click the title bar to create the Form1_Load event.
    5. At the top of the Form1 code view, add these using directives.
      Paste at the top of the Form1 code view
      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;
      
    6. In the existing code, add the following code.
      Example Title
      Copy Code
      namespace NestedDataSetSample
      {
          public partial class Form1 : Form
          {
              Designer designer;
              PropertyGrid propertyGrid;
              ReportExplorer reportExplorer;
              PageReport report;
              ReportSection section;
              public Form1()
              {
                  InitializeComponent();
              }
      
    7. Add the following code to the Form1_Load event.
      C# code. Paste INSIDE the Form1_Load event.
      Copy Code
      {
          CreateLayout();
          CreateReport();
          CreateDataRegions();
          CreateDataSource();
          designer.LoadReport(XmlReader.Create(new StringReader(report.ToRdlString())), DesignerReportType.Page);
      }
      
    8.  In the existing code, add the following code to create the Designer, PropertyGrid and ReportExplorer objects.
      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);
      }
      
    9. Add the following code to create the PageReport and ReportSection objects.
      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);
      }
      
    10. Add the following code to create data regions that will contain report data.
      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);
              }
      
    11. Add the following code to create a JSON data source, add a dataset and a nested dataset, and add fields to both datasets.
      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);
              }
      
    12. Add the following code to load the report to the Designer in Form1_Load event.
      C# code. Paste INSIDE the Form1_Load event.
      Copy Code
      designer.LoadReport(XmlReader.Create(new StringReader(report.ToRdlString())), DesignerReportType.Page);
      
    13. Run the project and preview the report.
    See Also