Working With TDBGrid - Getting Cell Values
Test and Monitor | Posted March 30, 2007

In order to obtain a TDBGrid cell value, you need to know the desired cell's position within the grid. After you have located the cell's row and column, you can retrieve the cell value.

The TDBGrid control does not contain methods and properties that allow you to retrieve the data displayed in the grid. However, you can obtain the cell's value by retrieving the value that is stored in the dataset field, whose data the cell displays. In VCL, the dataset fields are represented by TField objects. To get this object, we will use the FieldByName property of the TDataset object. After we obtain the TField object, we can get the field value by calling the AsString property of the TField object.

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.

The following sample demonstrates how to obtain the cell's value by the row and column indexes of the cell. The sample contains the following routines:

  • Main - The “main” routine of the sample. It obtains the scripting object that corresponds to the TDBGrid control, calls the GetCell function to obtain the cell data and then posts the data to the test log.
  • GetCell - Returns the cell's contents by the row and column indexes of the cell. Both indexes are zero-based, that is, the top-left data cell of the grid has the indexes of (0, 0). At the beginning, the GetCell function uses native properties of the TDBGrid object to obtain the name of the field that corresponds to the specified column. Then, GetCell calls the ActivateRow routine to activate the desired row in the dataset. Finally, GetCell uses the FieldByName property of the grid's dataset to obtain the field object (TField) and then calls the AsString property of this object to get the field's text.
  • ActivateRow - Activates the row that contains the desired cell (the row index is zero-based, that is, the topmost data row has an index of 0).

Note that VCL's TField and TDBGrid objects include special events that let programmers modify the field text before the application displays it on screen. The example above will return the field's text after it is modified by the events of the TField object. However, if a developer changes the displayed text using the TDBGrid object's events, the returned text will differ from what you see in a grid cell.


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

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