Iterating through grid rows means accessing rows in a series, one by one. You may need to iterate through TcxGrid rows if you need to locate a particular row, or perform the same set of operations on each row (TcxGrid is the class name of the QuantumGrid control). When you locate the desired row, you can obtain values of the rows cells and save them to a file.
QuantumGrid can display data in several levels. To save all grid data to a file, your script should iterate through grid rows starting from the root level, export each rows data, obtain child rows and export their data as well. Each level has an associated view object that determines, which data the level displays and how it displays it. To iterate through rows, use methods and properties of the view object. Note, that since the grid updates child data when the appropriate parent row is selected, you may need to focus rows when iterating through them.
Below is a script sample that demonstrates how you can iterate through the QuantumGrid rows using internal methods and properties of the TcxGrid object. The script saves grid data and column names to an XML file. The routine assumes that the grid contains several levels of data, each level displays data in a tabular form and uses the TcxGridDBTableView object. Also, we assume that the grid does not contain group rows.
In order for TestComplete to be able to access the internal methods and properties of the TcxGrid object, the tested application must be compiled as an Open Application with debug information.
The sample contains the following routines:
Main - The main routine of the sample. It obtains the scripting object that corresponds to the tested TcxGrid control and then passes this object and the desired file name to the
ExportToFile routine, which performs the export of grid data to an XML file.
ExportToFile - This routine performs the following actions:
- Creates the desired file.
- Writes the root tag (grid) to the file.
- Calls the following two routines:
ExportColumnsInfo - This routine saves the column captions used on each level to the file.
ExportToFile calls this routine and passes to it the object that corresponds to the topmost grid level.
ExportData - This routine iterates through grid rows and export their data to the file.
ExportToFile calls this routine and passes to it the view object that corresponds to the topmost grid level.
- Closes the root tag and closes the file.
To write data to a file, we use the
FileSystemObject OLE object. You can find the object reference in the MSDN library. The on-line version is available at http://msdn.microsoft.com.
To create an XML file, we use the simplest approach: we write tag names as text strings.
ExportColumnsInfo - Saves column captions to a file. The routine uses the following parameters:
- level - Specifies the object that corresponds to a grid level.
- levelIndex - Specifies the level index. 0 means the topmost level in the grid.
- XmlFile - The file object that is used to write data to the .XML file.
- indent - Helper parameter that defines the indentation for the tags.
The routine performs the following actions:
- Saves the columns tag to the file. The level attribute specifies the level, whose columns will be saved.
- Uses the
GridView property of the level object to obtain the view object that is used by the level.
- Uses the views
Columns properties to iterate through columns. The routine obtains the column caption by calling the columns
Caption property and saves this caption to the file.
- Finally, the routine uses the
Items property of the level object to process child levels. After that the routine closes the columns tag.
ExportData - This routine exports the grid to a file. It obtains the following parameters:
- view - The view object that provides access to the exported rows.
- XmlFile - The file object that is used to write data to the desired .XML file.
- indent - Helper parameter that defines the data indentation.
The routine iterates through rows in a loop. To determine the number of rows, we use the
ViewData.RowCount property of the view object. To access a row, we use the views
ViewData.Rows property that returns the row object by the desired row index (the index is zero-based).
Within the loop body, the routine performs the following actions for each row:
ConvertSpecialChars - This is a helper routine. It replaces special characters in a string (&, <, > and others) with their appropriate constants (&, <, > and so on) and then returns the resulting string.
Here is the sample code: