Iterating Through Microsoft PropertyGrid Rows

Iterating through grid rows means accessing rows in a series, one by one. You may need to iterate through PropertyGrid rows if you need to locate a particular row, or perform the same set of operations on each row, or export the grid data to a file.

In this article, I will explain how you can iterate through PropertyGrid rows and, as an example, save the grid data to an XML file. Before you read this article, I recommend that you read the Accessing Microsoft PropertyGrid Rows article to get the basic notion about the ways to access grid rows.

You can obtain the collection of top-level grid rows using the grid’s GetPropEntries method. To obtain a particular row by its index within the collection, use the Item property or the get_Item method of the collection object. Since the numeration of grid rows is zero-based, the index of the first grid row is 0, and the index of the last row is Count – 1.

Since the PropertyGrid rows are organized in a hierarchy, you need to process rows recursively. That is, after processing a single row, you need to apply the same processing to its child rows. To get the child rows collection, use the row’s GridItems property. You can iterate through the obtained collection in the same way as described above.

The example below shows how to process PropertyGrid rows and save the grid data to an XML file. You can use the resulting file to validate the grid data during the next test run.

The example contains the following routines:

  • Main is the main routine. It obtains the scripting object corresponding to the PropertyGrid control and calls SaveToXml to save the grid data to the “C:GridData.xml” file.
  • SaveToXml exports the Grid control data to an XML file whose full path is specified by the FileName parameter. The routine iterates through the top-level grid rows in a loop, and saves the row data in an XML file. It uses the helper GetRowXML function to get the XML text for a particular grid rowi. The routine writes data to a file using methods of the FileSystemObject OLE object. You can find the FileSystemObject reference in the MSDN library at:

    http://msdn2.microsoft.com/en-gb/library/z9ty6h50.aspx

  • The GetRowXml function returns the XML text for the PropertyGrid row specified by the Row parameter. If a row is a category row, it saves the row label only, otherwise, if the row is a property row, it saves the row label as well as the property value. Then the function recursively calls itself to get the XML text for the Row’s child rows. The helper Indent parameter of this function specified the string used for text indentation.
  • The GetTextValue function returns the property value of a PropertyGrid row in a text format. If the property value is not specified (that is, it has the null value), the “(null)” string is returned.

    I have not found any way to get the text actually displayed in the cells, so I just cast the property values to the string type using the .NET ToString method  – in most cases it gives the same text. Note that .NET String objects are not OLE-compatible, and we can use these string values in scripts only via the special OleValue property added to these objects by TestComplete.

  • IsNullValue is a helper function that checks whether Value is the null value. If so, it returns True, otherwise False.