This example shows how to connect to data from code. First, load scripts:
@using Stimulsoft.Base
@using Stimulsoft.Report
@using Stimulsoft.Report.Blazor
Next, add buttons:
<div align="center">
<button class="alert" @onclick="SetFirstDataSet">First dataset</button>
<button class="alert" @onclick="SetSecondDataSet">Second dataset</button>
</div>
<br />
Then, add report viewer:
<StiBlazorViewer Report="@Report" />
After that, create empty report object and load report template:
@code
{
private StiReport Report;
protected override void OnInitialized()
{
base.OnInitialized();
//Create empty report object
var report = new StiReport();
//Load report template
report.Load("Reports/Report.mrt");
}
...
Next, set first dataset:
...
private void SetFirstDataSet()
{
//Get a copy of the report into a new report object
var report = this.Report.Clone() as StiReport;
//Delete connections in the report template
report.Dictionary.Databases.Clear();
//Load new data from XML file
var data = new System.Data.DataSet();
data.ReadXml("Data/Demo1.xml");
//Register new data in the report template
report.RegData(data);
//Synchronize the dictionary with registered data
//Not necessary if the structure of the loaded data is the same as in the template
report.Dictionary.Synchronize();
//Re-render report
report.Render();
//Assign new report to the Viewer
this.Report = report;
}
...
Then, set second dataset:
...
private void SetSecondDataSet()
{
//Get a copy of the report into a new report object
var report = this.Report.Clone() as StiReport;
//Delete connections in the report template
report.Dictionary.Databases.Clear();
//Load new data from XML file
var data = new System.Data.DataSet();
data.ReadXml("Data/Demo2.xml");
//Register new data in the report
report.RegData(data);
//Synchronize the dictionary with registered data
//Not necessary if the structure of the loaded data is the same as in the template
report.Dictionary.Synchronize();
//Re-render report
report.Render();
//Assign new report to the Viewer
this.Report = report;
}
}
In the screenshot below you can see the result of the sample code: