Programmatically adding a SubReport

Stimulsoft Reports.NET discussion
Post Reply
Tobias
Posts: 104
Joined: Mon Nov 24, 2008 8:44 am

Programmatically adding a SubReport

Post by Tobias »

I have a report template / *.mrt that defines the content of a label.
Now I would like to programmatically create a master report that prints these labels as a SubReport on a DataBand with 3 columns.

Basically something like this, which causes a NullReferenceException:

Code: Select all

var labelTemplate = new StiReport().Load(mrtFileName);
var masterReport = new StiReport();
var dataBand = new StiDataBand
{
    Name = "MasteDataBand",
    Columns = 3,
    Height = labelTemplate.Pages[0].Height
};
var subReport = new StiSubReport
{
    Name = "LabelData",
    Report = labelTemplate,
    Width = labelTemplate.Pages[0].Width,
    Height = labelTemplate.Pages[0].Height
};
dataBand.Components.Add(subReport);
masterReport.Pages[0].Components.Add(dataBand);
masterReport.RegBusinessObject("LabelData", new[]
{
    new
    {
        ProductId = "Foo"
    },
    new
    {
        ProductId = "Bar"
    },
});
masterReport.Dictionary.SynchronizeBusinessObjects();
masterReport.Design();
Is something like this even possible? And if so, what am I doing wrong?
Tobias
Posts: 104
Joined: Mon Nov 24, 2008 8:44 am

Re: Programmatically adding a SubReport

Post by Tobias »

I kinda got it working now the following way. The "trick" was to take the page from the sub report template, re-assgin it to the master template, change it's name and then add it to the master template. Then this pages GUID needs to be assigned to the sub report. Not sure if this is the "right" way to do it.

Code: Select all

var labelTemplate = new StiReport().Load(mrtFileName);
var masterReport = new StiReport
{
    Unit = labelTemplate.Unit
};
masterReport.RegBusinessObject("LabelData", new[]
{
    new
    {
        ProductId = "Foo"
    },
    new
    {
        ProductId = "Bar"
    },
});
masterReport.Dictionary.SynchronizeBusinessObjects();
var dataBand = new StiDataBand
{
    Name = "MasteDataBand",
    BusinessObjectGuid = masterReport.Dictionary.BusinessObjects["LabelData"].Guid,
    Columns = 3,
    Height = labelTemplate.Pages[0].Height
};
masterReport.Pages[0].Components.Add(dataBand);
var subReportPage = labelTemplate.Pages[0];
subReportPage.Report = masterReport;
subReportPage.Name = "LabelDataPage";
masterReport.Pages.Add(subReportPage);
var subReport = new StiSubReport
{
    Name = "LabelDataSubReport",
    SubReportPageGuid = subReportPage.Guid,
    Width = labelTemplate.Pages[0].Width,
    Height = labelTemplate.Pages[0].Height
};
dataBand.Components.Add(subReport);
masterReport.Design();        
Lech Kulikowski
Posts: 7287
Joined: Tue Mar 20, 2018 5:34 am

Re: Programmatically adding a SubReport

Post by Lech Kulikowski »

Hello,

Thank you for sharing your experience with other users.
Post Reply