This topic explains how you can iterate through rows of the Infragistics UltraGrid controls and save the data, that is displayed by the control, to an .XML file.
Iterating through grid rows means accessing them one by one. You may need to iterate through rows if you need to locate a particular row, or perform the same set of operations on each row. When you locate the desired row, you can obtain values of the row’s cells and save them to a file.
You can determine the total number of data rows in a particular grid view using the wRowCount
property of the InfragisticsUltraGridView
object, or using the Rows.Count
property of an object corresponding to this view. 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 wRowCount
- 1. Therefore, to iterate through the rows, a loop counter should increment from 0 (the index of the first row) to wRowCount
- 1 (the index of the last row). Similarly, to iterate through cells within a row, the cell counter should cycle from 0 to wColumnCount
- 1.
Since UltraGrid can have several hierarchical data levels, it displays data form different levels via different data views. The rows of a root level view can have one or more child rows that are represented by child views. Child rows can be parents to some other rows and so on. This means that the grid rows should be processed recursively. That is, after processing a single row, you need to apply the same processing to its child rows.
To get the number of child views a particular row has, read the wChildViewCount
property. If a row does not have child views this property returns zero. To access a child data view use the wChildView
property. After obtaining a child view, you can process it the same way as the root level view. For more information on how you can access UltraGrid rows, see Accessing Grid Elements in Infragistics UltraGrid.
Note: | In order for TestComplete to access the properties and methods mentioned, the .NET Application Support plugin must be installed and enabled. |
The following example demonstrates how to process UltraGrid rows. It iterates through the grid rows in a loop, saves the data to the XML file and adds a link to this file in the test log.
Example
JavaScript
function Main()
{
var FileName = "c:\\MyFile.xml";
// Obtain the application process
p = Sys.Process("SamplesExplorer");
// Select the "Fixed Headers" demo
p.frmMain.WinFormsObject("tvwSamples").DblClickItem("|Feature Samples|V3 Fixed Headers");
// Obtain the grid control
Grid = Sys.Process("SamplesExplorer").WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1");
// Save grid data to a file
ExportGridDataToXml(Grid, FileName);
// Post a file to log
Log.File (FileName, "Exported grid data.")
}
// Export the grid's data and write it to file
function ExportGridDataToXml(Grid, FileName)
{
// Creating file
var fso = getActiveXObject("Scripting.FileSystemObject");
var XmlFile = fso.CreateTextFile(FileName, true, true);
// Export grid's root data view and all child views (if any)
ExportView(XmlFile, Grid, "", "");
// Close file
XmlFile.Close();
}
// Export data from a specified data view
function ExportView(XmlFile, View, ViewName, InitIndent)
{
// Check whether a data view is a root view or a child view
if (ViewName != "")
// Write the child view tag
XmlFile.WriteLine(InitIndent + "<ChildView ViewName=\"" + ViewName + "\">");
else
// Write the root tag
XmlFile.WriteLine(InitIndent + "<Grid>");
// Export columns information
// Write the columns tag
XmlFile.WriteLine(InitIndent + "<columns>");
// Iterate through columns
for (var i = 0; i < View.wColumnCount; i++)
{
// Get the columns caption
var str = View.wColumn(i);
// Replace XML specific characters
str = ConvertSpecialChars(str);
// Write column information
XmlFile.WriteLine(InitIndent + " <column>" +str + "</column>");
}
// Close the columns tag
XmlFile.WriteLine(InitIndent + "</columns>");
// Export data
// Iterate through rows
for (var i = 0; i < View.wRowCount; i++)
{
// Write the row tag
XmlFile.WriteLine(InitIndent + "<row>");
// Export row data including its child rows
ExportRow(XmlFile, View, i, InitIndent + " ");
// Close the row tag
XmlFile.WriteLine(InitIndent + "</row>");
}
if (ViewName != "")
// Close the child view tag
XmlFile.WriteLine(InitIndent + "</ChildView>");
else
// Close the root tag
XmlFile.WriteLine(InitIndent + "</Grid>");
}
// Export the specified row and all its children
function ExportRow(XmlFile, View, RowIndex, InitIndent)
{
// Export data from the row cells
// Iterate through cells
for (var i = 0; i < View.wColumnCount; i++)
{
// Get the cell value
str = View.wValue(RowIndex, i);
// Replace XML specific characters
str = ConvertSpecialChars(str);
// Write cell data
XmlFile.WriteLine(InitIndent + "<cell>" + str + "</cell>");
}
// If a row is a parent for some views
if (View.wChildViewCount(RowIndex)>0)
// Iterate through child views
for (var i = 0; i < View.wChildViewCount(RowIndex); i++)
// Export the child view
ExportView(XmlFile, View.wChildView(RowIndex,i), "Row Index: " + RowIndex + " ChildView Index: " + i, InitIndent + " ");
}
// Replace special characters
function ConvertSpecialChars(AString)
{
var s;
try
{
// Search and replace special characters
s = aqString.Replace(AString, "&", "&", true);
s = aqString.Replace(s, "<", "<", true);
s = aqString.Replace(s, ">", ">", true);
s = aqString.Replace(s, "\"", """, true);
s = aqString.Replace(s, "'", "'", true);
}
catch(err)
{
s = ""
}
// Return the result
return s;
}
JScript
function Main()
{
var FileName = "c:\\MyFile.xml";
// Obtain the application process
p = Sys.Process("SamplesExplorer");
// Select the "Fixed Headers" demo
p.frmMain.WinFormsObject("tvwSamples").DblClickItem("|Feature Samples|V3 Fixed Headers");
// Obtain the grid control
Grid = Sys.Process("SamplesExplorer").WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1");
// Save grid data to a file
ExportGridDataToXml(Grid, FileName);
// Post a file to log
Log.File (FileName, "Exported grid data.")
}
// Export the grid's data and write it to file
function ExportGridDataToXml(Grid, FileName)
{
// Creating file
var fso = Sys.OleObject("Scripting.FileSystemObject");
var XmlFile = fso.CreateTextFile(FileName, true, true);
// Export grid's root data view and all child views (if any)
ExportView(XmlFile, Grid, "", "");
// Close file
XmlFile.Close();
}
// Export data from a specified data view
function ExportView(XmlFile, View, ViewName, InitIndent)
{
// Check whether a data view is a root view or a child view
if (ViewName != "")
// Write the child view tag
XmlFile.WriteLine(InitIndent + "<ChildView ViewName=\"" + ViewName + "\">");
else
// Write the root tag
XmlFile.WriteLine(InitIndent + "<Grid>");
// Export columns information
// Write the columns tag
XmlFile.WriteLine(InitIndent + "<columns>");
// Iterate through columns
for (var i = 0; i < View.wColumnCount; i++)
{
// Get the columns caption
var str = View.wColumn(i);
// Replace XML specific characters
str = ConvertSpecialChars(str);
// Write column information
XmlFile.WriteLine(InitIndent + " <column>" +str + "</column>");
}
// Close the columns tag
XmlFile.WriteLine(InitIndent + "</columns>");
// Export data
// Iterate through rows
for (var i = 0; i < View.wRowCount; i++)
{
// Write the row tag
XmlFile.WriteLine(InitIndent + "<row>");
// Export row data including its child rows
ExportRow(XmlFile, View, i, InitIndent + " ");
// Close the row tag
XmlFile.WriteLine(InitIndent + "</row>");
}
if (ViewName != "")
// Close the child view tag
XmlFile.WriteLine(InitIndent + "</ChildView>");
else
// Close the root tag
XmlFile.WriteLine(InitIndent + "</Grid>");
}
// Export the specified row and all its children
function ExportRow(XmlFile, View, RowIndex, InitIndent)
{
// Export data from the row cells
// Iterate through cells
for (var i = 0; i < View.wColumnCount; i++)
{
// Get the cell value
str = View.wValue(RowIndex, i);
// Replace XML specific characters
str = ConvertSpecialChars(str);
// Write cell data
XmlFile.WriteLine(InitIndent + "<cell>" + str + "</cell>");
}
// If a row is a parent for some views
if (View.wChildViewCount(RowIndex)>0)
// Iterate through child views
for (var i = 0; i < View.wChildViewCount(RowIndex); i++)
// Export the child view
ExportView(XmlFile, View.wChildView(RowIndex,i), "Row Index: " + RowIndex + " ChildView Index: " + i, InitIndent + " ");
}
// Replace special characters
function ConvertSpecialChars(AString)
{
var s;
try
{
// Search and replace special characters
s = aqString.Replace(AString, "&", "&", true);
s = aqString.Replace(s, "<", "<", true);
s = aqString.Replace(s, ">", ">", true);
s = aqString.Replace(s, "\"", """, true);
s = aqString.Replace(s, "'", "'", true);
}
catch(err)
{
s = ""
}
// Return the result
return s;
}
Python
def Main():
# Obtain the application process
p = Sys.Process("SamplesExplorer")
# Select the "Fixed Headers" demo
p.frmMain.WinFormsObject("tvwSamples").DblClickItem("|Feature Samples|V3 Fixed Headers")
# Obtain the grid control
Grid = Sys.Process("SamplesExplorer").WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1")
# Save grid data to a file
ExportGridDataToXml(Grid, FileName)
# Post a file to log
Log.File (FileName, "Exported grid data.")
# Export the grid's data and write it to file
def ExportGridDataToXml(Grid, FileName):
# Create file
fso = Sys.OleObject["Scripting.FileSystemObject"]
XmlFile = fso.CreateTextFile(FileName, True, True)
# Export grid's root data view and all child views (if any)
ExportView(XmlFile, Grid, "", "")
# Close file
XmlFile.Close()
# Export data from a specified data view
def ExportView(XmlFile, View, ViewName, InitIndent):
# Check whether a data view is a root view or a child view
if (ViewName != ""):
# Write the child view tag
XmlFile.WriteLine(InitIndent + "<ChildView ViewName=\"" + ViewName + "\">")
else:
# Write the root tag
XmlFile.WriteLine(InitIndent + "<Grid>")
# Export columns information
# Write the columns tag
XmlFile.WriteLine(InitIndent + "<columns>")
# Iterate through columns
for i in range(0, View.wColumnCount-1):
# Get the columns caption
str = View.wColumn[i]
# Replace XML specific characters
str = ConvertSpecialChars(str)
# Write column information
XmlFile.WriteLine(InitIndent + " <column>" +str + "</column>")
# Close the columns tag
XmlFile.WriteLine(InitIndent + "</columns>")
# Export data
# Iterate through rows
for i in range(0, View.wRowCount-1):
# Write the row tag
XmlFile.WriteLine(InitIndent + "<row>")
# Export row data including its child rows
ExportRow(XmlFile, View, i, InitIndent + " ")
# Close the row tag
XmlFile.WriteLine(InitIndent + "</row>")
if (ViewName != ""):
# Close the child view tag
XmlFile.WriteLine(InitIndent + "</ChildView>")
else:
# Close the root tag
XmlFile.WriteLine(InitIndent + "</Grid>")
# Export the specified row and all its children
def ExportRow(XmlFile, View, RowIndex, InitIndent):
# Export data from the row cells
# Iterate through cells
for i in range(0, View.wColumnCount-1):
# Get the cell value
str = View.wValue[RowIndex, i]
# Replace XML specific characters
str = ConvertSpecialChars(str)
# Write cell data
XmlFile.WriteLine(InitIndent + "<cell>" + str + "</cell>")
# If a row is a parent for some views
if (View.wChildViewCount[RowIndex]>0):
# Iterate through child views
for i in range(0, View.wChildViewCount[RowIndex]-1):
# Export the child view
ExportView(XmlFile, View.wChildView[RowIndex,i], "Row Index: " + RowIndex + " ChildView Index: " + i, InitIndent + " ")
# Replace special characters
def ConvertSpecialChars(AString):
try:
# Search and replace special characters
s = aqString.Replace(AString, "&", "&", True)
s = aqString.Replace(s, "<", "<", True)
s = aqString.Replace(s, ">", ">", True)
s = aqString.Replace(s, "\"", """, True)
s = aqString.Replace(s, "'", "'", True)
except err:
s = ""
# Return the result
return s
VBScript
Sub Main
FileName = "c:\MyFile.xml"
' Obtain the application process
Set p = Sys.Process("SamplesExplorer")
' Select the "Fixed Headers" demo
Call p.frmMain.WinFormsObject("tvwSamples").DblClickItem("|Feature Samples|V3 Fixed Headers")
' Obtain the grid control
Set Grid = Sys.Process("SamplesExplorer").WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1")
' Save grid data to a file
Call ExportGridDataToXml(Grid, FileName)
' Post a file to log
Call Log.File (FileName, "Exported grid data.")
End Sub
' Export the grid's data and write it to file
Sub ExportGridDataToXml(Grid, FileName)
' Create file
Set fso = Sys.OleObject("Scripting.FileSystemObject")
Set XmlFile = fso.CreateTextFile(FileName, True, True)
' Export grid's root data view and all child views (if any)
Call ExportView(XmlFile, Grid, "", "")
' Close file
XmlFile.Close
End Sub
' Export data from a specified data view
Sub ExportView(XmlFile, View, ViewName, InitIndent)
' Check whether a data view is a root view or a child view
If ViewName <> "" Then
' Write the child view tag
XmlFile.WriteLine(InitIndent & "<ChildView ViewName=""" & ViewName & """>")
Else
' Write the root tag
XmlFile.WriteLine(InitIndent & "<Grid>")
End If
' Export columns information
' Write the columns tag
XmlFile.WriteLine(InitIndent & "<columns>")
' Iterate through columns
For i = 0 To View.wColumnCount-1
' Get the columns caption
str = View.wColumn(i)
' Replace XML specific characters
str = ConvertSpecialChars(str)
' Write column information
XmlFile.WriteLine(InitIndent & " <column>" & str & "</column>")
Next
' Close the columns tag
XmlFile.WriteLine(InitIndent & "</columns>")
' Export data
' Iterate through rows
For i = 0 To View.wRowCount-1
' Write the row tag
XmlFile.WriteLine(InitIndent & "<row>")
' Export row data including its child rows
Call ExportRow(XmlFile, View, i, InitIndent & " ")
' Close the row tag
XmlFile.WriteLine(InitIndent & "</row>")
Next
If ViewName <> "" Then
' Close the child view tag
XmlFile.WriteLine(InitIndent & "</ChildView>")
Else
' Close the root tag
XmlFile.WriteLine(InitIndent & "</Grid>")
End If
End Sub
' Export the specified row and all its children
Sub ExportRow(XmlFile, View, RowIndex, InitIndent)
' Export data from the row cells
' Iterate through cells
For i = 0 To View.wColumnCount-1
' Attempt to obtain the cell's value
Err.Clear
On Error Resume Next
' Get the cell value
str = View.wValue(RowIndex, i)
' On failure assume that the cell value is empty
If Err.Number <> 0 Then str = "" End If
' Replace XML specific characters
str = ConvertSpecialChars(str)
' Write cell data
XmlFile.WriteLine(InitIndent & "<cell>" & str & "</cell>")
Next
' If a row is a parent for some views
If View.wChildViewCount(RowIndex)>0 Then
' Iterate through child views
For i = 0 To View.wChildViewCount(RowIndex) - 1
' Export the child view
Call ExportView(XmlFile, View.wChildView(RowIndex,i), "Row Index: " & RowIndex & " ChildView Index: " & i, InitIndent & " ")
Next
End If
End Sub
' Replace special characters
Function ConvertSpecialChars(AString)
' Search and replace special characters
s = aqString.Replace(AString, "&", "&", True)
s = aqString.Replace(s, "<", "<", True)
s = aqString.Replace(s, ">", ">", True)
s = aqString.Replace(s, """", """, True)
s = aqString.Replace(s, "'", "'", True)
' Return the result
ConvertSpecialChars = s
End Function
DelphiScript
procedure ExportGridDataToXml(Grid, FileName); forward;
procedure ExportView(XmlFile, View, ViewName, InitIndent); forward;
procedure ExportRow(XmlFile, View, RowIndex, InitIndent); forward;
function ConvertSpecialChars(AString) : OleVariant; forward;
function Main;
var p, Grid: Variant;
FileName: string;
begin
FileName := 'c:\MyFile.xml';
// Obtain the application process
p := Sys.Process('SamplesExplorer');
// Select the "Fixed Headers" demo
p.frmMain.WinFormsObject('tvwSamples').DblClickItem('|Feature Samples|V3 Fixed Headers');
// Obtain the grid control
Grid := Sys.Process('SamplesExplorer').WinFormsObject('frmFixedHeaders').WinFormsObject('UltraGrid1');
// Save grid data to a file
ExportGridDataToXml(Grid, FileName);
// Post a file to log
Log.File (FileName, 'Exported grid data.')
end;
// Export the grid's data and write it to file
procedure ExportGridDataToXml(Grid, FileName);
var
XmlFile,
fso: Variant;
begin
// Create file
fso := Sys.OleObject['Scripting.FileSystemObject', ''];
XmlFile := fso.CreateTextFile(FileName, true, true);
// Export grid's root data view and all child views (if any)
ExportView(XmlFile, Grid, '', '');
// Close file
XmlFile.Close();
end;
// Export data from a specified data view
procedure ExportView(XmlFile, View, ViewName, InitIndent);
var
i: integer;
str: string;
begin
// Check whether a data view is a root view or a child view
if ViewName <> '' then
// Write the child view tag
XmlFile.WriteLine(InitIndent + '<ChildView ViewName="' + ViewName + '">')
else
// Write the root tag
XmlFile.WriteLine(InitIndent + '<Grid>');
// Export columns information
// Write the columns tag
XmlFile.WriteLine(InitIndent + '<columns>');
// Iterate through columns
for i := 0 to View.wColumnCount-1 do
begin
// Get the columns caption
str := View.wColumn(i);
// Replace XML specific characters
str := ConvertSpecialChars(str);
// Write column information
XmlFile.WriteLine(InitIndent + ' <column>' +str + '</column>');
end;
// Close the columns tag
XmlFile.WriteLine(InitIndent + '</columns>');
// Export data
// Iterate through rows
for i := 0 to View.wRowCount-1 do
begin
// Write the row tag
XmlFile.WriteLine(InitIndent + '<row>');
// Export row data including its child rows
ExportRow(XmlFile, View, i, InitIndent + ' ');
// Close the row tag
XmlFile.WriteLine(InitIndent + '</row>');
end;
if ViewName <> '' then
// Close the child band tag
XmlFile.WriteLine(InitIndent + '</ChildView>')
else
// Close the root tag
XmlFile.WriteLine(InitIndent + '</Grid>');
end;
// Export the specified row and all its children
procedure ExportRow(XmlFile, View, RowIndex, InitIndent);
var
str: string;
i: integer;
begin
// Export data from the row cells
// Iterate through cells
for i := 0 to View.wColumnCount-1 do
begin
// Get the cell value
str := View.wValue(RowIndex, i);
// Replace XML specific characters
str := ConvertSpecialChars(str);
// Write cell data
XmlFile.WriteLine(InitIndent + '<cell>' + str + '</cell>');
end;
// If a row is a parent for some views
if View.wChildViewCount(RowIndex)>0 then
// Iterate through child views
for i := 0 to View.wChildViewCount(RowIndex) - 1 do
// Export the child view
ExportView(XmlFile, View.wChildView(RowIndex,i), 'Row Index: ' + aqConvert.IntToStr(RowIndex) + ' ChildView Index: ' + aqConvert.IntToStr(i), InitIndent +' ');
end;
// Replace special characters
function ConvertSpecialChars(AString) : OleVariant;
var
s : OleVariant;
begin
try
// Search and replace special characters
s := aqString.Replace(AString, '&', '&', true);
s := aqString.Replace(s, '<', '<', true);
s := aqString.Replace(s, '>', '>', true);
s := aqString.Replace(s, '"', '"', true);
s := aqString.Replace(s, '''', ''', true);
except
s:= '';
end;
// Return the result
Result := s;
end;
C++Script, C#Script
function Main()
{
var FileName = "c:\\MyFile.xml";
// Obtain the application process
p = Sys["Process"]("SamplesExplorer");
// Select the "Fixed Headers" demo
p["frmMain"]["WinFormsObject"]("tvwSamples")["DblClickItem"]("|Feature Samples|V3 Fixed Headers");
// Obtain the grid control
Grid = Sys["Process"]("SamplesExplorer")["WinFormsObject"]("frmFixedHeaders")["WinFormsObject"]("UltraGrid1");
// Save grid data to a file
ExportGridDataToXml(Grid, FileName);
// Post a file to log
Log["File"] (FileName, "Exported grid data.")
}
// Export the grid's data and write it to file
function ExportGridDataToXml(Grid, FileName)
{
// Create file
var fso = Sys["OleObject"]("Scripting.FileSystemObject");
var XmlFile = fso["CreateTextFile"](FileName, true, true);
// Export grid's root data view and all child views (if any)
ExportView(XmlFile, Grid, "", "");
// Close file
XmlFile["Close"]();
}
// Export data from a specified data view
function ExportView(XmlFile, View, ViewName, InitIndent)
{
// Check whether a data view is a root view or a child view
if (ViewName != "")
// Write the child view tag
XmlFile["WriteLine"](InitIndent + "<ChildView ViewName=\"" + ViewName + "\">");
else
// Write the root tag
XmlFile["WriteLine"](InitIndent + "<Grid>");
// Export columns information
// Write the columns tag
XmlFile["WriteLine"](InitIndent + "<columns>");
// Iterate through columns
for (var i = 0; i < View["wColumnCount"]; i++)
{
// Get the columns caption
var str = View["wColumn"](i);
// Replace XML specific characters
str = ConvertSpecialChars(str);
// Write column information
XmlFile["WriteLine"](InitIndent + " <column>" +str + "</column>");
}
// Close the columns tag
XmlFile["WriteLine"](InitIndent + "</columns>");
// Export data
// Iterate through rows
for (var i = 0; i < View["wRowCount"]; i++)
{
// Write the row tag
XmlFile["WriteLine"](InitIndent + "<row>");
// Export row data including its child rows
ExportRow(XmlFile, View, i, InitIndent + " ");
// Close the row tag
XmlFile["WriteLine"](InitIndent + "</row>");
}
if (ViewName != "")
// Close the child view tag
XmlFile["WriteLine"](InitIndent + "</ChildView>");
else
// Close the root tag
XmlFile["WriteLine"](InitIndent + "</Grid>");
}
// Export the specified row and all its children
function ExportRow(XmlFile, View, RowIndex, InitIndent)
{
// Export data from the row cells
// Iterate through cells
for (var i = 0; i < View["wColumnCount"]; i++)
{
// Get the cell value
str = View["wValue"](RowIndex, i);
// Replace XML specific characters
str = ConvertSpecialChars(str);
// Write cell data
XmlFile["WriteLine"](InitIndent + "<cell>" + str + "</cell>");
}
// If a row is a parent for some views
if (View["wChildViewCount"](RowIndex)>0)
// Iterate through child views
for (var i = 0; i < View["wChildViewCount"](RowIndex); i++)
// Export the child view
ExportView(XmlFile, View["wChildView"](RowIndex,i), "Row Index: " + RowIndex + " ChildView Index: " + i, InitIndent + " ");
}
// Replace special characters
function ConvertSpecialChars(AString)
{
var s;
try
{
// Search and replace special characters
s = aqString["Replace"](AString, "&", "&", true);
s = aqString["Replace"](s, "<", "<", true);
s = aqString["Replace"](s, ">", ">", true);
s = aqString["Replace"](s, "\"", """, true);
s = aqString["Replace"](s, "'", "'", true);
}
catch(err)
{
s = ""
}
// Return the result
return s;
}
See Also
Working With Infragistics UltraGrid
Accessing Grid Elements in Infragistics UltraGrid
Searching for Records in Infragistics UltraGrid
wRowCount Property (Grid Controls)
wColumnCount Property (Grid Controls)
wChildViewCount Property (Grid Controls)