Using Hidden Columns
If you set a column object's pbVisible and pbShowInFieldChooser properties to False, you can create a hidden “virtual” column. This type of column cannot be controlled by the end user and will always be hidden. This provides a mechanism for storing additional information in each row that is never displayed.
The following example shows the object declarations for two columns: a regular (visible) column and a hidden column:
Object oCustNames is a cCJGrid
Set Size to 192 143
Set Location to 4 5
Object oNameCol is a cDbCJGridColumn
Set piWidth to 183
Set psCaption to "Customer Name"
End_Object
Object oCreditRisk is a cCJGridColumn
Set pbVisible to False
Set pbShowInFieldChooser to False
End_Object
End_Object
The values for hidden columns are created when you populate your datasource in exactly the same way that you would populate the data for the other columns in your grid. The following example shows how we could populate the two columns (above) with data from the Customer table:
Procedure LoadData
Handle hoDataSource
tDataSourceRow[] TheData
Boolean bFound
Integer iRow
Integer iName iCredit
Get phoDataSource to hoDataSource
// Get the datasource indexes of the various columns
Get piColumnId of oNameCol to iName
Get piColumnId of oCreditRisk to iCredit
// Load all data into the datasource array
Move 0 to iRow
Clear Customer
Find ge Customer by 1
Move (Found) to bFound
While bFound
Move Customer.Name to TheData[iRow].sValue[iName]
Move Customer.Balance to TheData[iRow].sValue[iCredit]
Find gt Customer by 1
Move (Found) to bFound
Increment iRow
Loop
// Initialize Grid with new data
Send InitializeData TheData
Send MovetoFirstRow
End_Procedure
Procedure Activating
Forward Send Activating
Send LoadData
End_Procedure
This code should be placed inside the grid object and would be called when the grid object is activated.
These column values can be accessed programmatically using the SelectedRowValue and RowValue methods. For example:
Get RowValue of oCreditRisk iRow to nDue
This would get the value of the oCreditRisk column for the iRow'th row.
After the grid has been activated, you would need a way to initialize the hidden columns for new rows being entered by the user. When a new row is created, the datasource object sends the InitialValue message to each grid column object. The returned value becomes the default initial value for the column in the new row. By default, this returns blank. You would calculate a hidden column's value by augmenting the InitialValue method.
The data in a hidden column can be used for a variety of purposes. For example, if in the above example you want to highlight all customer names who are credit risks, you could augment the OnSetDisplayMetrics event in the customer name column to manipulate the way the customer name is drawn based on the customer balance hidden column.
Object oNameCol is a cCJGridColumn
Set piWidth to 183
Set psCaption to "Customer Name"
Procedure OnSetDisplayMetrics Handle hoGridItemMetrics ;
Integer iRow String sValue
Number nDue
Get RowValue of oCreditRisk iRow to nDue
If (nDue > 3000) Begin
Set ComForeColor of hoGridItemMetrics to clRed
End
Else If (nDue < 1000) Begin
Set ComForeColor of hoGridItemMetrics to clGreen
End
End_Procedure
End_Object
Object oCreditRisk is a cCJGridColumn
Set pbVisible to False
Set pbShowInFieldChooser to False
End_Object