This walkthrough illustrates how to create a simple report using calculated fields.
The 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
- Viewing the run-time 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 finished 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 rptCalFields.
- Click Open.
Connecting the report to a database
To connect the report to a database
- Click on the gray report DataSource icon in the Detail section to open the report DataSource dialog.
- Select the "OLE DB" tab.
- Click on 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 Customers.CompanyName, Products.ProductName, [Order Details].UnitPrice, [Order Details].Quantity FROM Products INNER JOIN ((Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID) INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID) ON Products.ProductID = [Order Details].ProductID WHERE (((DatePart("yyyy", [OrderDate])) = 1996)) ORDER BY Customers.CompanyName, Products.ProductName".
- Click OK to return to the report design surface.
Adding controls to contain data
To add controls to the report
- Add a GroupHeader/Footer section to rptCalFields by right-clicking on the design surface of the report and selecting Insert > Report Header/Footer.
- Make the following changes to the group header:
- Change the Name property to ghProducts
- Change the DataField property to CompanyName
- Change the BackColor property to Gainsboro
- Change the Height property to 5.5 to accommodate the labels
- 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 Section OutputFormat Text Location CompanyName ghProducts Company Name 0, 0 ProductName Detail Product Name 0, 0 Quantity Detail Quantity 2.2, 0 UnitPrice Detail Currency Unit Price 3.5, 0 - Set the CanShrink property of the Detail section to True.
- Add the following controls from the ActiveReports 3.0 toolbox to ghProducts and make the property changes as indicated.
Control Name Location Text Label lblProductName 0, 0.3 Product Name Label lblQuantity 2.2, 0.3 Quantity Label lblUnitPrice 3.5, 0.3 Unit Price Label lblTotalPrice 5, 0.3 Total Price - In the Report Explorer, right-click on the Calculated node and select "Add" from the shortcut menu. This creates an unbound field named "field1" that can be used to perform custom calculations.
- Make the following changes to field1 in the Properties window:
- Change the Name property to TotalPrice
- Change the Formula property to Quantity * UnitPrice to bind it to the product of the Quantity and UnitPrice fields
- Drag TotalPrice from the Calculated node onto the detail section of the report. This creates an ActiveReports TextBox object, txtTotalPrice1, and sets its DataField property equal to TotalPrice.
- Make the following changes to txtTotalPrice1:
- Change the Location property to "5, 0"
- Change the OutPutFormat property to "Currency"
Viewing the report
To view the report
- Add the ActiveReports viewer control to a Windows Form.
- Add the code needed to set the viewer document equal to the report document. See Using the ActiveReports Windows Form Viewer for help.
You can quickly view your report at design time by clicking the Preview tab at the bottom of the designer. |