none
Subreports in VS 2005 - Newbie Question RRS feed

  • Question

  • Can anyone tell me in simpe terms how to link a report to a subreport.  I have a PK:FK i know i need to pass the PK to the subreport as a parameter then the subreport will no which records to display, but how do I do this?!!!

     

    Please help as this is driving me mad, used to be dead simple in MS Access  :-(

     

    Thanks in advance.

     

    Alex

    Tuesday, February 19, 2008 1:06 PM

Answers

  • Here is a brief C# example, notice the event I create, and the method that handles it

     

    DataSet ReportDataSet = new DataSet();

    protected void Form_Load(object sender, EventArgs e)

    {

    //Fill your dataset with the tables needed for the reports

     

    //load your report

     

    ReportViewer1.LocalReport.DataSources.Clear();

    //attach our returned datatable as a reportdatasource

     

    ReportDataSource customReportData = new ReportDataSource();

     

    customReportData.Name = "ReportDataset_ReportData1";

    customReportData.Value = ReportDataSet.Tables[0];

     

     

     

    ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandle(LocalReport_SubreportProcessing);//ATTENTION:  this is the line you need to add for the event handling

     

     

     

    //other code commented out for brevity

     

    ReportViewer1.LocalReport.DataSources.Add(customReportData);

    ReportViewer1.LocalReport.Refresh();

    }

    //our event handler for sub report processing

    void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)

    {

    e.DataSources.Add(new ReportDataSource("ReportDataset_ReportData2", ReportDataSet.Tables[1]));

    }

     

     

     

    And below is a VB example, but my VB is rusty...so I hope I got it right heh

     

    Private ReportDataSet As New DataSet()

    Protected Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs)

       
        'Fill your dataset with the tables needed for the reports
       
       
       
        'load your report
       
       
       
        ReportViewer1.LocalReport.DataSources.Clear()
       
        'attach our returned datatable as a reportdatasource
       
       
       
        Dim customReportData As New ReportDataSource()
       
       
       
        customReportData.Name = "ReportDataset_ReportData1"
       
        customReportData.Value = ReportDataSet.Tables(0)
       
       
       
       
       
       
       
        ReportViewer1.LocalReport.SubreportProcessing += New SubreportProcessingEventHandle(LocalReport_SubreportProcessing)
        'ATTENTION: this is the line you need to add for the event handling
       
       
       
       
       
       
        'other code commented out for brevity
       
       
       
        ReportViewer1.LocalReport.DataSources.Add(customReportData)
       
        ReportViewer1.LocalReport.Refresh()
       
    End Sub

    'our event handler for sub report processing

    Private Sub LocalReport_SubreportProcessing(ByVal sender As Object, ByVal e As SubreportProcessingEventArgs)

       
        e.DataSources.Add(New ReportDataSource("ReportDataset_ReportData2", ReportDataSet.Tables(1)))
       
    End Sub

    Friday, February 22, 2008 3:21 PM

