Iterating Through Rows in Developer Express QuantumGrid

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 row’s 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 row’s 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 view’s ColumnCount and Columns properties to iterate through columns. The routine obtains the column caption by calling the column’s Caption property and saves this caption to the file.

    • Finally, the routine uses the Count and 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 view’s 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:



    • Assigns True to the row’s Focused property to move the focus to the row. This way the row’s child data updates.

    • Opens the row tag.

    • Iterates through a row’s cells by using the ValueCount and DisplayTexts properties of the row object. ValueCount specifies the number of cells in the row and DisplayTexts returns the text of a cell by the cell’s index (the index is zero-based).

    • Before saving the cell text to the file, we pass this text to the ConvertSpecialChars function. It replaces special characters (&, <, > and others) with the appropriate constants (&amp;, &lt;, &gt; and so on) and then returns the resultant string. The replacement is needed since the mentioned characters are special characters and they can only be stored in an XML file in the form of these constants.

    • Then the routine processes the row’s child data (if any). To determine if the row has child data, we check if the row object contains the DetailGridViewCount property (to perform this check, we call the BuiltIn.IsSupported method). If the row has child data, the row object will contain the DetailGridViewCount property.

    • Then, the routine processes child views in a loop. To get the child view, we use the DetailGridViews property of the row object. It returns the desired child view by its index (zero-based). To get the total number of child views, we use the DetailGridViewCount property.

      To process child view data, the routine calls itself recursively.


    • Finally, the routine closes the row tag.

  • ConvertSpecialChars - This is a helper routine. It replaces special characters in a string (&, <, > and others) with their appropriate constants (&amp;, &lt;, &gt; and so on) and then returns the resulting string.

Here is the sample code:


Close

Add a little SmartBear to your life

Stay on top of your Software game with the latest developer tips, best practices and news, delivered straight to your inbox

By submitting this form, you agree to our
Terms of Use and Privacy Policy

Thanks for Subscribing

Keep an eye on your inbox for more great content.

Continue Reading