InitializeData - cCJGrid
Loads the initial data into the grid.
Type: Procedure
Parameters:
| Parameter | Description |
|---|---|
| DataSource | Variant array of rows. Most likely this will be of type tDataSourceRow. |
Syntax
Procedure InitializeData Variant[] DataSource
Call:
Send InitializeData DataSource
Description
InitializeData is used to load your data into a grid. This should be sent passing all of your data as an array struct variable.
The passed variable is typed as a variant array. In theory, this variable can be based on any type depending on the interface requirements of your Datasource object. Whatever the datatype is, the data passed must be of the type that the datasource object expects. If you are not customizing your datasource, and you probably will not, the type passed is an array of tDataSourceRow.
The sValue member is the most important member of the tDataSourceRow struct. It is an array of column values, and you must provide a value for each column you've defined in your grid. If you provide too many columns, the extra data will be ignored and lost. If you provide too few columns, a runtime error will occur.
If you want to reinitialize the data in your grid after you've made a "batch" update, use ReInitializeData.
When to Initialize the Grid
Most often, you will want to initialize your Grid when it is being activated. You must do that after the COM control has been created. The Activating event is a good place to load data.
Procedure Activating
Forward Send Activating
// Assume this creates the data and calls InitializeData
Send LoadMyData
End_Procedure
InitializeData always starts by clearing the datasource. Therefore, if you wish to refresh a grid, you can always send InitializeData again.
InitializeData and cCJGrids
Before you call InitializeData, you must create and fill your array. The mechanism for doing this is entirely up to you.
This example shows how to fill data for a single column grid from static data defined within your program.
Procedure LoadMyData
tDataSourceRow[] DataSourceArray
Move "First Choice" to DataSourceArray[0].sValue[0]
Move "Second Choice" to DataSourceArray[1].sValue[0]
Move "Third Choice" to DataSourceArray[2].sValue[0]
Move "Last Choice" to DataSourceArray[3].sValue[0]
Send InitializeData DataSourceArray
End_Procedure
This example shows how you would do the same with a three-column grid.
Procedure LoadMyData
tDataSourceRow[] DataSourceArray
Move "First Choice" to DataSourceArray[0].sValue[0]
Move "A" to DataSourceArray[0].sValue[1]
Move "100" to DataSourceArray[0].sValue[2]
Move "Second Choice" to DataSourceArray[1].sValue[0]
Move "B" to DataSourceArray[1].sValue[1]
Move "101" to DataSourceArray[1].sValue[2]
Move "Third Choice" to DataSourceArray[2].sValue[0]
Move "C" to DataSourceArray[2].sValue[1]
Move "101" to DataSourceArray[2].sValue[2]
Move "Last Choice" to DataSourceArray[3].sValue[0]
Move "D" to DataSourceArray[3].sValue[1]
Move "101" to DataSourceArray[3].sValue[2]
Send InitializeData DataSourceArray
End_Procedure
More typically, your data might come from some other source, such as a CSV file, an XML file, an SQL query, or some sort of programmatic method for fetching your data. This example shows how the grid could be loaded with data from an SQL query.
Procedure LoadMyData
tDataSourceRow[] DataSourceArray
Get FetchData to DataSourceArray
Send InitializeData DataSourceArray
End_Procedure
Function FetchData Returns tDataSourceRow[]
String sConnect sQuery
Handle hSQL hODBC hStmt
Integer iFetchResult iRows
tDataSourceRow[] DataSourceArray
tDataSourceRow DataRow
Move "SERVER=(local);Trusted_Connection=yes;Database=Order" to sConnect
Move "select name, address, city from customer where state = 'CA' order by name;" to sQuery
Get Create (RefClass(cSQLHandleManager)) to hSQL
Send SQLSetConnect of hSQL MSSQLDRV_ID sConnect
Get SQLConnect of hSQL "" "" to hODBC
If (hODBC <> 0) Begin
Get SQLOpen of hODBC to hStmt
If (hStmt <> 0) Begin
Send SQLExecDirect of hStmt sQuery
Repeat
Get SQLFetch of hStmt to iFetchResult
If (iFetchResult <> 0) Begin
Get SQLColumnValue of hStmt 1 to DataRow.sValue[0] // Name
Get SQLColumnValue of hStmt 2 to DataRow.sValue[1] // Address
Get SQLColumnValue of hStmt 3 to DataRow.sValue[2] // City
Move DataRow to DataSourceArray[iRows]
Increment iRows
End
Until (iFetchResult = 0)
Send SQLClose of hStmt
End
Send SQLDisconnect of hODBC
End
Send Destroy of hSQL
Function_Return DataSourceArray
End_Procedure
InitializeData and cDbCJGrids
InitializeData is primarily used to load static data into a non-data aware cCJGrid. cDbCJGrids are dynamic, and data is loaded and unloaded as needed. How the data is loaded is determined by the DataDictionary (Server), the Index order (Ordering), and the data binding of the cDbCJGridColumn objects (Entry_Item). This information is used by its cDbCJGridDataSource object to load data as needed. Therefore, you usually do not use InitializeData.
It is possible to create static cDbCJGrids. If you set pbStaticData to True, you can load a customized set of static data using InitializeData. This data can be edited just like any grid, but it will not use the automatic finding services built into the cDbCJGridDataSource object.
This example shows how you would load data into a static cDbCJGrid. Assume that pbStaticData is True and Auto_Fill_State is False.
Procedure LoadMyData
Handle hoServer hoDataSource
Boolean bFound
tDataSourceRow[] DataSource
Integer iRows iFile
Get Server to hoServer
Get Main_File of hoServer to iFile
Send Request_Read of hoServer FIRST_RECORD iFile 1
Move (Found) to bFound
Get phoDataSource to hoDataSource
While bFound
If (Customer.State<>"CA") Begin
// creates a datasourcerow based on current buffer data
Get CreateDataSourceRow of hoDataSource to DataSource[iRows]
Increment iRows
End
Send Request_Read of hoServer GT iFile 1
Move (Found) to bFound
Loop
Send InitializeData DataSource
End_Procedure
Sorting Column Data
Sorting of data is built into grids and straightforward for most uses. For custom sorting, you can use piSortColumn and pbReverseOrdering. These are used differently with regular grids and data-aware grids. See the documentation for these properties for more details.
See Also