Retrieving a Value from a Cell in a Custom Grid
From Seapine Labs
 Custom grids
Many custom grids are identified in the repository as generic controls, making it impossible to reference individual cells as separate controls. You must use low-level commands and key presses to navigate the grid. Before you can navigate the grid, you must locate a cell and place the cursor in the cell. For example, the first cell of the grid is located at the coordinates X=128 and Y=111. Using those coordinates in the LeftMouseDown and LeftMouseUp statements, you can click the first cell of the grid and then use KeyPress statements to tab to the different cells.
In many cases, you can obtain this information from the repository entry for the grid using the Parent Relative Rectangle or Screen Relative Client Rectangle properties. If you cannot locate coordinates of a given cell, use an OCR checkpoint to retrieve them.
Note: The OCR add-in is sold separately.
The following examples show how to retrieve data from a custom grid and how to locate a specific cell.
 Retrieving the value from a cell in a custom grid
The following example uses the DataGridView application, which contains a two-column custom grid.
Use an OCR checkpoint statement to determine the coordinates of the first cell (the header). By selecting the cell in an OCR checkpoing, you can see its dimensions and coordinates.
In the following example, the first cell is 41 pixels from the left of the window and 54 pixels from the top. The cell is 100 pixels wide and 22 pixels high.
Next, use the OCRGetText function to retrieve the contents of the cell and save them in a variable. Because the OCR statement already contains the coordinates you need, you can simply copy and paste the OCRCheckpoint arguments into the OCRGetText function and delete the accuracy and expected text values. In the following example, the script returns the value of the header cell and saves it in a variable named ‘CellContents’.
Using the information about the location of the first cell and the size of each cell, you can create a function like the following that returns the value of any cell.
The CellValue function accepts two arguments: the row number (ROW) and the column number (COL). It uses two variables, X and Y, to locate the cell. The grid starts 41 pixels from the left edge of the application, and each column is 100 pixels wide. In line 33, X is set to 41 + ((COL-1) * 100). If COL is equal to 1, X is set to 41 (the left edge of the first column). For each subsequent column, X is increased by a multiple of 100 (the width of a column).
Each row is 22 pixels high and there is a header row of the same height. The coordinates for the first data cell are (41, 76). To skip the header row above the first cell, Y is set equal to 54 (76 minus 22, the height of the header row) plus the ROW variable times 22 (the height of a row). When the ROW is 1, Y equals 76, which places the focus on the first data cell.
The X and Y variables are used in the OCRGetText function in line 37 to locate the cell. The OCRGetText function returns the contents of the cell which is stored in the CellContents variable. The function then returns the value of CellContents. The following screenshot shows the function used in a script to print the contents of row 4, column 2.