ActiveReports requires no special handling for "Top N Records" report styles. Such reports can be easily implemented by setting the data source to a Top N filtered query. If the data source does not support Top N queries, the query can be set to return records ordered by the Top N value descending. Then the MaxRows property should be set to N.
For example, to list the top 10 customers by their sales numbers, you can create a query that returns all customer sales ordered by the sales value descending. Then set the MaxRows property of the data control to 10. ActiveReports will process only 10 records from the sorted query results.
This walkthrough illustrates how to create a report that lists the top ten customers by sales.
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 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 rptTopN.
- Click Open.
Connecting the data source to a database
To connect the data source to a database
- Click on the gray report DataSource icon in the Detail section to open the report DataSource dialog.
- 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 TOP 10 Customers.CompanyName, Sum([UnitPrice]*[Quantity]) AS Sales FROM (Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID) INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID GROUP BY Customers.CompanyName ORDER BY Sum([UnitPrice]*[Quantity]) DESC".
- Click OK to return to the report design surface.
Adding controls to contain data
To add controls to the report
- 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 Location Miscellaneous CompanyName Company Name 0.5, 0 Sales Sales 5, 0 OutputFormat = Currency - Add the following control to the PageHeader section:
Control Name Text Location Label lblTopTen Top Ten Customers 2.6, 0
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. |