Searching for Records in Syncfusion GridControl

  June 18, 2007

This article is the first in a series of articles on how to work with Syncfusion GridControl controls using TestComplete. Before performing any operations with the grid control, such as clicking a particular cell or retrieving the cell value, you need to locate the data you're going to work with. In other words, you need to search for the desired record within the grid. This article explains how to locate records in GridControl controls usingTestComplete scripts.

Currently, TestComplete has no special support for GridControl controls. However, Syncfusion GridControl is a .NET control and can be used only within .NET applications, which are always Open to TestComplete. In this case, the methods and properties of  GridControl are also
available to TestComplete. This means that you can use  GridControl’s internal methods and properties to perform desired actions, for example, to access grid rows and get their cell values.

The GridControl object has no built-in methods that can be used to search for
records within a grid, so you have to implement the search algorithm yourself. A general search approach implies that you iterate through the grid rows and
on each iteration you check whether the row meets the specified condition. For example, you can check if the cell value or text in a particular column equals the sought-for value or text.

To iterate through grid rows, you need to organize a loop whose counter changes from gridObj.GridCellsRange.Top (index of the first data row) to gridObj.GridCellsRange.Bottom (index of the last data row). To determine the value in a particular cell, use the gridObj.Item(RowIndex, ColIndex).CellValue property, where RowIndex and ColIndex are zero-based indexes of the desired cell.

The script below demonstrates how you can locate a particular row by iterating through the grid rows and comparing cell values. This example works with the TextFormat sample application that comes with Syncfusion Essential Studio and resides in the following folder:

» <Syncfusion Essential Studio>WindowsGrid.WindowsSamplesAppearanceTextFormat

The script contains two routines:

  • Main is the main routine. It obtains the GridControl object and locates a grid row using the FindRow function.

  • FindRow locates a grid row by the value in the specified column. If the row was found, FindRow returns the its zero-based index, otherwise it returns -1. The function takes three parameters:

    • Grid -- The tested GridControl.
    • ColIndex -- The zero-based index of the grid column that contains the cell with the sought-for value.
    • Value -- The sought-for value. The type of the Value parameter should correspond to the grid cell data type: integer, double, string or other.

If in the tested application GridControl is configured so that it displays data
in a special format, it may be more convenient to search for records by text dispalyed in grid cells rather than by cell values. To determine the cell’s text, you can use the gridObj.Item(RowIndex, ColIndex).FormattedText.OleValue property. The following example demonstrates how you can use this statement in the search function. The example is similar to the previous one, except that it locates a row by cell text. The search function is FindRowByCellText.