Search code examples
c#visual-studiodatatablepaginationreportviewer

How to make ReportViewer display multiple pages (e.g. one page per DataRow)?


I'm new to MS reporting. What I'm trying to achieve is to make a simple report which would be filled from a DataTable (made programmatically). The idea is to assing data for each page to values from DataRow. I.e. report page 1 would get its TextBox values from DataTable.Rows[0], report page 2 would display values from DataTable.Rows[1] etc. Number of pages = number of DataRows.

I've coded a WinForms app that gets a DataTable from SQL and filters it based on parameters supplied by user and then displays a Report.

Then I created an empty dummy DataSet called ComplianceFormDataSet which contains all the field names I will need further. Then I made an .rdlc, added a TextBox to it with this expression:

=Fields!CustomerCode.Value

Here's the logic behind passing the data to ReportViewer:

DataTable MainDataTable = new DataTable();
MainDataTable.Columns.Add("CustomerCode", typeof(string));
MainDataTable.Rows.Add("Blah1");
MainDataTable.Rows.Add("Blah2");
ReportDataSource MainDataSource = new ReportDataSource("ComplianceFormDataSet", MainDataTable);
MainReportViewer.LocalReport.DataSources.Clear();
MainReportViewer.LocalReport.DataSources.Add(MainDataSource);
MainReportViewer.RefreshReport();

When I build this a get a report with one page saying "Blah1". How do I make it render two pages: one with "Blah1" and the second with "Blah2"? Thank you.


Solution

  • Ok, I figured this out:

    1. Add a List element to the report and stretch it to fill report body.
    2. Link the Tablix of this List element to my DataSet.
    3. Add a TextBox element inside List element.
    4. Select the expression for TextBox element: =Fields!CustomerCode.Value
    5. Right-click outside report body and select View -> Grouping.
    6. Add a Row Group.
    7. In Row Group properties General -> add grouping on CustomerCode field.
    8. In Row Group properties Page Breaks -> select Page break options -> Between each instance of a group.
    9. If report always generates an extra page with empty fields - open the .rdlc file in XML viewer, find page break tag and remove it.

    Pretty obvious, isn't it?