ActiveReports allows you to add pages to your report in Visual Studio for previewing in the viewer control or printing. The document containing the inserted pages can also be saved to an RDF file or exported.
This walkthrough illustrates how to create two reports and insert the second report as a cover page for the first one.
This walkthrough is split up into the following activities:
- Adding two ActiveReports to a Visual Studio project
- Connecting the report to a data source
- Adding controls to both reports
- Adding code to the detail format event of rptInsertPage to alternate colors
- Adding code to the Windows Form Load event to set the viewer document equal to the report document and to insert the second report as a cover page
- Viewing the report
- Adding code to insert a page into a specific report location using the AddNew() method
To complete the walkthrough, you must have access to the Northwind database.
A copy is located at C:\Program Files\Data Dynamics\ActiveReports for .NET 3.0\Data\NWIND.MDB.
When you have completed this walkthrough, you will have a report that looks similar to the following. Please note that this image reflects a "1 X 2 Pages" view.
Adding two ActiveReports to a Visual Studio project
To add two ActiveReports to a Visual Studio project
- Open a new project in Visual Studio.
- From the Project menu, select Add New Item.
- Select ActiveReports 3.0 File and rename the file rptInsertPage.
- Click Open.
- From the Project menu, select Add New Item.
- Select ActiveReports 3.0 File and rename the file rptCoverPage.
- Click Open.
Connecting the report to a data source
To connect rptInsertPage to a data source
- Click on the gray report DataSource icon in the Detail section to open the report DataSource dialog.
- Select the "OLE DB" tab.
- Click the Build button.
- Select Microsoft Jet 4.0 OLE DB Provider and click Next.
- Click the ellipsis button to browse for the access path to the Northwind database. Click Open once you have selected the appropriate access path.
- Click OK to continue.
- In the Query field, type "Select * from products order by productname".
- Click OK to return to the report design surface.
Adding controls to display the data
To add controls to rptInsertPage
- Make the following changes to the PageHeader section:
- Change the BackColor property to LightSteelBlue
- Add labels with the following properties to the page header:
Font Style Name Text Location Bold lblProductID Product ID 0, 0 Bold lblProductName Product Name 1.3, 0 Bold lblUnitsInStock Units In Stock 2.65, 0 Bold lblUnitsOnOrder Units On Order 3.9, 0 Bold lblUnitPrice Unit Price 5.25, 0 - In the Report Explorer, expand the Fields node, then the Bound node. Drag the following fields onto the detail section and set the following properties of each textbox as indicated.
Field Text Miscellaneous Location ProductID Product ID 0, 0 ProductName Product Name 1.3, 0 UnitsInStock Units in Stock 2.65, 0 UnitsOnOrder Units on Order 3.9, 0 UnitPrice Unit Price OutputFormat = Currency 5.25, 0
To add controls to rptCoverPage
- Make the following changes to the PageHeader section:
- Change the Height property to 0.8
- Change the BackColor property to LightSlateGray
- Add the following controls to the page header:
Control Name Text Miscellaneous Location Label lblCompanyInfo Company Street Name Company City, Company State Company Zip Code Alignment = Center
Size = 2, 0.56
0.2, 0.13 Label lblCompanyName Company Name Font Size = 16, Style = Bold 3.5, 0.25 Line Line1 X1 = 2.3
X2 = 6.4
Y1 = 0.06
Y2 = 0.06
Line Line2 X1 = 2.3
X2 = 6.4
Y1 = 0.72
Y2 = 0.72
Line Line3 X1 = 2.3
X2 = 2.3
Y1 = 0.06
Y1 = 0.72
Line Line4 X1 = 6.4
X2 = 6.4
Y1 = 0.06
Y2 = 0.72
- Set the Height property of the Detail section to 2.8.
- Add the following controls to the detail section:
Control Name Text Miscellaneous Location Label lblFax Fax Font Size = 24, Style = Bold 0.25, 0 Label lblTo To: Font Size = 12, Style = Bold 1, 0.75 Label lblFax2 Fax #: Font Size = 12, Style = Bold 1, 1 Label lblPhone Phone #: Font Size = 12, Style = Bold 1, 1.25 Label lblRe Re: Font Size = 12, Style = Bold 1, 1.5 Label lblCC CC: Font Size = 12, Style = Bold 3, 1.5 Label lblDate Date: Font Size = 12, Style = Bold 3, 1.25 Label lblPages Pages: Font Size = 12, Style = Bold 3, 1 Label lblFrom From Font Size = 12, Style = Bold 3, 0.75 CheckBox chkUrgent Urgent 0.5, 2 CheckBox chkForReview For Review 1.5, 2 CheckBox chkPleaseComment Please Comment 2.75, 2 CheckBox chkPleaseReply Please Reply 4.5, 2 Line Line5 X1 = 0.06 X2 = 6.44
Y1 = 2.38
Y2 = 2.38
Label lblComments Comments: 1, 2.5
Adding code to the detail format event of rptInsertPage to alternate colors
To write the code in Visual Basic or C#
- Double-click the detail section of rptInsertPage to create an event-handling method for the detail format event. Add code to the handler to:
- Alternate background colors
The following example shows what the code for the method looks like.
'Visual Basic Private color As Boolean Private Sub Detail1_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ Detail1.Format If color Then color = False Detail1.BackColor = System.Drawing.Color.Gainsboro Else Detail1.BackColor = System.Drawing.Color.Transparent color = True End If End Sub //C# bool color; private void detail_Format(object sender, System.EventArgs eArgs) { if(color) { color =false; this.detail.BackColor = System.Drawing.Color.Gainsboro; } else { this.detail.BackColor = System.Drawing.Color.Transparent; color = true; } }
Adding code to the Windows Form Load event
To write the code in Visual Basic or C#
- Add the ActiveReports viewer control to the Windows Form. Then, double-click the top of the Windows Form to create an event-handling method for the form's Load event. Add code to the handler to:
- Set the viewer document equal to the rptInsertPage report document
- Insert rptCoverPage at the beginning of rptInsertPage
The following example shows what the code for the Insert method looks like.
'Visual Basic Private Sub myForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Dim rpt As New rptInsertPage() Viewer1.Document = rpt.Document rpt.Run() Dim rpt2 As New rptCoverPage() rpt2.Run() rpt.Document.Pages.Insert(1, rpt2.Document.Pages(0)) End Sub //C# private void myForm_Load(object sender, System.EventArgs e) { rptInsertPage rpt = new rptInsertPage(); viewer1.Document = rpt.Document; rpt.Run(); rptCoverPage rpt2 = new rptCoverPage(); rpt2.Run(); rpt.Document.Pages.Insert(1, rpt2.Document.Pages[0]); }
Viewing the report
To view the report
- Press F5 to run the project and view the report.
You can quickly view your report at design time by clicking the Preview tab at the bottom of the designer. |
Adding code to insert a page into a specified report location using the AddNew() method
To write the code in Visual Basic or C#
- Double-click the Windows Form to create an event-handling method for the form's Load event. Add code to the handler to:
- Insert a page into a specific report location using the InsertNew() method
The following example shows what the code for the InsertNew() method looks like.
'Visual Basic Private Sub myForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Dim rpt As New rptInsertPage() rpt.Run() rpt.Document.Pages.InsertNew(3) Viewer1.Document = rpt.Document End Sub //C# private void myForm_Load(object sender, System.EventArgs e) { rptInsertPage rpt = new rptInsertPage(); rpt.Run(); rpt.Document.Pages.InsertNew(3); viewer1.Document = rpt.Document; }