xtrareports-402101-feature-guide-to-devexpress-reports-bind-reports-to-data-data-federation-bind-a-report-to-a-transformation-based-data-source-runtime-sample.md
This example demonstrates how to create a federated data source that unfolds another data source’s complex objects and flattens them in a simple data set.
View Example: How to Bind a Report to a Transformation-Based Query
Create a JsonDataSource object that retrieves data from the Products.json file.
Products.json
[
{
"CategoryId": 1,
"CategoryName": "Beverages",
"Description": "Soft drinks, coffees, teas, beers, and ales",
"Products": [
{
"ProductId": 1,
"ProductName": "Chai",
"SupplierId": 1,
"CategoryId": 1,
"QuantityPerUnit": "10 boxes x 20 bags",
"UnitPrice": 18.0000,
"UnitsInStock": 39,
"UnitsOnOrder": 0,
"ReorderLevel": 10,
"Discontinued": false,
"Supplier": null
},
{
"ProductId": 2,
"ProductName": "Chang",
"SupplierId": 1,
"CategoryId": 1,
"QuantityPerUnit": "24 - 12 oz bottles",
"UnitPrice": 19.0000,
"UnitsInStock": 17,
"UnitsOnOrder": 40,
"ReorderLevel": 25,
"Discontinued": false,
"Supplier": null
}
]
},
{
"CategoryId": 2,
"CategoryName": "Condiments",
"Description": "Sweet and savory sauces, relishes, spreads, and seasonings",
"Products": [
{
"ProductId": 3,
"ProductName": "Aniseed Syrup",
"SupplierId": 1,
"CategoryId": 2,
"QuantityPerUnit": "12 - 550 ml bottles",
"UnitPrice": 10.0000,
"UnitsInStock": 13,
"UnitsOnOrder": 70,
"ReorderLevel": 25,
"Discontinued": false,
"Supplier": null
},
{
"ProductId": 4,
"ProductName": "Chef Anton's Cajun Seasoning",
"SupplierId": 2,
"CategoryId": 2,
"QuantityPerUnit": "48 - 6 oz jars",
"UnitPrice": 22.0000,
"UnitsInStock": 53,
"UnitsOnOrder": 0,
"ReorderLevel": 0,
"Discontinued": false,
"Supplier": null
}
]
}
]
using DevExpress.DataAccess.Json;
// ...
// Create a JSON data source.
JsonDataSource jsonDataSource = new JsonDataSource();
Uri fileUri = new Uri("Products.json", UriKind.RelativeOrAbsolute);
jsonDataSource.JsonSource = new UriJsonSource(fileUri);
jsonDataSource.Fill();
Imports DevExpress.DataAccess.Json
' ...
' Create a JSON data source.
Dim jsonDataSource As New JsonDataSource()
Dim fileUri As New Uri("Products.json", UriKind.RelativeOrAbsolute)
jsonDataSource.JsonSource = New UriJsonSource(fileUri)
jsonDataSource.Fill()
The data source includes a nested Products collection.
Create a FederationDataSource object. Use a TransformationNodeBuilder instance to transform the JSON data source into a flat structure.
using DevExpress.DataAccess.DataFederation;
// ...
// Create a Federation data source.
FederationDataSource federationDataSource = new FederationDataSource();
Source jsonSource = new Source("json", jsonDataSource);
TransformationNode query = jsonSource
.Transform()
.TransformColumn("Products")
.Build("Transformation");
federationDataSource.Queries.Add(query);
Imports DevExpress.DataAccess.DataFederation
' ...
' Create a Federation data source.
Dim federationDataSource As New FederationDataSource()
Dim jsonSource As New Source("json", jsonDataSource)
Dim query As TransformationNode = jsonSource.Transform().TransformColumn("Products").Build("Transformation")
federationDataSource.Queries.Add(query)
The data source exposes the flattened structure.
Create a new report and construct a layout similar to the described layout. Use the report’s DataSource and DataMember properties to bind it to a federated data source.
using System.ComponentModel;
using System.Drawing;
using DevExpress.XtraReports.UI;
// ...
// Create a report and bind it to the Federation data source.
XtraReport1 report = new XtraReport1();
report.DataSource = federationDataSource;
report.DataMember = "Transformation";
Imports System.ComponentModel
Imports System.Drawing
Imports DevExpress.XtraReports.UI
' ...
' Create a report and bind it to the Federation data source.
Dim report As New XtraReport1()
report.DataSource = federationDataSource
report.DataMember = "Transformation"
Initialize the End-User Report Designer or Document Viewer and display the report. Refer to the following topics for instructions on how to do this on different platforms:
The following code sample demonstrates how to display the report in the Windows Forms Document Viewer:
// Display the report in the Document Viewer.
documentViewer1.DocumentSource = report;
' Display the report in the Document Viewer.
documentViewer1.DocumentSource = report
See Also
Bind a Report to a Join-Based Federated Data Source
Bind a Report to a Federated Master-Detail Data Source
Bind a Report to a Federated Master-Detail Data Source (Runtime Sample)
Bind a Report to a Union-Based Federated Data Source
Bind a Report to a Union-Based Federated Data Source (Runtime Sample)