ActiveReports allows you to save and load a report as a memory stream. Saving a report as a memory stream makes it possible to save and load reports from a database or pass reports back and forth between DLLs.
This walkthrough is split up into the following activities:
- Adding an ActiveReport to a Visual Studio project
- Connecting the report to a data source
- Adding controls to the report to contain data
- Adding the viewer to the Windows Form
- Adding code to the Form_Load event to save the report to a memory stream and load it to the viewer
- Viewing the report
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.
Adding an ActiveReport to a Visual Studio project
To add an ActiveReport to your 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 rptMemoryStream.
- Click Open (Add in Visual Studio 2005).
Connecting the report to a data source
To connect the report to a data source
- Click on the gray report DataSource icon in the Detail section to open the report DataSource dialog.
- Click Build.
- 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 customers ORDER BY country".
- Click OK to return to the report design surface.
Adding controls to the report to contain data
To add controls to the report
- Add a GroupHeader/Footer section to rptMemoryStream by right-clicking on the design surface of the report and selecting Insert > Group Header/Footer.
- Make the following changes to the group header:
- Change the name to ghCustomers
- Change the BackColor property to RosyBrown
- Change the DataField property to Country
- In the Report Explorer, expand the Fields node, then the Bound node. Drag the following fields onto rptMemoryStream and set the properties of each textbox as indicated.
Field Section Location Text Country GroupHeader 0, 0 Country CustomerID Detail 0, 0 Customer ID CompanyName Detail 1.125, 0 Company Name Address Detail 3.01, 0 Address City Detail 4.375, 0 City Phone Detail 5.437, 0 Phone - Set the CanShrink property of the Detail Section to True.
Adding the viewer to the Windows Form
To add the viewer to the Windows Form
- Click on the ActiveReports viewer control in the appropriate toolbox and drag it onto the Windows Form.
- Set the viewer control's Dock property to Fill.
Adding code to the Form_Load event
To write the code in Visual Basic or C#
- Double-click the title bar of the Windows Form to create an event-handling method for the Form_Load event. Add code to the handler to:
- Save the report to a memory stream
- Load the memory stream to the ActiveReports viewer
The following example shows what the code for the method looks like.
'Visual Basic Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Dim strm As New System.IO.MemoryStream() Dim rpt As New rptMemoryStream() rpt.Run() rpt.Document.Save(strm) Dim theBytes(strm.Length) As Byte strm.Read(theBytes, 0, Int(strm.Length)) strm.Position = 0 Viewer1.Document.Load(strm) End Sub //C# private void Form1_Load(object sender, System.EventArgs e) { System.IO.MemoryStream strm = new System.IO.MemoryStream(); rptMemoryStream rpt = new rptMemoryStream(); rpt.Run(); rpt.Document.Save(strm); byte[] theBytes = new byte[strm.Length]; strm.Read(theBytes, 0, (int)strm.Length); strm.Position =0; viewer1.Document.Load(strm); }
Viewing the report
To view the report
- Press F5 to run the project.
You can quickly view your report at design time by clicking the Preview tab at the bottom of the designer. |