Page 1 of 1

Programmatically adding a SubReport

Posted: Tue Oct 29, 2024 10:46 pm
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?

Re: Programmatically adding a SubReport

Posted: Wed Oct 30, 2024 7:14 am
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();        

Re: Programmatically adding a SubReport

Posted: Wed Oct 30, 2024 8:57 am
by Lech Kulikowski
Hello,

Thank you for sharing your experience with other users.