18
votes

I'm really new to RDLC so please forgive me if I'm missing something obvious.

I have some data that needs to get repeated, based on a 1:Many dataset that's linked to the main report. It's not something that can be displayed in a tabular fashion because the customers want it laid out in a specific manner, so using the Tablix control is out. I have a number of subreports based on 1:1 datasets in this main report, and everything is cool with those. I created a subreport for this new dataset and added it to the main RDLC report. Everything works fine, but it only includes the first record of the dataset.

Is it possible to repeat this subreport for each record in the dataset? I think the thing that confuses me is the main RDLC has no code that specifically loads the individual subreports, so I don't see anyplace obvious where I could include a loop or anything.

1
There should be SubreportProcessing event which is triggered for every instance of the subreport in the main report. If a report contains multiple subreports instances from the same report definition, this event is triggered for each instance.user2250152
How could I use that information to repeat a subreport n number of times, with n being the number of records returned to the dataset that populates it?Johnny Bones

1 Answers

7
votes

If you don't have particular needs I think it's important to know that you can put a Rectangle in Tablix cell and then use it as a container for simple controls as TextBox, Line, Image, etc. laied out as you want.

If you still need to use subreport you could put a SubReport in Tablix cell and solve any problem in the LocalReport.SubreportProcessing event that occurs when a subreport is processed.

If you have a lot of records you can use a single Dataset and filtered it in the SubreportProcessing event, using the subreport Parameters that you already set in Designer Mode.

Private Sub SubreportProcessingHandler(sender As Object, e As SubreportProcessingEventArgs)

    Dim dvFiltered As New DataView(Me.YourDataSet.Tables(0))

    dvFiltered.RowFilter = "Parameter1 = " & CInt(e.Parameters.Item("yourParameter1").Values(0)) & " AND " _
        & "Parameter2 = '" & CStr(e.Parameters.Item("yourParameter2").Values(0)) & "'"

    e.DataSources.Add(New ReportDataSource("YourDataSourceName", dvFiltered.ToTable("YourDataSourceName")))

End Sub

For example, using a DataSet that contains master and details data, you can build a main report grouped by IdMaster and put a subreport in details section.

main report

This is the subreport: please note that the DataSet is the same as main report but we also need 2 Parameters (IdMaster and IdRow) to display the correct data.

sub report

In the main report you need to link subreport Parameters to actual values of DataSet.

sub report parameter

Then, the most important part: the SubreportProcessingHandler event. This event is triggered for every instance of the subreport in the main report, so if you have 100 rows / 100 subreports this event is triggedered 100 times: every time you need to specify which data you want to display, so you have to filter the DataSet using the 2 Parameters (IdMaster and IdRow) defined in the previous step and filled with values coming from master report.

Private Sub SubreportProcessingHandler(sender As Object, e As SubreportProcessingEventArgs)

    Dim dvTest As New DataView(Me.dsTest.Tables(0))
    dvTest.RowFilter = "IdMaster = " & CInt(e.Parameters.Item("parIdMaster").Values(0)) & " AND " _
        & "IdRow = " & CInt(e.Parameters.Item("parIdRow").Values(0))

    e.DataSources.Add(New ReportDataSource("DataSet_TEST", dvTest.ToTable("DataSet_TEST")))

End Sub

This is the result:

result

As I stated at the beginning of the answer, if you don't have particular needs you can use a Rectangle instead of a SubReport. Regarding this example you can obtain the same result using the green Rectangle as container.

rectangle