XSD Dataset as Datasource for a Page Report
We have already discussed about using XSD (Typed Dataset) as a datasource for ActiveReports' Section Reports in this article. However, we didn't discuss about how to use it in case of Page Reports. The tricks of the trade remains almost same with some minor changes to make this work.
In case you are not aware of how to create a Typed Dataset then you can go through a quick recap here. Just remember to fill the DataSet so that it does not return empty rows.
Now in order to connect this XSD to the report we will need to use the DataSet provider with the LocateDataSource event. To use the Dataset provider as a report's data source we will need to set up a report definition at runtime, and attach the page document to a LocateDataSourceEventHandler.
Here are the steps that are needed to connect a Page Report to an XSD dataset.
- Create a PageReport.
- In the Report Explorer, go to the node 'DataSources' and right-click to select Add Data Source.
- In the Report Data Source dialog that appears, set Type to DataSetProvider and close the dialog. A data source node appears in the ReportExplorer.
- Right-click the data source node and to select Add Data Set.
- In the DataSet dialog that appears select the Fields page.
- On the Fields page, add fields like =Fields!ProductID.Value and =Fields!InStock.Value.
- Click OK to close the dialog. Nodes with the field names appear under the dataset name.
- From the Visual Studio toolbox ActiveReports 7 Page Report tab, drag a Table data region onto the design surface of the report.
- From the ReportExplorer, add the newly added fields onto cells in the detail row of the Table and save the report
- In the code behind ,create a LocateDataSourceEvent handler for the PageDocument , and then set the Data Property of the LocateDataSourceEventArgs to the Typed Dataset.
- Load the Page Document into the Viewer and you should be able to see the PageReport which is bound to the Typed dataset.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using GrapeCity.ActiveReports;
using GrapeCity.ActiveReports.Document;
using GrapeCity.ActiveReports.Configuration;
namespace WindowsFormsApplication24
{
public partial class Viewer : Form
{
PageDocument runtime;
Northwind.CustomersDataTable table = new Northwind.CustomersDataTable();
NorthwindTableAdapters.CustomersTableAdapter da = new NorthwindTableAdapters.CustomersTableAdapter();
private void LoadReport()
{
FileInfo rptPath = new FileInfo(@"..\\..\\PageReport2.rdlx");
PageReport definition = new PageReport(rptPath);
definition.ConfigurationProvider = new GrapeCity.ActiveReports.Configuration.DefaultConfigurationProvider();
runtime = new PageDocument(definition);
runtime.LocateDataSource += new LocateDataSourceEventHandler(runtime_LocateDataSource);
\\\viewer1.LoadDocument(runtime);
}
void runtime_LocateDataSource(object sender, LocateDataSourceEventArgs args)
{
da.Fill(table);
args.Data = table;
}
public Viewer()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadReport();
}
}
Download the attached samples for complete implementation. Download VB Sample Download C# Sample