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:
Mainis the main routine. It obtains the scripting object corresponding to the PropertyGrid control and calls
SaveToXmlto save the grid data to the “C:GridData.xml” file.
SaveToXmlexports 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
GetRowXMLfunction to get the XML text for a particular grid rowi. The routine writes data to a file using methods of the
FileSystemObjectOLE object. You can find the
FileSystemObjectreference in the MSDN library at:
GetRowXmlfunction 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.
GetTextValuefunction 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
ToStringmethod – in most cases it gives the same text. Note that .NET
Stringobjects are not OLE-compatible, and we can use these string values in scripts only via the special
OleValueproperty added to these objects by TestComplete.
IsNullValueis a helper function that checks whether Value is the null value. If so, it returns True, otherwise False.
Dim p, Grid
' Obtain the grid object
Set p = Sys.Process (“PropertyGridSample”)
Set Grid = p.WinFormsObject(“Form1″).WinFormsObject(“propertyGrid1″)
' Save grid data to the XML file
Call SaveToXML (Grid, “C:GridData.xml”)
' Saves the grid data to the XML file
Sub SaveToXML (Grid, FileName)
Const Overwrite = True
Const Unicode = True
Dim FSO, XmlFile, Rows, i
' Create file and open it for writing
Set FSO = CreateObject (“Scripting.FileSystemObject”)
Set XmlFile = FSO.CreateTextFile (FileName, Overwrite, Unicode)
' Write the root tag (Grid)
Call XmlFile.WriteLine (“<Grid>”)
' Save the grid rows data to the file
Set Rows = Grid.GetPropEntries
For i=0 to Rows.Count-1
Call XmlFile.Write (GetRowXML(Rows.Item(i), ” “))
' Close the root tag (Grid)
Call XmlFile.WriteLine (“</Grid>”)
' Close the file
' Returns the XML string for the grid row
Function GetRowXML (Row, Indent)
Dim TagName, IsCategoryRow, XMlStr, ChildRows, i
' Determine whether the row is the category row
TagName = Row.GridItemType.OleValue
IsCategoryRow = (TagName = “Category”)
' Open the row tag (Category or Property)
XmlStr = Indent & “<” & TagName & “>” & vbCrLf
' Process the row data (name and value)
XmlStr = XmlStr & Indent & ” <Name>” & Row.Label.OleValue & “</Name>” & vbCrLf
If Not IsCategoryRow Then
XmlStr = XmlStr & Indent & ” <Value>” & GetTextValue (Row) & “</Value>” & vbCrLf
' Process child rows
Set ChildRows = Row.GridItems
If (Not (ChildRows Is Nothing)) And (ChildRows.Count > 0) Then
' Open the child rows tag (Properties)
XmlStr = XmlStr & Indent & ” <Properties>” & vbCrLf
For i=0 To ChildRows.Count-1
XmlStr = XmlStr & GetRowXML (ChildRows.Item(i), Indent & ” “)
' Close the child rows tag (Properties)
XmlStr = XmlStr & Indent & ” </Properties>” & vbCrLf
' Close the row tag (Category or Property)
XmlStr = XmlStr & Indent & “</” & TagName & “>” & vbCrLf
GetRowXML = XmlStr
' Returns an OLE-compatible string value of a .NET object
Function GetTextValue (Row)
If IsNullValue (Row.Value) Then
GetTextValue = “(none)” ' String that represents the null value
GetTextValue = Row.Value.ToString.OleValue
' Checks whether Value is the null value
Function IsNullValue2 (Value)
If VarType (Value) = varDispatch Then
IsNullValue = Value Is Nothing
IsNullValue = IsNull (Value) Or IsEmpty (Value)