Working With TDBGrid - Get Selected Row Values
Posted by
Alex Kuzin on Wed, Apr 04, 2007 @ 05:58 PM
To obtain the values of several rows, you need to create a script that will do the following:
- Iterate through all grid rows.
- Check if the row is selected and if it is, obtain the desired values.
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.
Iterating Through Grid Rows
Your script can iterate through grid rows by using the following methods and properties of the TDataset object, whose data the grid displays: First, Next, EOF and others. These are internal methods and properties of the TDataset object and TestComplete can only access them if the application under test is compiled as an Open Application. Do not iterate through grid rows using script instructions that simulate keystrokes. Doing this will unmark the selected rows.
Checking Whether Row Is Selected
To check if a row is selected, use the SelectedRows.CurrentRowSelected property of the TDBGrid object. This is an internal property of the object. TestComplete can only access it if the tested application is compiled as an Open Application. If the grid's current row is selected, the property is True, and it is False otherwise. The sample code below demonstrates how you can use this property.
Getting Cell Values
Since the TDBGrid control does not include methods or properties that will return the cell's value, we will obtain the value via the dataset field, whose data the cell displays. In VCL, 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.
Below is the sample code that demonstrates how you can obtain the selected rows' cell values. The example includes the following routines: