0
Reply

How can I add a subreport to a working RDLC main report .

Abdullah Arshad

Abdullah Arshad

Jan 30 2014 11:41 AM
7k
 Hello there. I have been racking my brain and pulling what hair I hve left for the past couple of days and am hoping someone can assist me.
I want to apply a subreport to my RDLC main report. The main report populates fine using a programmatically created dataset created from within the form containing the reportviewer.
That works a treat. What I now want to do is to create a subreport and add it to a table column in the main report.
There are 2 datasets, one called Phases ( used in the Main report with primary keys quoteNo,quoteSuffix, quoteType) and the other called PhaseLines (used in the SubReport with primary keys quoteNo,quoteSuffix, quoteType & phaseNo ).
I simply want the subreport to show all the lines within each phase shown on the Main report.
So I created a subreport with 4 parameters ( quoteNo,quoteSuffix, quoteType & phaseNo ) and a dataset for PhaseLines.
I fully understand that I need to create a SubreportProcessingEventHandler which will add the datasource.
But I don't know how to utilise the subreport's parameters in order to populate the subreport's PhaseLines dataset as I won't know what phaseNo I'm currently at.
I want to populate the subreport from within the code. Can anyone kindly assist me and point me in the right direction.
 
Here is a snippet of my code from within the form containing the reportviewer :
  
private void Form1_Load(object sender, EventArgs e)
        {
            LoadMainReport(quoteNo, quoteSuffix, quoteType);
            mainReportViewer.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(SetSubDataSource);
            this.mainReportViewer.RefreshReport();
        }
 

private void LoadMainReport(string quoteNo, string quoteSuffix, string quoteType)
        {
            mainReportViewer.Visible = true;
            mainReportViewer.LocalReport.ReportPath = @"c:\users\abdullah.arshad\Documents\CharconDev\VISUAL STUDIO PROJECTS\TestSubReportApp\TestSubReportApp\Reports\MainReport.rdlc";
 
            DataTable phasesDT = csData.GetPhaseNumbersTable(quoteNo, quoteSuffix, quoteType);
            DataSet phasesDS = new DataSet("phases");
            phasesDS.Tables.Add(phasesDT);
            ReportDataSource rds = new ReportDataSource("PhasesDataSet", phasesDS.Tables[0]);
            mainReportViewer.LocalReport.DataSources.Clear();
            mainReportViewer.LocalReport.DataSources.Add(rds);
            this.mainReportViewer.RefreshReport();
        }
 
private void SetSubDataSource(object sender, SubreportProcessingEventArgs e)
        {
            string phaseNo = (e.Parameters["phaseNo"].Values.First()).ToString();
            DataTable phaseLinesDT = csData.GetPhaseCostLinesDT(quoteNo, quoteSuffix, quoteType, phaseNo);
            e.DataSources.Add(new ReportDataSource("PhaseLinesDataSet", phaseLinesDT)); ;
        }