Using DataWindow expressions as property values

Many DataWindow object properties are backed by a DataWindow expression. 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. For example, the text color property of a GraphicObjectEditableColumn can be controlled by an expression such as:

if ( salary>50000, RGB(255,0,0), RGB(0,0,0) )

This expression changes the text color to red if the salary is greater than 50,000.

A DataWindow expression can include:

Assigning an expression

To assign an expression in the painter, click the icon next to the property you want to set and specify the expression in the Modify Expression dialog box.

To assign an expression in code, you can use the Modify and SetProperty methods, or you can use a descendant of the ExpressionBasedProperty class.

The ExpressionBasedProperty class has descendants for each of the datatypes used in expressions, including AlignmentProperty, BooleanProperty, BorderStyleProperty, ColorProperty, Int16Property, Int32Property, FileNameProperty, and StringProperty.

The following figure shows the hierarchy of the ExpressionBasedProperty classes.

Figure 6-2: ExpressionBasedProperty class hierarchy

The image shows the classes that inherit from the ExpressionBasedProperty class as described above.

Each of these property classes has two properties: Expression and Value. The Expression property is a String. The Value property is of the datatype required in the expression. For example, this is a ColorProperty expression property:

if ( salary>50000, RGB(255,0,0), RGB(0,0,0) )

The ColorProperty object’s Value property is a System.Drawing.Color datatype.

The following Visual Basic code sets the BackgroundColor.Value property of the last name column to Silver. It also sets the TextColor.Expression property to a string that sets the text color to red if the state is New York and green otherwise:

Dim colLName As  _
   Sybase.DataWindow.GraphicObjectEditableColumn

colLName = CType(dwSort.GetObjectByName("lname"),  _
   Sybase.DataWindow.GraphicObjectEditableColumn

colLName.BackgroundColor.Value =  _
   System.Drawing.Color.Silver
colLName.TextColor.Expression =  _
   "If(state ='NY', rgb(255, 0, 0), rgb(0, 255, 0) )"

Obtaining property values

You can use the Describe and GetProperty methods to get the value of expressions, or you can use dot notation with the ExpressionBasedProperty classes. The ValueInRow method for most of the descendent classes returns the value of a property for a column in a particular row.

The following C# code accesses the TextColor property and uses the ValueInRow method to return the color value in a specific row:

GraphicObjectEditableColumn gobColumn;
Int32 someRow;
System.Drawing.Color C1, C2;
String expr;

C1 = gobColumn.TextColor.Value;
expr = gobColumn.TextColor.Expression;
C2 = gobColumn.TextColor.ValueInRow ( someRow );

Properties with subproperties

For properties that use multiple “dots,” new property objects are instantiated when they are needed.