To define a computed field in the DataWindow painter Design view:
Select Insert>Control>Computed Field from the menu bar.
Click where you want to place the computed field.
If the calculation is to be based on column data that changes for each row, make sure you place the computed field in the detail band.
The Modify Expression dialog box displays, listing:
DataWindow expression functions you can use in the computed field
The columns in the DataWindow object
Operators and parentheses
Enter the expression that defines the computed field as described in “Entering the expression”.
(Optional) Click Verify to test the expression.
PowerBuilder analyzes the expression.
Click OK.
You can enter any valid DataWindow expression when defining a computed field. You can paste operators, columns, and DataWindow expression functions into the expression from information in the Modify Expression dialog box. Use the + operator to concatenate strings.
You can use any built-in or user-defined global function in an expression. You cannot use object-level functions.
DataWindow expressions You are entering a DataWindow expression, not a SQL expression processed by the DBMS, so the expression follows the rules for DataWindow expressions. For complete information about DataWindow expressions, see the DataWindow Reference.
You can refer to other rows in a computed field. This is particularly useful in n-up DataWindow objects when you want to refer to another row in the detail band. Use this syntax:
ColumnName[x]
where x is an integer. 0 refers to the current row (or first row in the detail band), 1 refers to the next row, –1 refers to the previous row, and so on.
Table 20-1 shows some examples of computed fields.
To display |
Enter this expression |
In this band |
---|---|---|
Current date at top of each page |
|
Header |
Current time at top of each page |
|
Header |
Current page at bottom of each page |
|
Footer |
Total page count at bottom of each page |
|
Footer |
Concatenation of Fname and Lname columns for each row |
|
Detail |
Monthly salary if Salary column contains annual salary |
|
Detail |
Four asterisks if the value of the Salary column is greater than $50,000 |
|
Detail |
Average salary of all retrieved rows |
|
Summary |
Count of retrieved rows, assuming each row contains a value for EmpID |
|
Summary |
For complete information about the functions you can use in computed fields in the DataWindow painter, see the DataWindow Reference.
PowerBuilder provides a quick way to create computed fields that summarize values in the detail band, display the current date, or show the current page number.
To summarize values:
Select one or more columns in the DataWindow object’s detail band.
Select Insert>Control from the menu bar.
Select one of the options at the bottom of the cascading menu: Average, Count, or Sum.
The same options are available at the bottom of the Controls drop-down toolbar on the PainterBar.
PowerBuilder places a computed field in the summary band or in the group trailer band if the DataWindow object is grouped. The band is resized automatically to hold the computed field. If there is already a computed field that matches the one being generated, it is skipped.
To insert a computed field for the current date or page number:
Select Insert>Control from the menu bar.
Select Today() or Page n of n from the options at the bottom of the cascading menu.
The same options are available at the bottom of the Controls drop-down toolbar on the PainterBar.
Click anywhere in the DataWindow object.
If you selected Today, PowerBuilder inserts a computed field
containing this expression: Today()
.
For Page n of n, the computed field contains this expression: 'Page
' + page() + ' of ' + pageCount()
.
You can add buttons to the PainterBar in the DataWindow painter that place computed fields using any of the aggregate functions, such as Max, Min, and Median.
To customize the PainterBar with custom buttons for placing computed fields:
Place the mouse pointer over the PainterBar and select Customize from the pop-up menu.
The Customize dialog box displays.
Click Custom in the Select palette group to display the set of custom buttons.
Drag a custom button into the Current toolbar group and release it.
The Toolbar Item Command dialog box displays.
Click the Function button.
The Function For Toolbar dialog box displays.
Select a function and click OK.
You return to the Toolbar Item Command dialog box.
Specify text and microhelp that displays for the button, and click OK.
PowerBuilder places the new button in the PainterBar. You can click it to add a computed field to your DataWindow object the same way you use the built-in Sum button.