Working With TDBGrid - Get Selected Row Values
  April 04, 2007

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:

  • Main - The “main” routine of the sample. It obtains the scripting object that corresponds to the TDBGrid control, selects a number of grid rows and then calls the GetSelectedRows procedure that iterates through grid rows. To simulate the row selection, we use the SelectRow routine that is described in the Working With TDBGrid - Selecting Several Rows article. The routine's code is not included in the sample below, because the sample is not dependent on the way that the grid rows are selected, so the SelectRow routine is just one of the possible variants.
  • GetSelectedRows - This routine iterates through rows and if a row is selected, the routine posts the values of the LastName and FirstName columns to the test log.

    To perform the iteration, the routine performs the following actions:

    • Obtains the names of the dataset fields that correspond to the LastName and FirstName columns. This is needed since, in general, the column captions can differ from field names.
    • Calls the First method of the TDataset object, whose data the grid displays. This method activates the first record in the dataset.
    • Goes through the dataset records in a loop (when another record in the dataset is activated, the appropriate row in the grid is activated).
    • Within the loop body, the routine checks the SelectedRows.CurrentRowSelected property of the TDBGrid object. If this property is True, the current row is selected and the routine obtains the cell values via the AsString property of the appropriate TField object and then posts the values to the test log.
    • To proceed with the next record, the routine calls the Next method of the TDataset object.
    • The loop is over when the dataset's EOF property returns 1. The EOF property signals whether the last dataset record is active. If so, the property returns True, else False. TestComplete gets the property value through the Debug Info Agent™. Due to certain peculiarities, you should convert the property value to the boolean type by calling the BuiltIn.VarToBool function provided by TestComplete (when calling this function, you can omit the object name, BuiltIn).
  • GetFieldName - Helper routine that is used to obtain the name of the field, whose data the grid column displays.