[]
Using JSON data requires some setup that is different from other types of data. This walkthrough illustrates how to set up a subreport bound to the JSON data source in the parent report.
When you complete this walkthrough you get a layout that looks similar to the following at design time and at run time.
On the detail section band, click the Data Source Icon.
In the Report Data Source dialog, select the JSON tab.
Click the Build button next to the Connection String section to open the Configure JSON Data Source dialog box.
Specify the following JSON file URL in the Data Path field of the dialog box.
https://demodata.mescius.io/northwind/odata/v1/Categories?$expand=Products
The Connection String section displays the generated connection string as shown below.
jsondoc=https://demodata.mescius.io/northwind/odata/v1/Categories?$expand=Products
Click OK to proceed further.
In the JSON Path field, enter the below query to fetch the required data from the JSON path defined above.
$.value[*]
Click OK to save the data source and return to the report design surface.
On the design surface, select the detail section and in the Properties panel, set the BackColor to 'White Smoke'
type=info
Tip: Even if you do not want colors in your finished reports, using background colors on subreports can help in troubleshooting layout issues.
On the design surface, right-click the pageHeader or pageFooter section and select Delete. Subreports do not render these sections, so deleting them saves processing time.
From the toolbox, drag the following controls to the detail section and in the Properties panel, set the properties as follows.
Property Name | Property Value |
---|---|
TextBox1 | |
DataField | ProductName |
Name | txtProductName |
TextBox2 | |
DataField | UnitPrice |
Name | txtUnitPrice |
OutputFormat | $#,##0.00 (or select Currency in the dialog) |
type=warning
Warning: Do not create a new instance of the subreport in the Format event. Doing so creates a new subreport each time the section Format code is run, which uses a lot of memory.
VB Code:
The following example shows what the code for the method looks like.
Dim rpt As rptSub
rpt = New rptSub()
C# Code:
The following example shows what the code for the method looks like.
private rptSub rpt;
rpt = new rptSub();
To add code to pass a subset of the parent report's data to the subreport
The following example shows what the code for the method looks like.
Dim jsonDS As New GrapeCity.ActiveReports.Data.JsonDataSource
jsonDS.JsonData = (CType(Me.DataSource, GrapeCity.ActiveReports.Data.JsonDataSource)).Field("Products").ToString()
jsonDS.JsonPath = "$.[*]"
rpt.DataSource = jsonDS
SubReport1.Report = rpt
GrapeCity.ActiveReports.Data.JsonDataSource jsonDS = new GrapeCity.ActiveReports.Data.JsonDataSource();
jsonDS.JsonData = ((GrapeCity.ActiveReports.Data.JsonDataSource)this.DataSource).Field("Products").ToString();
jsonDS.JsonPath = "$.[*]";
rpt.DataSource = jsonDS;
subReport1.Report = rpt;
Click the preview tab to view the report at design time.