Iterating Through Rows in Syncfusion GridControl

Applies to TestComplete 15.69, last modified on November 13, 2024

Iterating through grid rows means accessing rows is a series, one by one. You may need to iterate through GridControl 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 GridControl rows and columns, see Accessing Rows, Columns and Cells in Syncfusion GridControl.

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 GridControl 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

View description

JavaScript

function Main ()
{
  var p, Grid, FileName;

  // Obtain the application process and the grid object
  p = Sys.Process("GridControlSort");
  Grid = p.WinFormsObject("Form1").WinFormsObject("gridControl1");

  // 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, StartColIndex, 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;
  StartColIndex = Grid.GridCellsRange.Left;
  EndColIndex = Grid.GridCellsRange.Right;

  // Export column headers
  XmlFile.WriteLine ("  <colheaders>");
  for (let i=0; i<StartDataRowIndex; i++)
  {
    XmlFile.WriteLine ("    <row>");
    for (let j=StartColIndex; 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<=EndDataRowIndex; i++)
  {
    // Write the opening tag
    XmlFile.WriteLine ("    <row>");
    // Export cells text
    for (let j=StartColIndex; 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, "&", "&amp;", true);
  s = aqString.Replace (s, "<", "&lt;", true);
  s = aqString.Replace (s, ">", "&gt;", true);
  s = aqString.Replace (s, "\"", "&quot;", true);
  s = aqString.Replace (s, "'", "&apos;", true);
  return s;
}

JScript

function Main ()
{
  var p, Grid, FileName;

  // Obtain the application process and the grid object
  p = Sys.Process("GridControlSort");
  Grid = p.WinFormsObject("Form1").WinFormsObject("gridControl1");

  // 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, StartColIndex, 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;
  StartColIndex = Grid.GridCellsRange.Left;
  EndColIndex = Grid.GridCellsRange.Right;

  // Export column headers
  XmlFile.WriteLine ("  <colheaders>");
  for (i=0; i<StartDataRowIndex; i++)
  {
    XmlFile.WriteLine ("    <row>");
    for (j=StartColIndex; 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<=EndDataRowIndex; i++)
  {
    // Write the opening tag
    XmlFile.WriteLine ("    <row>");
    // Export cells text
    for (j=StartColIndex; 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, "&", "&amp;", true);
  s = aqString.Replace (s, "<", "&lt;", true);
  s = aqString.Replace (s, ">", "&gt;", true);
  s = aqString.Replace (s, "\"", "&quot;", true);
  s = aqString.Replace (s, "'", "&apos;", true);
  return s;
}

Python

def Main ():

  # Obtain the application process and the grid object
  p = Sys.Process("GridControlSort")
  Grid = p.WinFormsObject("Form1").WinFormsObject("gridControl1")

  # 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
  StartColIndex = Grid.GridCellsRange.Left
  EndColIndex = Grid.GridCellsRange.Right

  # Export column headers
  XmlFile.WriteLine ("  <colheaders>")
  for i in range(0, StartDataRowIndex-1):
    XmlFile.WriteLine ("    <row>")
    for j in range(StartColIndex, EndColIndex-1):
      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 range(StartDataRowIndex, EndDataRowIndex):
    # Write the opening tag
    XmlFile.WriteLine ("    <row>")
    # Export cells text
    for j in range(StartColIndex, 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, "&", "&amp;", True)
  s = aqString.Replace (s, "<", "&lt;", True)
  s = aqString.Replace (s, ">", "&gt;", True)
  s = aqString.Replace (s, "\"", "&quot;", True)
  s = aqString.Replace (s, "'", "&apos;", True)
  return s

VBScript

Sub Main
  Dim p, Grid, FileName

  ' Obtain the application process and the grid object
  Set p = Sys.Process("GridControlSort")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("gridControl1")

  ' 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, StartColIndex, 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
  StartColIndex = Grid.GridCellsRange.Left
  EndColIndex = Grid.GridCellsRange.Right

  ' Export column headers
  Call XmlFile.WriteLine ("  <colheaders>")
  For i = 0 To StartDataRowIndex-1
    Call XmlFile.WriteLine ("    <row>")
    For j = StartColIndex 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 EndDataRowIndex
    ' Write the opening tag
    Call XmlFile.WriteLine ("    <row>")
    ' Export cells text
    For j = StartColIndex 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, "&", "&amp", True)
  s = aqString.Replace (s, "<", "&lt", True)
  s = aqString.Replace (s, ">", "&gt", True)
  s = aqString.Replace (s, """", "&quot", 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('GridControlSort');
  Grid := p.WinFormsObject('Form1').WinFormsObject('gridControl1');

  // 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, StartColIndex, 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;
  StartColIndex := Grid.GridCellsRange.Left;
  EndColIndex := Grid.GridCellsRange.Right;

  // Export column headers
  XmlFile.WriteLine ('  <colheaders>');
  for i := 0 to StartDataRowIndex-1 do
  begin
    XmlFile.WriteLine ('    <row>');
    for j := StartColIndex 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 EndDataRowIndex do
  begin
    // Write the opening tag
    XmlFile.WriteLine ('    <row>');
    // Export cells text
    for j := StartColIndex 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, '&', '&amp;', true);
  s := aqString.Replace (s, '<', '&lt;', true);
  s := aqString.Replace (s, '>', '&gt;', true);
  s := aqString.Replace (s, '"', '&quot;', true);
  s := aqString.Replace (s, '''', '&apos;', 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"]("GridControlSort");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("gridControl1");

  // 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, StartColIndex, 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"];
  StartColIndex = Grid["GridCellsRange"]["Left"];
  EndColIndex = Grid["GridCellsRange"]["Right"];

  // Export column headers
  XmlFile["WriteLine"]("  <colheaders>");
  for (i=0; i<StartDataRowIndex; i++)
  {
    XmlFile["WriteLine"]("    <row>");
    for (j=StartColIndex; 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<=EndDataRowIndex; i++)
  {
    // Write the opening tag
    XmlFile["WriteLine"]("    <row>");
    // Export cells text
    for (j=StartColIndex; 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, "&", "&amp;", true);
  s = aqString["Replace"](s, "<", "&lt;", true);
  s = aqString["Replace"](s, ">", "&gt;", true);
  s = aqString["Replace"](s, "\"", "&quot;", true);
  s = aqString["Replace"](s, "'", "&apos;", true);
  return s;
}

See Also

Working With Syncfusion GridControl
Accessing Rows, Columns and Cells in Syncfusion GridControl
Obtaining and Setting Cell Values in Syncfusion GridControl
Searching for Records in Syncfusion GridControl

Highlight search results