Iterating through grid rows means accessing rows is a series, one by one. You may need to iterate through GridDataBoundGrid rows if you need to locate a particular row in the grid, or perform the same set of operations on each row.
You can iterate through the grid rows by organizing a loop. Since the numeration of grid rows and columns is zero-based, the index of the first grid row (column) is 0, and the index of the last row (column) is returned by the Model.RowCount
(Model.ColCount
) property. On each loop iteration, you can perform the desired actions over the current row. For example, you can obtain or modify cell values.
Note that the first rows and columns represent grid headers and do not contain actual data. To determine the indexes of the topmost row and leftmost column that contain data rows, you can use the grid’s GridCellsRange.Top
and GridCellsRange.Left
properties. The GridCellsRange.Bottom
and GridCellsRange.Right
properties return the indexes of the last data row and column. For more information on addressing GridDataBoundGrid rows and columns, see Accessing Rows, Columns and Cells in Syncfusion GridDataBoundGrid.
Note: | In order for TestComplete to access these properties, the .NET Application Support plugin must be installed and enabled. |
The following example demonstrates how to process GridDataBoundGrid rows in a loop. It iterates through the grid rows, saves the data to an XML file and adds a link to this file in the test log.
Example
JavaScript
function Main ()
{
var p, Grid, FileName;
// Obtain the application process and the grid object
p = Sys.Process("GDBGMultiHeader");
Grid = p.WinFormsObject("Form1").WinFormsObject("gridRecordNavigationControl1").WinFormsObject("gridDataBoundGrid1");
// Save the grid data to an XML file
FileName = "C:\\GridData.xml";
ExportToXml (Grid, FileName);
Log.File (FileName, "Exported grid data.");
}
function ExportToXml (Grid, FileName)
{
Indicator.PushText ("Exporting the grid data to a file...");
// Create a text file and open it for writing
let Overwrite = true;
let Unicode = true;
let fso = getActiveXObject("Scripting.FileSystemObject");
let XmlFile = fso.CreateTextFile (FileName, Overwrite, Unicode);
// Export the grid data
ExportGridInternal (Grid, XmlFile);
// Close the file
XmlFile.Close();
Indicator.PopText();
}
function ExportGridInternal (Grid, XmlFile)
{
var StartDataRowIndex, EndDataRowIndex, EndColIndex;
// Write the root tag
XmlFile.WriteLine ("<?xml version=\"1.0\"?>");
XmlFile.WriteLine ("<grid>");
// Determine the data rows and columns range
StartDataRowIndex = Grid.GridCellsRange.Top;
EndDataRowIndex = Grid.GridCellsRange.Bottom;
EndColIndex = Grid.GridCellsRange.Right;
// Export column headers
XmlFile.WriteLine (" <colheaders>");
for (let i=0; i<StartDataRowIndex; i++)
{
XmlFile.WriteLine (" <row>");
for (let j=0; j<=EndColIndex; j++)
XmlFile.WriteLine (" " + GetCellXmlInternal (Grid.Item(i, j), "header"));
XmlFile.WriteLine (" </row>");
}
XmlFile.WriteLine (" </colheaders>");
// Iterate through data rows and export their data
XmlFile.WriteLine (" <rows>");
for (let i=StartDataRowIndex; i<=EndRowIndex; i++)
{
// Write the opening tag
XmlFile.WriteLine (" <row>");
// Export cells text
for (let j=0; j<=EndColIndex; j++)
XmlFile.WriteLine (" " + GetCellXmlInternal (Grid.Item(i, j), "cell"));
// Write the closing tag
XmlFile.WriteLine (" </row>");
}
XmlFile.WriteLine (" </rows>");
// Close the root tag
XmlFile.WriteLine ("</grid>");
}
function GetCellXmlInternal (Cell, TagName)
{
let CellType = Cell.CellType.OleValue;
let CellText = Cell.FormattedText.OleValue;
// Replace special characters in the cell text
CellText = GetValidXmlString (CellText);
return "<" + TagName + " type=\"" + CellType + "\">" + CellText + "</" + TagName + ">";
}
function GetValidXmlString (str)
{
let s;
s = aqString.Replace (str, "&", "&", true);
s = aqString.Replace (s, "<", "<", true);
s = aqString.Replace (s, ">", ">", true);
s = aqString.Replace (s, "\"", """, true);
s = aqString.Replace (s, "'", "'", true);
return s;
}
JScript
function Main ()
{
var p, Grid, FileName;
// Obtain the application process and the grid object
p = Sys.Process("GDBGMultiHeader");
Grid = p.WinFormsObject("Form1").WinFormsObject("gridRecordNavigationControl1").WinFormsObject("gridDataBoundGrid1");
// Save the grid data to an XML file
FileName = "C:\\GridData.xml";
ExportToXml (Grid, FileName);
Log.File (FileName, "Exported grid data.");
}
function ExportToXml (Grid, FileName)
{
Indicator.PushText ("Exporting the grid data to a file...");
// Create a text file and open it for writing
var Overwrite = true;
var Unicode = true;
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var XmlFile = fso.CreateTextFile (FileName, Overwrite, Unicode);
// Export the grid data
ExportGridInternal (Grid, XmlFile);
// Close the file
XmlFile.Close();
Indicator.PopText();
}
function ExportGridInternal (Grid, XmlFile)
{
var StartDataRowIndex, EndDataRowIndex, EndColIndex, i, j;
// Write the root tag
XmlFile.WriteLine ("<?xml version=\"1.0\"?>");
XmlFile.WriteLine ("<grid>");
// Determine the data rows and columns range
StartDataRowIndex = Grid.GridCellsRange.Top;
EndDataRowIndex = Grid.GridCellsRange.Bottom;
EndColIndex = Grid.GridCellsRange.Right;
// Export column headers
XmlFile.WriteLine (" <colheaders>");
for (i=0; i<StartDataRowIndex; i++)
{
XmlFile.WriteLine (" <row>");
for (j=0; j<=EndColIndex; j++)
XmlFile.WriteLine (" " + GetCellXmlInternal (Grid.Item(i, j), "header"));
XmlFile.WriteLine (" </row>");
}
XmlFile.WriteLine (" </colheaders>");
// Iterate through data rows and export their data
XmlFile.WriteLine (" <rows>");
for (i=StartDataRowIndex; i<=EndRowIndex; i++)
{
// Write the opening tag
XmlFile.WriteLine (" <row>");
// Export cells text
for (j=0; j<=EndColIndex; j++)
XmlFile.WriteLine (" " + GetCellXmlInternal (Grid.Item(i, j), "cell"));
// Write the closing tag
XmlFile.WriteLine (" </row>");
}
XmlFile.WriteLine (" </rows>");
// Close the root tag
XmlFile.WriteLine ("</grid>");
}
function GetCellXmlInternal (Cell, TagName)
{
var CellType = Cell.CellType.OleValue;
var CellText = Cell.FormattedText.OleValue;
// Replace special characters in the cell text
CellText = GetValidXmlString (CellText);
return "<" + TagName + " type=\"" + CellType + "\">" + CellText + "</" + TagName + ">";
}
function GetValidXmlString (str)
{
var s;
s = aqString.Replace (str, "&", "&", true);
s = aqString.Replace (s, "<", "<", true);
s = aqString.Replace (s, ">", ">", true);
s = aqString.Replace (s, "\"", """, true);
s = aqString.Replace (s, "'", "'", true);
return s;
}
Python
def Main ():
# Obtain the application process and the grid object
p = Sys.Process("GDBGMultiHeader")
Grid = p.WinFormsObject("Form1").WinFormsObject("gridRecordNavigationControl1").WinFormsObject("gridDataBoundGrid1")
# Save the grid data to an XML file
FileName = "C:\\GridData.xml"
ExportToXml (Grid, FileName)
Log.File (FileName, "Exported grid data.")
def ExportToXml (Grid, FileName):
Indicator.PushText ("Exporting the grid data to a file...")
# Create a text file and open it for writing
Overwrite = True
Unicode = True
fso = Sys.OleObject["Scripting.FileSystemObject"]
XmlFile = fso.CreateTextFile (FileName, Overwrite, Unicode)
# Export the grid data
ExportGridInternal (Grid, XmlFile)
# Close the file
XmlFile.Close()
Indicator.PopText()
def ExportGridInternal (Grid, XmlFile):
# Write the root tag
XmlFile.WriteLine ("<?xml version=\"1.0\"?>")
XmlFile.WriteLine ("<grid>")
# Determine the data rows and columns range
StartDataRowIndex = Grid.GridCellsRange.Top
EndDataRowIndex = Grid.GridCellsRange.Bottom
EndColIndex = Grid.GridCellsRange.Right
# Export column headers
XmlFile.WriteLine (" <colheaders>")
for i in range(0, StartDataRowIndex):
XmlFile.WriteLine (" <row>")
for j in range(0, EndColIndex):
XmlFile.WriteLine (" " + GetCellXmlInternal (Grid.Item(i, j), "header"))
XmlFile.WriteLine (" </row>")
XmlFile.WriteLine (" </colheaders>")
# Iterate through data rows and export their data
XmlFile.WriteLine (" <rows>")
for i in rfange(StartDataRowIndex, EndRowIndex):
# Write the opening tag
XmlFile.WriteLine (" <row>")
# Export cells text
for j in range(0, EndColIndex):
XmlFile.WriteLine (" " + GetCellXmlInternal (Grid.Item(i, j), "cell"))
# Write the closing tag
XmlFile.WriteLine (" </row>")
XmlFile.WriteLine (" </rows>")
# Close the root tag
XmlFile.WriteLine ("</grid>")
def GetCellXmlInternal (Cell, TagName):
CellType = Cell.CellType.OleValue
CellText = Cell.FormattedText.OleValue
# Replace special characters in the cell text
CellText = GetValidXmlString (CellText)
return "<" + TagName + " type=\"" + CellType + "\">" + CellText + "</" + TagName + ">"
def GetValidXmlString (str):
s = aqString.Replace (str, "&", "&", True)
s = aqString.Replace (s, "<", "<", True)
s = aqString.Replace (s, ">", ">", True)
s = aqString.Replace (s, "\"", """, True)
s = aqString.Replace (s, "'", "'", True)
return s
VBScript
Sub Main
Dim p, Grid, FileName
' Obtain the application process and the grid object
Set p = Sys.Process("GDBGMultiHeader")
Set Grid = p.WinFormsObject("Form1").WinFormsObject("gridRecordNavigationControl1").WinFormsObject("gridDataBoundGrid1")
' Save the grid data to an XML file
FileName = "C:\\GridData.xml"
Call ExportToXml (Grid, FileName)
Call Log.File (FileName, "Exported grid data.")
End Sub
Sub ExportToXml (Grid, FileName)
Dim fso, XmlFile, Overwrite, Unicode
Call Indicator.PushText ("Exporting the grid data to a file...")
' Create a text file and open it for writing
Overwrite = True
Unicode = True
Set fso = CreateObject ("Scripting.FileSystemObject")
Set XmlFile = fso.CreateTextFile (FileName, Overwrite, Unicode)
' Export the grid data
Call ExportGridInternal (Grid, XmlFile)
' Close the file
XmlFile.Close
Indicator.PopText
End Sub
Sub ExportGridInternal (Grid, XmlFile)
Dim StartDataRowIndex, EndDataRowIndex, EndColIndex, i, j
' Write the root tag
Call XmlFile.WriteLine ("<?xml version=""1.0""?>")
Call XmlFile.WriteLine ("<grid>")
' Determine the data rows and columns range
StartDataRowIndex = Grid.GridCellsRange.Top
EndDataRowIndex = Grid.GridCellsRange.Bottom
EndColIndex = Grid.GridCellsRange.Right
' Export column headers
XmlFile.WriteLine (" <colheaders>")
For i = 0 To StartDataRowIndex-1
XmlFile.WriteLine (" <row>")
For j = 0 To EndColIndex
Call XmlFile.WriteLine (" " & GetCellXmlInternal (Grid.Item(i, j), "header"))
Next
Call XmlFile.WriteLine (" </row>")
Next
Call XmlFile.WriteLine (" </colheaders>")
' Iterate through data rows and export their data
Call XmlFile.WriteLine (" <rows>")
For i = StartDataRowIndex To EndRowIndex
' Write the opening tag
Call XmlFile.WriteLine (" <row>")
' Export cells text
For j = 0 To EndColIndex
Call XmlFile.WriteLine (" " & GetCellXmlInternal (Grid.Item(i, j), "cell"))
Next
' Write the closing tag
Call XmlFile.WriteLine (" </row>")
Next
Call XmlFile.WriteLine (" </rows>")
' Close the root tag
Call XmlFile.WriteLine ("</grid>")
End Sub
Function GetCellXmlInternal (Cell, TagName)
Dim CellType, CellText
CellType = Cell.CellType.OleValue
CellText = Cell.FormattedText.OleValue
' Replace special characters in the cell text
CellText = GetValidXmlString (CellText)
GetCellXmlInternal = "<" & TagName & " type=""" & CellType & """>" & CellText & "</" & TagName & ">"
End Function
Function GetValidXmlString (str)
Dim s
s = aqString.Replace (str, "&", "&", True)
s = aqString.Replace (s, "<", "<", True)
s = aqString.Replace (s, ">", ">", True)
s = aqString.Replace (s, """", """, True)
s = aqString.Replace (s, "'", "&apos", True)
GetValidXmlString = s
End Function
DelphiScript
procedure ExportToXml (Grid, FileName); forward;
procedure ExportGridInternal (Grid, XmlFile); forward;
function GetCellXmlInternal (Cell, TagName); forward;
function GetValidXmlString (str); forward;
procedure Main;
var p, Grid, FileName : OleVariant;
begin
// Obtain the application process and the grid object
p := Sys.Process('GDBGMultiHeader');
Grid := p.WinFormsObject('Form1').WinFormsObject('gridRecordNavigationControl1').WinFormsObject('gridDataBoundGrid1');
// Save the grid data to an XML file
FileName := 'C:\GridData.xml';
ExportToXml (Grid, FileName);
Log.File (FileName, 'Exported grid data.');
end;
procedure ExportToXml (Grid, FileName);
var fso, XmlFile, Overwrite, Unicode : OleVariant;
begin
Indicator.PushText ('Exporting the grid data to a file...');
// Create a text file and open it for writing
Overwrite := true;
Unicode := true;
fso := Sys.OleObject['Scripting.FileSystemObject'];
XmlFile := fso.CreateTextFile (FileName, Overwrite, Unicode);
// Export the grid data
ExportGridInternal (Grid, XmlFile);
// Close the file
XmlFile.Close;
Indicator.PopText;
end;
procedure ExportGridInternal (Grid, XmlFile);
var StartDataRowIndex, EndDataRowIndex, EndColIndex, i, j : OleVariant;
begin
// Write the root tag
XmlFile.WriteLine ('<?xml version="1.0"?>');
XmlFile.WriteLine ('<grid>');
// Determine the data rows and columns range
StartDataRowIndex := Grid.GridCellsRange.Top;
EndDataRowIndex := Grid.GridCellsRange.Bottom;
EndColIndex := Grid.GridCellsRange.Right;
// Export column headers
XmlFile.WriteLine (' <colheaders>');
for i := 0 to StartDataRowIndex-1 do
begin
XmlFile.WriteLine (' <row>');
for j := 0 to EndColIndex do
XmlFile.WriteLine (' ' + GetCellXmlInternal(Grid.Item[i, j], 'header'));
XmlFile.WriteLine (' </row>');
end;
XmlFile.WriteLine (' </colheaders>');
// Iterate through data rows and export their data
XmlFile.WriteLine (' <rows>');
for i := StartDataRowIndex to EndRowIndex do
begin
// Write the opening tag
XmlFile.WriteLine (' <row>');
// Export cells text
for j := 0 to EndColIndex do
XmlFile.WriteLine (' ' + GetCellXmlInternal(Grid.Item[i, j], 'cell'));
// Write the closing tag
XmlFile.WriteLine (' </row>');
end;
XmlFile.WriteLine (' </rows>');
// Close the root tag
XmlFile.WriteLine ('</grid>');
end;
function GetCellXmlInternal (Cell, TagName);
var CellType, CellText : OleVariant;
begin
CellType := Cell.CellType.OleValue;
CellText := Cell.FormattedText.OleValue;
// Replace special characters in the cell text
CellText := GetValidXmlString (CellText);
Result := '<' + TagName + ' type="' + CellType + '">' + CellText + '</' + TagName + '>';
end;
function GetValidXmlString (str);
var s : OleVariant;
begin
s := aqString.Replace (str, '&', '&', true);
s := aqString.Replace (s, '<', '<', true);
s := aqString.Replace (s, '>', '>', true);
s := aqString.Replace (s, '"', '"', true);
s := aqString.Replace (s, '''', ''', true);
Result := s;
end;
C++Script, C#Script
function Main ()
{
var p, Grid, FileName;
// Obtain the application process and the grid object
p = Sys["Process"]("GDBGMultiHeader");
Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("gridRecordNavigationControl1")["WinFormsObject"]("gridDataBoundGrid1");
// Save the grid data to an XML file
FileName = "C:\\GridData.xml";
ExportToXml (Grid, FileName);
Log["File"](FileName, "Exported grid data.");
}
function ExportToXml (Grid, FileName)
{
Indicator.PushText ("Exporting the grid data to a file...");
// Create a text file and open it for writing
var Overwrite = true;
var Unicode = true;
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var XmlFile = fso.CreateTextFile (FileName, Overwrite, Unicode);
// Export the grid data
ExportGridInternal (Grid, XmlFile);
// Close the file
XmlFile["Close"]();
Indicator["PopText"]();
}
function ExportGridInternal (Grid, XmlFile)
{
var StartDataRowIndex, EndDataRowIndex, EndColIndex, i, j;
// Write the root tag
XmlFile["WriteLine"]("<?xml version=\"1.0\"?>");
XmlFile["WriteLine"]("<grid>");
// Determine the data rows and columns range
StartDataRowIndex = Grid["GridCellsRange"]["Top"];
EndDataRowIndex = Grid["GridCellsRange"]["Bottom"];
EndColIndex = Grid["GridCellsRange"]["Right"];
// Export column headers
XmlFile["WriteLine"](" <colheaders>");
for (i=0; i<StartDataRowIndex; i++)
{
XmlFile["WriteLine"](" <row>");
for (j=0; j<=EndColIndex; j++)
XmlFile["WriteLine"](" " + GetCellXmlInternal (Grid["Item"](i, j), "header"));
XmlFile["WriteLine"](" </row>");
}
XmlFile["WriteLine"](" </colheaders>");
// Iterate through data rows and export their data
XmlFile["WriteLine"](" <rows>");
for (i=StartDataRowIndex; i<=EndRowIndex; i++)
{
// Write the opening tag
XmlFile["WriteLine"](" <row>");
// Export cells text
for (j=0; j<=EndColIndex; j++)
XmlFile["WriteLine"](" " + GetCellXmlInternal (Grid["Item"](i, j), "cell"));
// Write the closing tag
XmlFile["WriteLine"](" </row>");
}
XmlFile["WriteLine"](" </rows>");
// Close the root tag
XmlFile["WriteLine"]("</grid>");
}
function GetCellXmlInternal (Cell, TagName)
{
var CellType = Cell["CellType"]["OleValue"];
var CellText = Cell["FormattedText"]["OleValue"];
// Replace special characters in the cell text
CellText = GetValidXmlString (CellText);
return "<" + TagName + " type=\"" + CellType + "\">" + CellText + "</" + TagName + ">";
}
function GetValidXmlString (str)
{
var s;
s = aqString["Replace"](str, "&", "&", true);
s = aqString["Replace"](s, "<", "<", true);
s = aqString["Replace"](s, ">", ">", true);
s = aqString["Replace"](s, "\"", """, true);
s = aqString["Replace"](s, "'", "'", true);
return s;
}
See Also
Working With Syncfusion GridDataBoundGrid
Accessing Rows, Columns and Cells in Syncfusion GridDataBoundGrid
Obtaining and Setting Cell Values in Syncfusion GridDataBoundGrid
Searching for Records in Syncfusion GridDataBoundGrid