All replies

  • You can pass values from the main report to the subreport via report parameters.  When you specifying the report path on the subreport report item, you can also specify the parameters to the subreport.  The parameter values can be expressions based on data set values in the main report.

    Thursday, February 21, 2008 2:45 AM
    Moderator
  • Thanks Brian,

     

    Have looked at the properties of the subreport in the main report and can't find a report path property.  Can you advise where I could find this?

     

    Thanks for help

     

    Alex

     

    Thursday, February 21, 2008 10:11 AM
  • report path can be set in a few ways, but the first and most simple way would be to right click on the subreport, choose properties, and then on the general tab, the last box is "subreport", here you can choose the report you want to associated with the subreport.

    -------------------------------------

    Now, half the work in regards to your original question, is setting up the subreport to receive the value you want to filter on.    We will assume you've attached a datatable to your subreport that has a FK from the table on the primary report.

     

    I'll give my example using the concept of a table on your subreport.

     

    First, make sure of course your report designer is open to the subreport, since that's what were working on right now, not the primary.

     

    Next right click in a blank area and choose "Report Parameters".    Set up a parameter that is designed to accept a value from your primary report.....

     

    Now that you've created a parameter, go to the table in your subreport, right click the table, and choose properties.  This should bring you to the table properties.  There should be a filter tab.  Click on the filter tab.  Set the expression to equal

    Fields!FK.value        where FK equals the name of the foreign key to be filtered on in your subreport.

    Set your operator to equals

    and the value   to the parameter you just created a bit ago

    = Parameters!MyFKParam.Value

     

    Now that your subreport is all setup, it's time to link it to the primary report

    Drag the subreport control onto your Primary report, right click the subreport, choose properties...and set the subreport to the one we just got all setup.

    Choose the parameters tab, in your first parameter row, set the parameter name to equal the name of the parameter you created in your subreport, and set the parameter value to equal the key value you want to pass to the subreport

    Fields!PK.Value  where PK equals the name of the field to be passed.

     

     

    Once you've done that, the last thing you should need to do is setup event handling in your page to give the datasource to your subreport when the subreportprocessing event fires.....if you need help doing this, or a better explanation of anything above, just lemme know.

    Thursday, February 21, 2008 2:37 PM
  • Thanks a lot for that, most helpfull.

     

    If you could post VB code for setting up the Event Handling, or explain how its done I'd be most gratefull.

     

    Thanks

     

    Alex

     

    Friday, February 22, 2008 10:30 AM
  • Here is a brief C# example, notice the event I create, and the method that handles it

     

    DataSet ReportDataSet = new DataSet();

    protected void Form_Load(object sender, EventArgs e)

    {

    //Fill your dataset with the tables needed for the reports

     

    //load your report

     

    ReportViewer1.LocalReport.DataSources.Clear();

    //attach our returned datatable as a reportdatasource

     

    ReportDataSource customReportData = new ReportDataSource();

     

    customReportData.Name = "ReportDataset_ReportData1";

    customReportData.Value = ReportDataSet.Tables[0];

     

     

     

    ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandle(LocalReport_SubreportProcessing);//ATTENTION:  this is the line you need to add for the event handling

     

     

     

    //other code commented out for brevity

     

    ReportViewer1.LocalReport.DataSources.Add(customReportData);

    ReportViewer1.LocalReport.Refresh();

    }

    //our event handler for sub report processing

    void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)

    {

    e.DataSources.Add(new ReportDataSource("ReportDataset_ReportData2", ReportDataSet.Tables[1]));

    }

     

     

     

    And below is a VB example, but my VB is rusty...so I hope I got it right heh

     

    Private ReportDataSet As New DataSet()

    Protected Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs)

       
        'Fill your dataset with the tables needed for the reports
       
       
       
        'load your report
       
       
       
        ReportViewer1.LocalReport.DataSources.Clear()
       
        'attach our returned datatable as a reportdatasource
       
       
       
        Dim customReportData As New ReportDataSource()
       
       
       
        customReportData.Name = "ReportDataset_ReportData1"
       
        customReportData.Value = ReportDataSet.Tables(0)
       
       
       
       
       
       
       
        ReportViewer1.LocalReport.SubreportProcessing += New SubreportProcessingEventHandle(LocalReport_SubreportProcessing)
        'ATTENTION: this is the line you need to add for the event handling
       
       
       
       
       
       
        'other code commented out for brevity
       
       
       
        ReportViewer1.LocalReport.DataSources.Add(customReportData)
       
        ReportViewer1.LocalReport.Refresh()
       
    End Sub

    'our event handler for sub report processing

    Private Sub LocalReport_SubreportProcessing(ByVal sender As Object, ByVal e As SubreportProcessingEventArgs)

       
        e.DataSources.Add(New ReportDataSource("ReportDataset_ReportData2", ReportDataSet.Tables(1)))
       
    End Sub

    Friday, February 22, 2008 3:21 PM