Iterating Through Rows in Developer Express XtraGrid

Iterating through grid rows means accessing rows in a series, one by one. You may need to iterate through grid rows if you need to locate a particular row, or perform the same set of operations on each row. This article explains how you can use TestComplete to iterate through rows in the Developer Express XtraGrid controls and save the data displayed by the grid to an .XML file.

Iterating Through Data Rows and Cards

You can determine the total number of data rows (cards) in a particular grid view using its DataRowCount property. To iterate through the rows (cards), you need to organize a loop whose counter changes from 0 (index of the first data row/card) to the DataRowCount-1 (which is index of the last row/card). On each loop iteration, you can perform the needed actions with the current row (card). For example, you can modify values in its cells or save them to a file. To learn how you can obtain and modify grid data, see Getting Cell Values in Developer Express XtraGrid and Modifying Cell Values in Developer Express XtraGrid.

Processing Child Views

If the XtraGrid control displays data of multiple nested tables, then each row in a parent table can have child data. So, besides processing the row data itself, you may need to iterate through its child data as well. Child views are created at run-time, when their parent rows are expanded. That is, to access a detail view for a particular master row, you need to expand it first. (I explained how to do this in the Expanding and Collapsing Rows in Developer Express XtraGrid article.)

The total number of child views for a particular row can be determined using the view’s GetRelationCount (RowIndex) property. To get a child view by its index, use the view’s GetDetailView (RowIndex, ChildViewIndex) method. After you obtain an object corresponding to a child view, you can

also iterate through its rows and process them as described above.

Example

Below is an example that demonstrates how you can iterate through grid records and save the grid data, including column (card field) captions, to an XML file. The example works with the GridMainDemo sample application that is shipped with Developer Express XtraGrid Suite. The script works equally well for table and card layouts. It processes rows in the root (top-level) table as well as in child tables. However, in thisexample we do not process group rows. You can find an example that saves grid data along with information on groups to a file in the “Iterating Through Rows in Developer Express XtraGrid” topic of TestComplete’s help
system.

The script contains the following routines:

  • Main is the main routine. It accesses the GridMainDemo process and its main form, selects the “Master-Detail” link in the navigation bar, calls ExportToXml to save the grid data to the C:GridData.xml file and posts the link for this file to the test log.
  • ExportToXml -- Saves the grid view data to an XML file whose full path is specified by the FileName parameter. The Grid parameter holds the tested XtraGrid control, and View is the grid view to be exported. If the View parameter has the null value (Nothing in VBScript), the entire grid is exported.

    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

    Note that the ExportView routine call is enclosed in BeginUpdate and EndUpdate methods of the grid control. This is necessary because accessing chils views in hierarchical grid controls requires expanding the corresponding parent rows. Using the BeginUpdate and EndUpdate methods prevents the grid from repainting until the processing is complete and thus speeds up performance.

The following ExportNNN routine uses similar parameters: View specifies the grid view in which the row resides; RowIndex is the zero-based indexof the row being processed; XmlFile holds the object used to write data to a file; indent is a helper parameter that specified the string used for text indentation.

  • ExportView -- Writes the data of the grid view specified by the View parameter to the XmlFile file. First, it saves column captions. Then it iterates through the view’s rows and exports data of each row using the ExportRow routine.
  • ExportRow -- Writes the data of the row specified by the RowIndex index to the XmlFile file. At that, it saves the actual formatted text displayed in cells instead of the cells’ values. After exporting the cells’ data, it iterates through the row’s child views and writes their data to a file. On each iteration, it accesses the corresponding child view in the following way: first saves the row’s expanded state to the Expanded variable, then expands the row in order to create the child view, calls ExportView to write the child view data to a file and finally restores the row’s expanded state.
  • ReplaceSpecialCharsis a helper function that replaces all occurrences of the special characters (&, <, > ", ') in the specified string with the corresponding XML entities (&amp;, &lt;, &gt; &quot;, &apos;), and returns the resulting string.


Close

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

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