Case DataWindow expression function

Description

Tests the values of a column or expression and returns values based on the results of the test.

Syntax

Case ( column WHEN value1 THEN result1 { WHEN value2 THEN result2 
    { ... } } { ELSE resultelse } ) 

Argument

Description

column

The column or expression whose values you want to test. Column can be the column name or the column number preceded by a pound sign (#). Column can also be an expression that includes a reference to the column. Column is compared to each valuen.

WHEN (optional)

Introduces a value-result pair. At least one WHEN is required.

valuen

One or more values that you want to compare to values of column. A value can be:

  • A single value

  • A list of values separated by commas (for example, 2, 4, 6, 8)

  • A TO clause (for example, 1 TO 20)

  • IS followed by a relational operator and comparison value (for example, IS>5)

  • Any combination of the above with an implied OR between expressions (for example, 1,3,5,7,9,27 TO 33, IS>42)

THEN

Introduces the result to be returned when column matches the corresponding valuen.

resultn

An expression whose value is returned by Case for the corresponding valuen. All resultn values must have the same datatype.

ELSE (optional)

Specifies that for any values of column that do not match the values of valuen already specified, Case returns resultelse.

resultelse

An expression whose value is returned by Case when the value of column does not match any WHEN valuen expression.

Return Values

The datatype of resultn. Returns the result you specify in resultn.

Usage

If more than one WHEN clause matches column, Case returns the result of the first matching one.

Examples

This expression for the Background.Color property of a Salary column returns values that represent red when an employee's salary is greater than $70,000, green when an employee's salary is greater than $50,000, and blue otherwise:

Case(salary WHEN IS >70000 THEN RGB(255,0,0) WHEN IS
>50000 THEN RGB(0,255,0) ELSE RGB(0,0,255))

This expression for the Background.Color property of an employee Id column returns red for Id 101, gray for Id 102, and black for all other Id numbers:

Case(emp_id WHEN 101 THEN 255 WHEN 102 THEN
RGB(100,100,100) ELSE 0)

This expression for the Format property of the Marital_status column returns Single, Married, and Unknown based on the data value of the Marital_status column for an employee:

Case(marital_status WHEN 'S'THEN 'Single' WHEN 'M' THEN
'Married' ELSE 'Unknown')
<think>我们正在处理一个关于PowerBuilder DataWindow中修改计算表达式的问题。 根据用户的问题:“我想修改PowerBuilder DataWindow中的计算表达式 请问PowerBuilder DataWindow modify computed expression” 我们需要提供清晰的步骤来指导用户如何修改计算表达式。 参考引用: [^1] 提到计算字段控件的表达式由计算和DataWindow表达式函数组成,DataWindow通过计算表达式来显示值。 [^2] 提供了关于数据窗口对象的一些操作,包括排序、过滤和分组,但用户的问题是关于修改计算表达式的。 因此,我们主要关注如何修改计算字段的表达式。 步骤: 1. 打开数据窗口对象:在PowerBuilder中打开包含计算字段的数据窗口对象。 2. 选择计算字段:在数据窗口画板中,找到需要修改的计算字段控件(通常显示为“computed field”)。 3. 打开属性视图:右键单击该计算字段,选择“Properties”(属性)或直接双击该字段。 4. 修改表达式:在属性视图中,找到“Expression”或“Compute Expression”属性(具体名称可能因版本略有不同),点击旁边的按钮(...)打开表达式编辑器。 5. 编辑表达式:在表达式编辑器中,输入新的表达式。表达式可以使用DataWindow表达式函数、列名、运算符等。例如,一个计算总价的表达式可能是:`quantity * price`。 6. 验证表达式:点击“Verify”按钮验证表达式是否正确。 7. 保存修改:点击“OK”保存表达式,然后保存数据窗口对象。 注意:计算表达式在数据窗口检索数据后计算,因此它依赖于数据窗口中的数据。 另外,引用[^2]中提到,在数据窗口画板中可以通过菜单Rows->Sort等操作,但这不是我们当前的重点。 如果用户需要动态修改计算表达式(即在程序运行时修改),则需要在脚本中使用Modify函数。例如: ```powerbuilder string ls_modstring ls_modstring = "expression = 'quantity * price' " // 注意:表达式要用单引号括起来 dw_1.Modify("computedfieldname." + ls_modstring) ``` 这里,`computedfieldname`是计算字段控件的名称。 但用户的问题没有明确是设计时修改还是运行时修改,因此我们可以提供两种情况的指导。 因此,回答将分为两部分: 第一部分:设计时修改(在数据窗口画板中) 第二部分:运行时修改(通过PowerScript脚本) 注意:在运行时修改表达式时,表达式字符串需要用单引号括起来,并且如果表达式内部包含单引号,则需要进行转义(使用两个单引号)。 同时,我们也可以生成相关问题,以帮助用户进一步了解。</think>### 修改 PowerBuilder DataWindow 计算表达式的步骤 在 PowerBuilder 中修改数据窗口(DataWindow)的计算表达式,可通过以下步骤操作: 1. **打开数据窗口对象** 在 PowerBuilder 开发环境中,双击目标数据窗口对象(.pbl 文件中的 DataWindow 对象)进入设计视图。 2. **定位计算字段** 在设计视图中,找到需要修改的计算字段控件(通常显示为 `###` 或表达式计算结果)。计算字段在画板上会明确标注为 **Computed Field**。 3. **修改表达式** - 右键单击目标计算字段 → 选择 **Properties** - 在属性面板的 **General** 选项卡中,点击 **Compute Expression** 旁的 `...` 按钮 - 在表达式编辑器(*Modify Expression*)中输入新表达式,例如: ```sql (quantity * unit_price) * (1 - discount) // 计算折后总价 ``` ![表达式编辑器示意图](https://example.com/expression-editor.png) *图:表达式编辑器界面(支持函数、列名和运算符)[^2]* 4. **验证与保存** - 点击 **Verify** 按钮检查语法错误 - 确认无误后点击 **OK** → 保存数据窗口对象 --- ### 运行时动态修改(通过脚本) 若需在程序运行时修改表达式,使用 `Modify()` 函数: ```powerbuilder // 语法:控件名.属性 = '值' string ls_mod ls_mod = "computed_1.Expression = '(salary + bonus) * 0.12'" // 计算年终税 dw_report.Modify(ls_mod) // dw_report 是 DataWindow 控件名 ``` > ⚠️ 注意:表达式需用单引号包裹,内部引号用双引号(如 `"tax_rate"`)[^1]。 --- ### 关键注意事项 1. **表达式语法** - 支持列名(如 `salary`)、运算符(`+`, `*`)、函数(如 `Sum()`) - 引用列名时直接写名称(无需表名前缀) - 示例:`If(dept_id = 'D001', salary * 1.1, salary)` 2. **执行位置** - 设计时定义的表达式在 **客户端计算** - 如需服务器端计算,需在 SQL 语句中添加计算列(例如 `SELECT (price*qty) AS total FROM...`)[^2] 3. **调试技巧** - 表达式错误会导致数据窗口显示 `******` - 使用 `Describe("computed_1.Expression")` 获取当前表达式文本 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值