Iterating Through Rows in Syncfusion GridGroupingControl

Applies to TestComplete 15.45, last modified on December 01, 2022

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

To determine the number of grid rows, you can use the wRowCount property of the SyncfusionEssGrid object. Once you have determined the number of data rows in the grid, you can iterate through them in a loop. Since the numeration of grid rows is zero-based, index of the first grid row is 0, and index of the last row is wRowCount-1. On each loop iteration, you can perform the needed actions with the current row. For example, you can modify values in its cells or save them to a file. For more information on how to obtain or modify grid cell values, see Obtaining and Setting Cell Values in Syncfusion GridGroupingControl.

The grid control can display data grouped by one or several columns. In a grouped grid, you can only access data rows via their parent groups. To access a particular group, use the wGroup property. The number of child groups in a grid table is specified by the wGroupCount property.

If the grid control displays data of multiple nested tables, then after processing a data row you may need to process its child data. You can access child tables via the wChildView property. The total number of child tables for a particular grid row is specified by the wChildViewCount property. You can process a child table in the same way as the root table -- by iterating through its rows. That is, the algorithm of processing the GridGroupingControl data implies that it is recursive.

TestComplete extends the GridGroupingControl functionality with properties and methods of the SyncfusionEssGrid object only if the Syncfusion Systems Control Support plugin is installed and enabled.

Below is an example that demonstrates how you can iterate through grid tables and rows and save the grid data to an XML file. Note that the routine only saves the data rows and ignores the grid’s grouping state.

Example

View description

JavaScript

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

  // Obtain the grid object
  p = Sys.Process("HierarchySample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("groupingGrid1");

  // Save the grid data to a file
  FileName = "C:\\GridData.xml";
  ExportToXml (Grid, FileName);
  Log.File(FileName, "Exported grid data.");
}

// Exports the grid data to a file
function ExportToXml (Grid, FileName)
{
  Indicator.PushText ("Exporting the grid data to a file...");

  // Create a text file and opens it for writing
  let Overwrite = true;
  let Unicode = true;
  let fso = getActiveXObject("Scripting.FileSystemObject");
  let XmlFile = fso.CreateTextFile (FileName, Overwrite, Unicode);

  // Write the root tag
  XmlFile.WriteLine("<?xml version=\"1.0\"?>");
  XmlFile.WriteLine ("<grid>");

  ExportTable (Grid, XmlFile, "  ");

  // Close the root tag
  XmlFile.WriteLine ("</grid>");
  // Close the file
  XmlFile.Close();

  Indicator.PopText();
}

// Writes the grid table data to the specified file
function ExportTable (View, XmlFile, indent)
{

  XmlFile.WriteLine (indent + "<table caption=\"" + ReplaceSpecialChars(View.wViewName) + "\">");

  // Export column captions
  for (let i=0; i<View.wColumnCount; i++)
    XmlFile.WriteLine (indent + "  <column>" + ReplaceSpecialChars(View.wColumn(i)) + "</column>");

  // If the table is grouped, ...
  if (View.wGroupCount > 0)
  {
    // ... process child groups
    for (let i=0; i<View.wGroupCount; i++)
      ExportGroup (View.wGroup(i), XmlFile, indent + "  ");
  }
  else
    // Export data rows
    ExportRows (View, XmlFile, indent + "  ");

  XmlFile.WriteLine (indent + "</table>");
}

function ExportGroup (Group, XmlFile, indent)
{
  // Check if the group has child groups
  if (Group.wGroupCount > 0)
  {
    // Process child groups
    for (let i=0; i<Group.wGroupCount; i++)
      ExportGroup (Group.wGroup(i), XmlFile, indent);
  }
  else
    // Export data rows
    ExportRows (Group, XmlFile, indent)
}

function ExportRows (Level, XmlFile, indent)
{
  var CellValue, TextValue;

  // Iterate through data rows and export their data
  for (let i=0; i<Level.wRowCount; i++)
  {
    XmlFile.WriteLine (indent + "<row>");
    for (let j=0; j<Level.wColumnCount; j++)
    {
      // Get the cell value
      CellValue = Level.wValue(i, j);
      // Get the text representation of the cell value
      if (equal(aqObject.GetVarType(CellValue), varDispatch))
        TextValue = CellValue.ToString().OleValue
      else
        TextValue = aqConvert.VarToStr(CellValue);
      XmlFile.WriteLine (indent + "  <cell>" + ReplaceSpecialChars(TextValue) + "</cell>");
    }

    // Export child tables, if any
    if (Level.wChildViewCount(i) > 0)
      for (let j=0; j<Level.wChildViewCount(i); j++)
        ExportTable (Level.wChildView(i, j), XmlFile, indent + "  ");

    XmlFile.WriteLine (indent + "</row>");
  }
}

