Articles

[Collapse]TestComplete/viewarticle/63445/
 [Expand]General Information/viewarticle/63445/
 [Expand]Using Help System/viewarticle/55799/
 [Expand]Getting Started Tutorial/viewarticle/56246/
 [Expand]Using TestComplete/viewarticle/57875/
 [Expand]Testing Types/viewarticle/62721/
 [Expand]Applications Testing/viewarticle/62606/
 [Collapse]Working With Application Objects and Controls/viewarticle/55815/
  [Expand]General Information/viewarticle/63362/
  [Expand]Common Tasks/viewarticle/56267/
  [Expand]Simulating User Actions/viewarticle/56262/
  [Collapse]Object-Specific Tasks/viewarticle/63386/
   [Expand]Working With Standard Windows Controls/viewarticle/61593/
   [Expand]Working With Android Controls/viewarticle/58141/
   [Expand]Working With iOS Controls/viewarticle/63575/
   [Collapse]Working With Grids/viewarticle/55282/
     Working With Grids - Basic Concepts/viewarticle/61980/
     Common Operations for Grids/viewarticle/62006/
    [Expand]Borland TDBGrid/viewarticle/63214/
    [Expand]Developer Express QuantumGrid/viewarticle/60614/
    [Expand]Developer Express XtraGrid/viewarticle/60655/
    [Expand]Microsoft DataGrid/viewarticle/63259/
    [Expand]Microsoft DataGridView/viewarticle/63250/
    [Expand]Microsoft PropertyGrid/viewarticle/63261/
    [Expand]Infragistics UltraWebGrid/viewarticle/63309/
    [Expand]Infragistics WinGrid/viewarticle/63241/
    [Expand]Java Swing JTable/viewarticle/63315/
    [Collapse]Syncfusion GridControl/viewarticle/63291/
      Working With Syncfusion GridControl - Overview/viewarticle/63291/
      Accessing Rows, Columns and Cells in Syncfusion GridControl/viewarticle/62119/
      Searching for Records in Syncfusion GridControl/viewarticle/63293/
      Selecting Cells in Syncfusion GridControl/viewarticle/63294/
      Activating and Closing In-place Editors in Syncfusion GridControl/viewarticle/63295/
      Obtaining and Setting Cell Values in Syncfusion GridControl/viewarticle/63290/
      Working With Specific Cells in Syncfusion GridControl/viewarticle/63297/
      Copying and Pasting Cell Values in Syncfusion GridControl/viewarticle/63296/
      Selecting Multiple Rows in Syncfusion GridControl/viewarticle/63299/
      Obtaining Selected Rows in Syncfusion GridControl/viewarticle/63298/
      Iterating Through Rows in Syncfusion GridControl/viewarticle/63292/
      Sorting Data in Syncfusion GridControl/viewarticle/63300/
    [Expand]Syncfusion GridDataBoundGrid/viewarticle/63268/
    [Expand]Syncfusion GridGroupingControl/viewarticle/63283/
    Working With Open File and Save File Dialogs/viewarticle/55492/
    Testing Modal Windows/viewarticle/55107/
    Working With Ribbon Command Bars in Windows 7 Applications/viewarticle/63385/
    Working With XtraBars Controls/viewarticle/60549/
    Working With .NET ErrorProvider/viewarticle/62759/
 [Expand]Samples and Tutorials/viewarticle/56239/
 [Expand]References/viewarticle/57878/
 [Expand]Other Tools That Help You Test Better/viewarticle/63442/
 [Expand]TestComplete 10 Licensing Guide/viewarticle/63451/
Updated: 9/23/2014 Applies To: TestComplete 10 Rating: No votes Click to rate: PoorNot badAverageGoodExcellent
Working With Syncfusion GridControl

Iterating Through Rows in Syncfusion GridControl

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 plug-in 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

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

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;
}

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 - Overview | Accessing Rows, Columns and Cells in Syncfusion GridControl | Obtaining and Setting Cell Values in Syncfusion GridControl | Searching for Records in Syncfusion GridControl


© 2014 SmartBear Software. All rights reserved.
Email Send feedback on this document