Using DataWindow expressions as property values

When a DataWindow object property's value can be an expression, you can make the control's appearance or other properties depend on other information in the DataWindow.

A DataWindow expression can include:

  • Operators

  • The names of controls within the DataWindow, especially column and computed field names

  • DataWindow expression functions. Some functions, such as IsRowNew, refer to characteristics of an individual row

  • User-defined functions

Different formats for the expression

When you assign an expression in the painter, you specify just the expression:

DataWindowexpression

When you assign an expression in code, you specify a default value, a tab, and the expression:

defaultvalue [tab] DataWindowexpression

Examples

In the painter

This expression for a column called emp_lname is applied to the Background.Color property. It causes the name's background to be light gray (15790320) if the current row (person) uses the day care benefit. If not, the background color is set to white:

If(bene_day_care = 'Y', 15790320, 1677215)

In code

The expression assigned to the Background.Color property includes a default value. Nested quotes complicate the syntax:

PowerBuilder

dw_1.Object.emp_lname.Background.Color = "16777215 ~t If(bene_day_care = 'Y', 15790320, 16777215)"

JavaScript

dw_1.Modify("emp_lname.Background.Color = \"16777215 \t If(bene_day_care = 'Y', 15790320, 16777215)\");

More examples in the DataWindow painter and in code

These examples illustrate the difference between the format for a DataWindow expression specified in the DataWindow painter versus in code.

Border property

The expression applied to the Border property of the salary_plus_benefits column displays a border around salaries over $60,000:

If(salary_plus_benefits > 60000, 1, 0)

This statement changes the expression in code:

dw_1.Object.salary_plus_benefits.Border = &
      "0 ~t If(salary_plus_benefits > 60000, 1, 0)"

Font.Weight property for a column

To make out-of-state (not in Massachusetts) names and numbers bold in a phone list, apply this expression to the name and phone_number columns. The state column must be part of the data source, but it does not have to be displayed:

If(state = 'MA', 400, 700)

This statement changes the expression in code:

dw_1.Object.name.Font.Weight = &
      "700 ~t If(state = 'MA', 400, 700)"
dw_1.Object.phone_number.Font.Weight = &
      "700 ~t If(state = 'MA', 400, 700)"

Brush.Color property for a rectangle

This expression, applied to a rectangle drawn around all the columns in a tabular report, causes alternate rows to be shaded (a graybar effect). Make sure the columns and computed fields have a transparent background. The expression Mod(GetRow( ), 2) = 1 distinguishes odd rows from even rows:

If(Mod(GetRow(), 2) = 1, 16777215, 15790320)

This statement changes the expression in code:

dw_1.Object.rectangle_1.Brush.Color = &
   "0 ~t If(Mod(GetRow(), 2) = 1, 16777215, 15790320)"

Brush.Color and Brush.Hatch properties for a rectangle

To highlight employees whose review date is approaching, draw a rectangle behind the row. This expression for the rectangle's Brush.Color property makes the rectangle light gray for employees for whom the month of the start date matches the current month or the next month:

If(month(start_date) = month(today())
or month(start_date) = month(today()) + 1
or (month(today()) = 12 and month(start_date) = 1), 12632256, 16777215)

A similar expression for the Brush.Hatch property makes the fill pattern of the rectangle Bdiagonal (1) for review dates that are approaching. Otherwise, the rectangle is transparent (7) so that it does not show:

If(month(start_date) = month(today())
or month(start_date) = month(today()) + 1
or (month(today()) = 12 and month(start_date) = 1), 1, 7)

You can also set the Pen.Color and Pen.Style properties to affect the outline of the rectangle.

If you wanted to change the Brush.Color property in code instead of setting it in the painter, the code would look like this:

dw_1.Object.rectangle_1.Brush.Color = &
   "'16777215 ~t " + &
   "If(month(start_date) = month(today()) " + &
   "or month(start_date) = month(today()) + 1 " + &
   "or (month(today()) = 12 " + &
   "and month(start_date) = 1), 12632256, 16777215)'"

Font.Height property for a rectangle

This expression applied to the Font.Height property of a text control makes the text control in the first row of a DataWindow larger than it appears in other rows. Make sure the borders of the text control are large enough to accommodate the increased size:

If(GetRow() = 1, 500, 200)

This statement changes the expression for the text control t_desc in code:

dw_1.Object.t_desc.Font.Height = &
      "200 ~t If(GetRow() = 1, 500, 200)"

For more information

For more information about DataWindow expressions, see DataWindow Operators and Expressions