Skip to main content
All docs
V26.1
  • Calculate Discounted Price Tutorial

    • 6 minutes to read

    This tutorial creates a “Price Book” report bound to the Products table of the sample Northwind database. The report uses a calculated field to display a discounted price for each product, and a report parameter that allows users to change the discount rate at runtime without editing the calculated field’s expression.

    Recalculate a Report

    Sample SQLite Database

    This tutorial uses the following SQLite database shipped with compiled DevExpress VCL demos:

    %PUBLIC%\Documents\DevExpress VCL Demos\MegaDemos\Product Demos\ExpressReports\Data\nwind.db
    

    Download: Compiled VCL Demos

    Step 1 — Create a Report Bound to the Products Table

    1. Create a new Delphi or C++Builder project and place TdxReportControl and TdxBackendDataConnectionManager components on a form:

      Basic Report Project

    2. Configure a Database (SQL) connection to use the sample SQLite database:

      XpoProvider=SQLite;Data Source=C:\Users\Public\Documents\DevExpress VCL Demos\MegaDemos\Product Demos\ExpressReports\Data\nwind.db
      
    3. Right-click the TdxReportControl component and select Designer… from the context menu.

    4. In the Report Designer window, click the hamburger button to display the component menu:

      VCL Reports: The Report Designer Dialog

    5. Select Design in the Report Wizard… and create a tabular report bound to the previously configured SQL Data Source.

      Report Data Source

    6. Expand the Tables node and select the Products table:

      Report Data Source

    7. On the Define Report Layout step, select the following fields from the Products table:

      • ProductName
      • UnitPrice
      • UnitsInStock

        Report Fields

    8. Click the Finish button to generate the report layout:

      Initial Report Layout

    Tip

    Refer to the following tutorial for detailed instructions on how to create a project, configure a SQL data source, and generate a table report using the Report Wizard:

    Read Tutorial: A Report Viewer in a Tabbed MDI (SQL Database)

    Step 2 — Add a Report Parameter for the Discount Rate

    1. In the FIELD LIST panel, click the Plus button next to Parameters to create a new report parameter:

      Add a Report Parameter

    2. Specify the following parameter options:

      Name (1)
      discountParameter
      Description (2)
      Discount (%)
      Type (3)
      Number (floating-point)
      Value (4)
      10 (a default 10% discount)

      Discount Report Parameter Options

    3. Click the OK button to create the parameter.

    Step 3 — Add a Calculated Field for the Discounted Price

    1. Select the Products data member (1) and click the Add calculated field button (2). Since the created field is scoped to the Products table, you can use the field to calculate a value for each row:

      Add a Calculated Field to the Products Data Member

    2. Click the Edit button next to the newly added field to display its properties:

      Edit a Calculated Field

    3. Specify the following calculated field options:

      Name
      discountedPriceField
      Display Name
      Discounted Price
      Field Type
      Float
      Expression
      Click the ellipsis button to open the Expression Editor and construct a field’s formula.

      Calculated Field Properties

    Calculated Field Formula

    In the Expression Editor dialog, build a formula that calculates the value of the field from the product price and the report parameter:

    1. Double-click the Unit Price field to add it as an expression operand.
    2. Expand Parameters and double-click discountParameter to add it as an expression operand.

    Calculated Field Operands

    Add operators to subtract the discount from the unit price:

    [UnitPrice] * (1 - ?discountParameter / 100)
    

    Click the OK button to apply the created expression to the calculated field.

    Step 4 — Display the Calculated Field in the Report

    1. Insert a new Discounted Price column into report details:

      Calculated Field Column

      How to add a report column…
      1. Right-click the Unit Price cell in the group header and select Insert > Column to Right from the context menu.
      2. Double-click the newly added cell in the group header and rename the cell to Discounted Price.
      3. Right-click the [UnitPrice] cell in the details row and select Insert > Column to Right from the context menu.
      4. Resize the new cells and change their styles to match the rest of the report.
    2. Drag the Discounted Price field from the FIELD LIST panel onto the detail band that is next to Unit Price and Units In Stock cells.

      Display the Discounted Price Calculated Field

      Tip

      You can also reference the calculated field in the cell’s expression using the [<field name>] or [<field display name>] syntax:

      Reference the Calculated Field in Expressions

    3. Change the field’s format to Currency:

      How to change the field format…
      1. Select the new Discounted Price cell in the details row.
      2. Switch to the PROPERTIES panel and expand TABLE CELL TASKS.
      3. Enter {0:$0.00} in the Text Format String field to display prices with a dollar sign and two decimal places.

      Calculated Field Format

    Step 5 — Preview the Report

    1. Click the PREVIEW button in the upper-right corner of the Report Designer.
    2. Modify the Discount (%) parameter value.
    3. Click the Submit button to rebuild the report and display updated discounted prices.

    Preview Calculated Field

    Step 6 — Build & Test Your App

    Post-Build Events for WebView2 Loader DLL Deployment

    All applications that include the TdxReportControl component require the WebView2 Runtime as a dependency.

    To automatically copy a 32- or 64-bit WebView2Loader.dll file to the target build folder, do the following:

    1. Open the Project Options dialog. Select the Project → Options… item in the RAD Studio menu or press Ctrl + Shift + F11).
    2. Select Build → Build Events in the tree view pane on the left and select the following option in the Target combo box:

      'All Configurations - All Platforms'

      VCL Shared Libraries: Project Options — Select the Target Platform

    3. Copy the command line for the target compiler and platform:

      copy /Y "$(BDS)\Redist\$(Platform)\WebView2Loader.dll" "$(OUTPUTDIR)"
      
    4. Paste the DLL deployment command line into the Commands box:

      VCL Shared Libraries: Paste the DLL Deployment Command Line

    5. Optional. This step is required only for the Modern C++ RAD Studio compiler.

      If you use Windows 64-bit (Modern) as a build target, select the Windows 64-bit (Modern) platform (1), expand the Post-build events → Commands node (2), uncheck the Inherit option (3), and paste the Modern compiler-specific command into the Commands box (4):

      VCL Shared Libraries: Project Options — Paste Windows 64-bit Modern-Specific Command Line

    6. Click Save to apply pending changes and close the Project Options dialog.

    7. Build the project. Confirm that the configured post-build event is trusted in the following dialog:

      VCL Reports: The "Trust this Project" Dialog

    All build operations in the current RAD Studio project now ensure that the platform-specific WebView2Loader.dll file version is available in the target build folder (for both Debug and Release configurations).

    Run & Test the App

    Run the app. Specify a discount percentage in the Discount (%) field (1) and click the SUBMIT button (2) to generate a Price Book with the specified discounted price.

    Run Calculated Field App

    Basic Tutorials

    Read Tutorial: Create a Table Report Using JSON Data

    Load JSON data from a remote source, define the report layout, and bind the data to the report at design time using the Report Wizard dialog. Display the resulting report in a VCL application.

    Featured Components: TdxReport | TdxBackendDataConnectionManager | TdxBackendInMemoryJSONConnection

    Read Tutorial: Bind a Table Report to FireDAC Data

    Follow this tutorial to create a table report and bind it to data using standard FireDAC components (TFDQuery and TFDConnection) shipped with the RAD Studio IDE.

    Featured Components: TdxReport | TdxBackendDataConnectionManager | TdxBackendDataSetJSONConnection