[]
        
(Showing Draft Content)

Bind Page/RDLX Report to CSV, JSON, and XML Data

You can use common data formats such as CSV, JSON, XML to bind your Page/RDLX report to data. You can use a provider with the LocateDataSource event to connect to unbound data sources at runtime. The reporting engine calls the LocateDataSource event when it needs data. To do this, you need to add a handler for this event and set the data for the report in it.

When using the LocateDataSource event as a data binding method, the general approach is as follows:

string path = ""; // Your path to the report
PageReport report = new PageReport(new FileInfo(path));
report.Document.LocateDataSource += (sender, args) => { // Data binding  };

The event handler receives an argument of the LocateDataSourceEventArgs type with data, related to this event. The following LocateDataSourceEventArgs properties provide information, specific to this event.

  • Data - The data returned by the event handler.
  • DataSet - Gets the report's GrapeCity.ActiveReports.PageReportModel.IDataSet object to locate data for.
  • Parameters - Gets the Parameters collection specified for a given report instance.
  • Report - Gets the Report that is trying to locate the data set.

CSV Data Source

To connect to the CSV data source, use the following code:

private PageReport LoadReport()
{           
    string path = ""; // Your path to the report
    PageReport report = new PageReport(new FileInfo(path));
    report.Document.LocateDataSource += new LocateDataSourceEventHandler(OnLocateDataSource);
   
    return report;
}
private void OnLocateDataSource(object sender, LocateDataSourceEventArgs args)
{
    string data = ""; // Your csv string
    args.Data = data;
}

type=note

Note: We assume that the report has defined data fields and datasets, which can be done in various ways, e.g., in the Web or Windows Forms Designer.

To interact with the CSV data source, you can specify a connection string that includes the following parameters:

  • Specify the path to the CSV file
  • Specify the encoding
  • Specify the rows separator
  • Specify the columns separator
  • Specify the text qualifier
  • Specify the columns

You can also use the LocateDataSource event to add new data. Data is added as a new data source in the OnLocateDataSource event handler. To control this, you previously subscribed to this event. The reporting engine raises the LocateDataSource event when it needs data.

The LocateDataSource event handler sets the data in the required format as you can see in the sample below.

private void OnLocateDataSource(object sender, LocateDataSourceEventArgs args)
{
    string data = ""; // Your csv string
    args.Data = data;
}

JSON Data Source

To demonstrate how to bind to JSON data, let's pass a string in the target format as a data source. To do this, you need to subscribe to the LocateDataSource event.

private void OnLocateDataSource(object sender, LocateDataSourceEventArgs args)
{
    string data = ""; // Your json  string
    args.Data = data;
}
private PageReport LoadReport()
{
    string path = ""; // Your path to the report
    PageReport report = new PageReport(new FileInfo(path));
    report.Document.LocateDataSource += OnLocateDataSource;
   
    return report;
}

type=note

Note: We assume that the report has defined data fields and datasets, which can be done in various ways, e.g., in the Web or Windows Forms Designer.

Data in the JSON format is assigned when the application calls the LocateDataSource event. Data is added as a new data source in the OnLocateDataSource event handler. To control this, you previously subscribed to this event. The reporting engine raises the LocateDataSource event when it needs data. To do this, we create the OnLocateDataSource event handler.

XML Data Source

The example below shows how to bind a report to the XML data source by subscribing to the LocateDataSource event and assigning a handler to it.

private void OnLocateDataSource(object sender, LocateDataSourceEventArgs args)
{
    XmlTextReader xmlReader = new XmlTextReader(""); // Your path to the xml file
    args.Data = xmlReader;
}
private PageReport LoadReport()
{
    string path = ""; // Your path to the report file
    PageReport report = new PageReport(new FileInfo(path));
    report.Document.LocateDataSource += OnLocateDataSource;
   
    DataSource dataSource = report.Report.DataSources[0]; // Your data source
    if(dataSource.ConnectionProperties.DataProvider == "XML")
    {
        dataSource.ConnectionProperties.ConnectString = "xmldoc="; // Your connection string
    }
   
    return report;
}

In the event handler, an XML file is assigned as a data source by using one of the allowed binding methods. For an XML file, these methods include XmlReader, XmlDocument, IXPathNavigable.

The reporting mechanism calls the LocateDataSource event, which is used to set the data into the report. To do this, you create the OnLocateDataSource event handler and pass in it the data that will be transmitted to the report.

type=note

Note: We assume that the report has defined data fields and datasets, which can be done in various ways, e.g., in the Web or Windows Forms Designer.

See Also

Nested Datasets