Working With TDBGrid - Copying and Pasting Cell Values

Copying and pasting cell values are typical operations that you'll perform over grid controls. This article contains examples that show you how to copy and paste cell values in the Borland TDBGrid control.


Note that to perform these actions, TestComplete should have access to internal methods and properties of the TDBGrid control. That is, the tested application must be compiled as an Open Application with debug information.


Copying Cell Values


To be able to copy a grid cell value, you need to locate the desired cell within the grid, select it and activate its in-place editor. After the in-place editor is activated, you can copy the cell value by simulating the CTRL-C shortcut or by displaying the context menu and selecting Copy from it. The approach that uses the shortcut is a lot simpler. If you decide to use the context menu item, you will have to calculate the cell coordinates. Below is code that demonstrates how to copy the cell contents by simulating the CTRL-C shortcut.


Note: After activating the in-place editor, the cell's data is selected, so, there is no need to simulate user actions to select the cell's content. However, if the cell's content is not selected for some reason, you will have to select it. You can do this by sending the CTRL-A shortcut to the grid's window (that is, GridWndObject.Keys("^A")).


The sample code contains the following routines:



  • Main - The “main” routine of the sample. It performs the following actions:

    • Obtains the scripting object that corresponds to the grid window.

    • Calls the CopyCell function to copy the cell value.

    • Posts the copied data to the log.

  • CopyCell - Activates the cell editor, simulates pressing the CTRL-C shortcut to copy the cell value and returns the copied data. To obtain the copied data, we use the Sys.Clipboard property. The CopyCell function uses three parameters:

    • GridObject - specifies the scripting object that corresponds to the tested TDBGrid control.

    • Row and Col - specify the row and column indexes of the desired cell. Both indexes are zero-based, that is, we assume that the top-left data cell in the grid has the (0, 0) indexes.

  • ActivateCellEditor - Activates the in-place editor of the specified cell.

  • SelectCell - Moves focus to the specified cell.

  • ActivateRow - Selects a row in the TDBGrid control.

Close

Add a little SmartBear to your life

Stay on top of your Software game with the latest developer tips, best practices and news, delivered straight to your inbox

By submitting this form, you agree to our
Terms of Use and Privacy Policy

Thanks for Subscribing

Keep an eye on your inbox for more great content.

Continue Reading