// Replaces special XML characters in the specified string
function ReplaceSpecialChars (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 grid object
  p = Sys.Process("HierarchySample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("groupingGrid1");

  // Save the grid data to a file
  FileName = "C:\\GridData.xml";
  ExportToXml (Grid, FileName);
  Log.File(FileName, "Exported grid data.");
}

// Exports the grid data to a file
function ExportToXml (Grid, FileName)
{
  Indicator.PushText ("Exporting the grid data to a file...");

  // Create a text file and opens it for writing
  var Overwrite = true;
  var Unicode = true;
  var fso = new ActiveXObject ("Scripting.FileSystemObject");
  var XmlFile = fso.CreateTextFile (FileName, Overwrite, Unicode);

  // Write the root tag
  XmlFile.WriteLine("<?xml version=\"1.0\"?>");
  XmlFile.WriteLine ("<grid>");

  ExportTable (Grid, XmlFile, "  ");

  // Close the root tag
  XmlFile.WriteLine ("</grid>");
  // Close the file
  XmlFile.Close();

  Indicator.PopText();
}

// Writes the grid table data to the specified file
function ExportTable (View, XmlFile, indent)
{
  var i;

  XmlFile.WriteLine (indent + "<table caption=\"" + ReplaceSpecialChars(View.wViewName) + "\">");

  // Export column captions
  for (i=0; i<View.wColumnCount; i++)
    XmlFile.WriteLine (indent + "  <column>" + ReplaceSpecialChars(View.wColumn(i)) + "</column>");

  // If the table is grouped, ...
  if (View.wGroupCount > 0)
  {
    // ... process child groups
    for (i=0; i<View.wGroupCount; i++)
      ExportGroup (View.wGroup(i), XmlFile, indent + "  ");
  }
  else
    // Export data rows
    ExportRows (View, XmlFile, indent + "  ");

  XmlFile.WriteLine (indent + "</table>");
}

function ExportGroup (Group, XmlFile, indent)
{
  // Check if the group has child groups
  if (Group.wGroupCount > 0)
  {
    // Process child groups
    for (var i=0; i<Group.wGroupCount; i++)
      ExportGroup (Group.wGroup(i), XmlFile, indent);
  }
  else
    // Export data rows
    ExportRows (Group, XmlFile, indent)
}

function ExportRows (Level, XmlFile, indent)
{
  var i, j, CellValue, TextValue;

  // Iterate through data rows and export their data
  for (i=0; i<Level.wRowCount; i++)
  {
    XmlFile.WriteLine (indent + "<row>");
    for (j=0; j<Level.wColumnCount; j++)
    {
      // Get the cell value
      CellValue = Level.wValue(i, j);
      // Get the text representation of the cell value
      if (aqObject.GetVarType(CellValue) == varDispatch)
        TextValue = CellValue.ToString().OleValue
      else
        TextValue = aqConvert.VarToStr(CellValue);
      XmlFile.WriteLine (indent + "  <cell>" + ReplaceSpecialChars(TextValue) + "</cell>");
    }

    // Export child tables, if any
    if (Level.wChildViewCount(i) > 0)
      for (j=0; j<Level.wChildViewCount(i); j++)
        ExportTable (Level.wChildView(i, j), XmlFile, indent + "  ");

    XmlFile.WriteLine (indent + "</row>");
  }
}

// Replaces special XML characters in the specified string
function ReplaceSpecialChars (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 grid object
  p = Sys.Process("HierarchySample")
  Grid = p.WinFormsObject("Form1").WinFormsObject("groupingGrid1")

  # Save the grid data to a file
  FileName = "C:\\GridData.xml"
  ExportToXml (Grid, FileName)
  Log.File(FileName, "Exported grid data.")

# Exports the grid data to a file
def ExportToXml (Grid, FileName):
  Indicator.PushText ("Exporting the grid data to a file...")

  # Create a text file and opens it for writing
  Overwrite = True
  Unicode = True
  fso = Sys.OleObject ['Scripting.FileSystemObject']
  XmlFile = fso.CreateTextFile (FileName, Overwrite, Unicode)

  # Write the root tag
  XmlFile.WriteLine("<?xml version=\"1.0\"?>")
  XmlFile.WriteLine ("<grid>")

  ExportTable (Grid, XmlFile, "  ")

  # Close the root tag
  XmlFile.WriteLine ("</grid>")
  # Close the file
  XmlFile.Close()

  Indicator.PopText()

# Writes the grid table data to the specified file
def ExportTable (View, XmlFile, indent):

  XmlFile.WriteLine (indent + "<table caption=\"" + ReplaceSpecialChars(View.wViewName) + "\">")

  # Export column captions
  for i in range(0, View.wColumnCount-1):
    XmlFile.WriteLine (indent + "  <column>" + ReplaceSpecialChars(View.wColumn[i]) + "</column>")

  # If the table is grouped, ...
  if (View.wGroupCount > 0):
    # ... process child groups
    for i in range(0, View.wGroupCount-1):
      ExportGroup (View.wGroup[i], XmlFile, indent + "  ")
  else:
    # Export data rows
    ExportRows (View, XmlFile, indent + "  ")

  XmlFile.WriteLine (indent + "</table>")

def ExportGroup (Group, XmlFile, indent):
  # Check if the group has child groups
  if (Group.wGroupCount > 0):
    # Process child groups
    for i in range(0, Group.wGroupCount-1):
      ExportGroup (Group.wGroup[i], XmlFile, indent)
  else:
    # Export data rows
    ExportRows (Group, XmlFile, indent)

def ExportRows (Level, XmlFile, indent):

  # Iterate through data rows and export their data
  for i in range(0, Level.wRowCount-1):
    XmlFile.WriteLine (indent + "<row>")
    for j in range(0, Level.wColumnCount):
      # Get the cell value
      CellValue = Level.wValue[i, j]
      # Get the text representation of the cell value
      if (aqObject.GetVarType(CellValue) == varDispatch):
        TextValue = CellValue.ToString().OleValue
      else:
        TextValue = aqConvert.VarToStr(CellValue)
      XmlFile.WriteLine (indent + "  <cell>" + ReplaceSpecialChars(TextValue) + "</cell>")

    # Export child tables, if any
    if (Level.wChildViewCount(i) > 0):
      for j in range(0, Level.wChildViewCount[i]-1):
        ExportTable (Level.wChildView[i, j], XmlFile, indent + "  ")

    XmlFile.WriteLine (indent + "</row>")

# Replaces special XML characters in the specified string
def ReplaceSpecialChars (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 grid object
  Set p = Sys.Process("HierarchySample")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("groupingGrid1")

  ' Save the grid data to a file
  FileName = "C:\GridData.xml"
  Call ExportToXml (Grid, FileName)
  Call Log.File(FileName, "Exported grid data.")
End Sub

' Exports the grid data to a file
Sub ExportToXml (Grid, FileName)
  Dim fso, XmlFile, Overwrite, Unicode

  Indicator.PushText ("Exporting the grid data to a file...")

  ' Create a text file and opens it for writing
  Overwrite = True
  Unicode = True
  Set fso = CreateObject ("Scripting.FileSystemObject")
  Set XmlFile = fso.CreateTextFile (FileName, Overwrite, Unicode)

  ' Write the root tag
  Call XmlFile.WriteLine("<?xml version=""1.0""?>")
  XmlFile.WriteLine ("<grid>")

  Call ExportTable (Grid, XmlFile, "  ")

  ' Close the root tag
  XmlFile.WriteLine ("</grid>")
  ' Close the file
  XmlFile.Close

  Indicator.PopText
End Sub

' Writes the grid table data to the specified file
Sub ExportTable (View, XmlFile, indent)
  Dim i

  XmlFile.WriteLine (indent & "<table caption=""" & ReplaceSpecialChars(View.wViewName) & """>")

  ' Export column captions
  For i = 0 To View.wColumnCount-1
    XmlFile.WriteLine (indent & "  <column>" & ReplaceSpecialChars(View.wColumn(i)) & "</column>")
  Next

  ' If the table is grouped, ...
  If View.wGroupCount > 0 Then
    ' ... process child groups
    For i = 0 To View.wGroupCount-1
      Call ExportGroup (View.wGroup(i), XmlFile, indent & "  ")
    Next
  Else
    ' Export data rows
    Call ExportRows (View, XmlFile, indent & "  ")
  End If

  XmlFile.WriteLine (indent & "</table>")
End Sub

Sub ExportGroup (Group, XmlFile, indent)
  Dim i
  ' Check if the group has child groups
  If Group.wGroupCount > 0 Then
    ' Process child groups
    For i = 0 To Group.wGroupCount-1
      Call ExportGroup (Group.wGroup(i), XmlFile, indent)
    Next
  Else
    ' Export data rows
    Call ExportRows (Group, XmlFile, indent)
  End If
End Sub

Sub ExportRows (Level, XmlFile, indent)
  Dim i, j, CellValue, TextValue

  ' Iterate through data rows and export their data
  For i = 0 To Level.wRowCount-1
    XmlFile.WriteLine (indent & "<row>")
    For j = 0 To Level.wColumnCount-1
      ' Get the cell value and its text representation
      If aqObject.GetVarType(Level.wValue(i, j)) = varDispatch Then
        Set CellValue = Level.wValue(i, j)
        TextValue = CellValue.ToString.OleValue
      Else
        CellValue = Level.wValue(i, j)
        TextValue = aqConvert.VarToStr(CellValue)
      End If
      XmlFile.WriteLine (indent & "  <cell>" & ReplaceSpecialChars(TextValue) & "</cell>")
    Next

    ' Export child tables, if any
    If Level.wChildViewCount(i) > 0 Then
      For j = 0 To Level.wChildViewCount(i)-1
        Call ExportTable (Level.wChildView(i, j), XmlFile, indent & "  ")
      Next
    End If

    XmlFile.WriteLine (indent & "</row>")
  Next
End Sub

' Replaces special XML characters in the specified string
Function ReplaceSpecialChars (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)
  ReplaceSpecialChars = s
End Function

DelphiScript

procedure ExportToXml (Grid, FileName); forward;
procedure ExportTable (View, XmlFile, indent); forward;
procedure ExportGroup (Group, XmlFile, indent); forward;
procedure ExportRows (Level, XmlFile, indent); forward;
function ReplaceSpecialChars (str); forward;

procedure Main;
var p, Grid, FileName : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('HierarchySample');
  Grid := p.WinFormsObject('Form1').WinFormsObject('groupingGrid1');

  // Save the grid data to a file
  FileName := 'C:\GridData.xml';
  ExportToXml (Grid, FileName);
  Log.File(FileName, 'Exported grid data.');
end;

// Exports the grid data to a file
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 opens it for writing
  Overwrite := true;
  Unicode := true;
  fso := Sys.OleObject ('Scripting.FileSystemObject');
  XmlFile := fso.CreateTextFile (FileName, Overwrite, Unicode);

  // Write the root tag
  XmlFile.WriteLine('<?xml version="1.0"?>');
  XmlFile.WriteLine ('<grid>');

  ExportTable (Grid, XmlFile, '  ');

  // Close the root tag
  XmlFile.WriteLine ('</grid>');
  // Close the file
  XmlFile.Close;

  Indicator.PopText;
end;

// Writes the grid table data to the specified file
procedure ExportTable (View, XmlFile, indent);
var i : OleVariant;
begin
  XmlFile.WriteLine (indent + '<table caption="' + ReplaceSpecialChars(View.wViewName) + '">');

  // Export column captions
  for i := 0 to View.wColumnCount-1 do
    XmlFile.WriteLine (indent + '  <column>' + ReplaceSpecialChars(View.wColumn[i]) + '</column>');

  // If the table is grouped, ...
  if View.wGroupCount > 0 then
  begin
    // ... process child groups
    for i := 0 to View.wGroupCount-1 do
      ExportGroup (View.wGroup[i], XmlFile, indent + '  ');
  end
  else
    // Export data rows
    ExportRows (View, XmlFile, indent + '  ');

  XmlFile.WriteLine (indent + '</table>');
end;

procedure ExportGroup (Group, XmlFile, indent);
var i : OleVariant;
begin
  // Check if the group has child groups
  if Group.wGroupCount > 0 then
  begin
    // Process child groups
    for i := 0 to Group.wGroupCount-1 do
      ExportGroup (Group.wGroup[i], XmlFile, indent);
  end
  else
    // Export data rows
    ExportRows (Group, XmlFile, indent)
end;

procedure ExportRows (Level, XmlFile, indent);
var i, j, CellValue, TextValue : OleVariant;
begin
  // Iterate through data rows and export their data
  for i := 0 to Level.wRowCount-1 do
  begin
    XmlFile.WriteLine (indent + '<row>');
    for j := 0 to Level.wColumnCount-1 do
    begin
      // Get the cell value
      CellValue := Level.wValue[i, j];
      // Get the text representation of the cell value
      if aqObject.GetVarType(CellValue) = varDispatch then
        TextValue := CellValue.ToString.OleValue
      else
        TextValue := aqConvert.VarToStr(CellValue);
      XmlFile.WriteLine (indent + '  <cell>' + ReplaceSpecialChars(TextValue) + '</cell>');
    end;

    // Export child tables, if any
    if Level.wChildViewCount[i] > 0 then
      for j := 0 to Level.wChildViewCount[i]-1 do
        ExportTable (Level.wChildView[i, j], XmlFile, indent + '  ');

    XmlFile.WriteLine (indent + '</row>');
  end;
end;

// Replaces special XML characters in the specified string
function ReplaceSpecialChars (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 grid object
  p = Sys["Process"]("HierarchySample");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("groupingGrid1");

  // Save the grid data to a file
  FileName = "C:\\GridData.xml";
  ExportToXml (Grid, FileName);
  Log["File"](FileName, "Exported grid data.");
}

// Exports the grid data to a file
function ExportToXml (Grid, FileName)
{
  Indicator["PushText"]("Exporting the grid data to a file...");

  // Create a text file and opens it for writing
  var Overwrite = true;
  var Unicode = true;
  var fso = new ActiveXObject ("Scripting.FileSystemObject");
  var XmlFile = fso["CreateTextFile"](FileName, Overwrite, Unicode);

  // Write the root tag
  XmlFile["WriteLine"]("<?xml version=\"1.0\"?>");
  XmlFile["WriteLine"]("<grid>");

  ExportTable (Grid, XmlFile, "  ");

  // Close the root tag
  XmlFile["WriteLine"]("</grid>");
  // Close the file
  XmlFile["Close"]();

  Indicator["PopText"]();
}

// Writes the grid table data to the specified file
function ExportTable (View, XmlFile, indent)
{
  var i;

  XmlFile["WriteLine"](indent + "<table caption=\"" + ReplaceSpecialChars(View["wViewName"]) + "\">");

  // Export column captions
  for (i=0; i<View["wColumnCount"]; i++)
    XmlFile["WriteLine"](indent + "  <column>" + ReplaceSpecialChars(View["wColumn"](i)) + "</column>");

  // If the table is grouped, ...
  if (View["wGroupCount"] > 0)
  {
    // ... process child groups
    for (i=0; i<View["wGroupCount"]; i++)
      ExportGroup (View["wGroup"](i), XmlFile, indent + "  ");
  }
  else
    // Export data rows
    ExportRows (View, XmlFile, indent + "  ");

  XmlFile["WriteLine"](indent + "</table>");
}

function ExportGroup (Group, XmlFile, indent)
{
  // Check if the group has child groups
  if (Group["wGroupCount"] > 0)
  {
    // Process child groups
    for (var i=0; i<Group["wGroupCount"]; i++)
      ExportGroup (Group["wGroup"](i), XmlFile, indent);
  }
  else
    // Export data rows
    ExportRows (Group, XmlFile, indent)
}

function ExportRows (Level, XmlFile, indent)
{
  var i, j, CellValue, TextValue;

  // Iterate through data rows and export their data
  for (i=0; i<Level["wRowCount"]; i++)
  {
    XmlFile["WriteLine"](indent + "<row>");
    for (j=0; j<Level["wColumnCount"]; j++)
    {
      // Get the cell value
      CellValue = Level["wValue"](i, j);
      // Get the text representation of the cell value
      if (aqObject["GetVarType"](CellValue) == varDispatch)
        TextValue = CellValue["ToString"]()["OleValue"]
      else
        TextValue = aqConvert["VarToStr"](CellValue);
      XmlFile["WriteLine"](indent + "  <cell>" + ReplaceSpecialChars(TextValue) + "</cell>");
    }

    // Export child tables, if any
    if (Level["wChildViewCount"](i) > 0)
      for (j=0; j<Level["wChildViewCount"](i); j++)
        ExportTable (Level["wChildView"](i, j), XmlFile, indent + "  ");

    XmlFile["WriteLine"](indent + "</row>");
  }
}

// Replaces special XML characters in the specified string
function ReplaceSpecialChars (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 GridGroupingControl
Obtaining and Setting Cell Values in Syncfusion GridGroupingControl
Searching for Records in Syncfusion GridGroupingControl
wRowCount Property (Grid Controls)
wChildViewCount Property (Grid Controls)

Highlight search results