ActiveReports for .NET 3 Online Help Request technical support
Walkthrough: Calculated Fields
See Also
User Guide > Samples and Walkthroughs > Walkthroughs > Standard Edition Walkthroughs > Basic > Walkthrough: Calculated Fields

Glossary Item Box

 ActiveReports allows you to use the DataField property of a textbox to perform calculations based on the value of specific data fields.

This walkthrough illustrates how to create a simple report using calculated fields.

The walkthrough is split up into the following activities:

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

  1. Open a new project in Visual Studio.
  2. From the Project menu, select Add New Item.
  3. Select ActiveReports 3.0 File and rename the file rptCalFields.
  4. Click Open.

Connecting the report to a database

To connect the report to a database

  1. Click on the gray report DataSource icon in the Detail section to open the report DataSource dialog.
  2. Select the "OLE DB" tab.
  3. Click on Build.
  4. Select "Microsoft Jet 4.0 OLE DB Provider" and click Next.
  5. Click the ellipsis button to browse for the access path to the Northwind database. Click Open once you have selected the appropriate access path.
  6. Click OK to continue.
  7. 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".
  8. Click OK to return to the report design surface.

Adding controls to contain data

To add controls to the report

  1. Add a GroupHeader/Footer section to rptCalFields by right-clicking on the design surface of the report and selecting Insert > Report Header/Footer.
  2. 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
  3. 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

  4. Set the CanShrink property of the Detail section to True.
  5. 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
  6. 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.
  7. 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
  8. 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.
  9. 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

  1. Add the ActiveReports viewer control to a Windows Form.
  2. 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.

See Also

©2009. All Rights Reserved.