Working With TDBGrid - Selecting Several Rows

When working with the Borland TDBGrid controls, you may need to simulate selecting several grid rows.

Before selecting the rows, you need to locate the desired rows in the grid. To perform this task, use the Locate method of the TDataset object whose data the grid displays. Do not use the approaches that simulates keystrokes over the grid window, since pressing a key over the grid window removes the selection from the selected rows.

After the desired row is located, you can select it either by using the internal properties of the TDBGrid control, or by simulating mouse clicks over the grid row. 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.

Note that multiple selection in the TDBGrid control is possible only if the TDBGrid.Options property contains the dgMultiSelect value. If you are not sure whether the tested grid supports multi-selection, ask the application developer about this. You can also check this yourself by selecting several records with CTRL- or SHIFT-clicks.

Using TDBGrid Internal Properties

To mark the current row as selected, assign True to the SelectedRows.CurrentRowSelected property of the TDBGrid object. The following code demonstrates how you can do this.

The sample code contains the following routines:

  • Main - The “main” routine of the sample. It obtains the scripting object that corresponds to the TDBGrid object and then calls the SelectRow routine several times to choose several rows of the grid. Note: selecting a row does not remove the selection from the current grid row. To unselect the current row, we simulate the ESC keystroke over the grid window before selecting the desired rows.
  • SelectRow - This routine calls ActivateRow to activate the desired grid row and then assigns True to the SelectedRows.CurrentRowSelected property of the TDBGrid object.
  • ActivateRow - This is a helper routine that is used to activate the desired grid row. The routine uses the First and MoveBy methods of the TDataset object, whose data the grid displays.

Simulating Mouse Clicks on Rows

To mark the current row as selected, you can simulate a CTRL-click over it. To simulate a click, you can use the Click action of the scripting object that corresponds to the tested TDBGrid control. The Click action uses three parameters: ClientX, ClientY and Shift. The ClientX and ClientY parameters specify the coordinates of the click. Shift specifies the keys (CTRL, SHIFT, ALT) that should be pressed during the click. To simulate the click, the test script should calculate the row coordinate within the grid. To do this, you have to use the internal properties of the TDBGrid object. The following sample code demonstrates how you can do this and simulate a click. The sample simulates a CTRL-click rather than a SHIFT-click. CTRL-click chooses one row, while SHIFT-click chooses a range of rows, so using a CTRL-click lets you create more flexible testing procedures.

The sample code contains the following routines:

  • Main - The “main” routine of the sample. It obtains the scripting object that corresponds to the tested TDBGrid control. Then the routine calls the SelectRowByClick procedure several times to select several grid rows.

    Note that selecting a row does not remove the selection from the current grid row. To unselect the current row, we simulate the ESC keystroke over the grid window before selecting the desired rows.

  • SelectRowByClick - This routine performs actions that select a row with a mouse click. The routine calls the ActivateRow routine to choose the desired grid row, calculates the row's vertical coordinate within the grid window and then simulates a Ctrl-click over the row, thus, including the row in the collection of selected rows.

    The SelectRowByClick routine uses two parameters:

    • GridObject - specifies the tested TDBGrid object.
    • Row - specifies the index of the desired row in the grid. We assume that this index is zero-based, that is, the first data row in the grid has the index of 0.

      The vertical coordinate of the click point is stored in the Y variable. To find this coordinate, we use the internal Row and RowHeights properties of the TDBGrid control. Row specifies the index of the current row among other visible rows (do not confuse this property with the Row parameter). RowHeights specifies the height of each visible row (we use this property since row heights can differ).

      Since we process only visible rows, we ignore the position of the vertical scroll bar. When going through visible rows, we also add the height of grid lines to the Y variable. To obtain the line height, we use the grid's internal GridLineWidth property of the TDBGrid object.

      Finally, we increase the resultant Y value by 2 to ensure that the resultant coordinate belongs to the desired row.

      To simulate the click, we use the Click action of the scripting object that corresponds to the TDBGrid window. This action uses three parameters. The first and the second parameters specify the coordinates of the point where the click will occur. The third parameter specifies whether the SHIFT, CTRL or ALT key will be pressed during the click.

    • ActivateRow - This is a helper routine that is used to activate the desired grid row. The routine uses the First and MoveBy methods of the TDataset object, whose data the grid displays.

Close

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