In This Topic
This sample shows how to use calculated fields and data field expressions for simple calculations in a report.
Sample Location
Visual Basic.NET
https://github.com/activereports/Samples18/tree/main/API/Section/Summary/VB.NET
C#
https://github.com/activereports/Samples18/tree/main/API/Section/Summary/C#
Details
When you run this sample, The Viewer control with the Select Report drop-down list is displayed. There, you can select one of the two reports - OrdersReport or DataFieldExpressionsReport, and click the Load Report button to display the report in the Viewer.
The OrdersReport shows how to use calculated fields, where the field values are calculated in code. A custom field is added to the Fields collection in the DataInitialize event and the field value is calculated in the FetchData event.
The DataFieldExpressionsReport demonstrates the use of data field expressions for simple calculations within the same section of the unbound report using known Fields collection values. These data field expressions cannot be used with the built in summary functions.
Note: To run this sample, you must have access to the
Nwind.db. The NWIND.db file can be downloaded from
GitHub: ..\Samples18\Data\NWIND.db.
The sample consists of the StartForm and two reports - OrdersReport and DataFieldExpressionsReport.
- StartForm: The Viewer control has the Dock property set to Fill. This ensures that the viewer resizes along with the form at run time. Right-click the form and select View Code to see the code used to run the report and display it in the viewer.
- OrdersReport: The report shows the ProductID, UnitPrice, Quantity, Discount, Extended Price and Total value for each OrderID. The Extended Price value is a calculated field that displays the result of the formula specified in FetchData event.
The OrdersReport uses a GroupHeader section, a Detail section and a GroupFooter section as well as a Label in the PageFooter section to display data.
Note: Except for the Detail section, all sections come in header and footer pairs. Unused sections have their Height properties set to 0 and their Visible properties set to False.
ghOrderID section
This group header section has the DataField property set to OrderID. This setting, along with data sorted by the same field, displays a report grouped by OrderID. The section contains one bound TextBox control to display the OrderID at the beginning of each group.
Detail section
The Detail section of this report contains 5 bound TextBox controls that render for each row of data of the OrderID.
gfOrderID section
This group footer section displays total of the gfOrderID data in TextBox controls that have values passed in code, or are bound to fields from the report's Fields collection using the DataField property. The total extended price for the OrderID is summarized using the following properties:
SummaryFunc: Sum (the default value)
Adds values rather than counting or averaging them.
SummaryGroup: ghOrderID
Summarizes the values that fall within the current OrderID group.
SummaryRunning: Group
Calculates a running summary (each value is the sum of the current value and all preceding values) within the same group level.
SummaryType: SubTotal
Summarizes the current group rather than a page or report total.
PageFooter section
The page footer section contains a static Label control that prints at the bottom of each page and contains the note on the number of pages in the report.
- DataFieldExpressionsReport: The DataFieldExpressionsReport displays data, using the Fields collection from the OrderDetail class. The report data under ExtendedPrice is calculated by the data field expression, specified in the DataField property of the txtExtendedPrice TextBox at the design time. The DataFieldExpressionsReport is an unbound report that uses the field values from the OrderDetail class for displaying the report data.
PageHeader section
The PageHeader section contains one Label control with the note text and four labels with the names of the report data fields.
Detail section
The Detail section contains four textboxes that use the Fields collection values to display the report data. The DataField property of the txtExtendedPrice textbox in the Detail section demonstrates how to format your data field expression.
OrderDetail class
The OrderDetail class contains data that is used in the fields of the report. When the report is run, the values of these fields are used to display data of the report. The field values are bound to the fields in the DataInitilize event and the data is bound to the field values in the FetchData event of the DataFieldExpressionsReport.