Selecting Multiple Rows in Borland TDBGrid

Applies to TestComplete 14.10, last modified on June 5, 2019

When working with TDBGrid controls, you may need to simulate selecting several grid rows and this topic gives examples of how you can do this. To learn how you can get values of the selected rows, see Obtaining Selected Rows in Borland TDBGrid.

To perform these actions, TestComplete must have access to internal methods and properties of the TDBGrid control. This requires the following conditions be met:

When testing Borland TDBGrid controls, use specific methods and properties of the corresponding BorlandTDBGrid object. You can call these methods and properties from your keyword tests, as well as from scripts. This topic describes how to work with an object’s properties and methods from your scripts. However, when testing a TDBGrid control from your keyword test, you can use the same methods and properties calling them from keyword test operations. For more information, see Keyword Tests Basic Operations.

General Notes

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.

Note that before selecting the rows, you need to locate the desired rows in the grid. For example, you can search for the desired rows using methods described in Searching for Records in Borland TDBGrid.

Note: When writing the code that will locate the desired rows, 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.
Simulating Selections With Mouse

To mark the current row as selected, you can simulate a Ctrl-click over its indicator. To simulate a click, you can use the ClickRowIndicator action of the BorlandTDBGrid scripting object that corresponds to the tested TDBGrid control. The ClickRowIndicator action has the Row and Shift parameters. The Row parameter specifies the zero-based index of the desired row. Shift specifies the keys (Ctrl, Shift, Alt) that should be pressed during the click.

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.

JavaScript, JScript

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys.Process("csdemos");
  p.VCLObject("FrmLauncher").VCLObject("BtnViews").ClickButton();
  Grid = p.VCLObject("FrmViewDemo").VCLObject("Panel2").VCLObject("DBGrid1");

  // Enable multiple selection
  Grid.Options += ",dgMultiSelect";

  // Select several rows
  Grid.Keys ("[Esc]");
  Grid.ClickRowIndicator (2, skCtrl);
  Grid.ClickRowIndicator (3, skCtrl);
  Grid.ClickRowIndicator (5, skCtrl);
}

Python

def Main ():

  # Obtain the grid object
  p = Sys.Process("csdemos")
  p.VCLObject("FrmLauncher").VCLObject("BtnViews").ClickButton()
  Grid = p.VCLObject("FrmViewDemo").VCLObject("Panel2").VCLObject("DBGrid1")

  # Enable multiple selection
  Grid.Options += ",dgMultiSelect"

  # Select several rows
  Grid.Keys ("[Esc]")
  Grid.ClickRowIndicator (2, skCtrl)
  Grid.ClickRowIndicator (3, skCtrl)
  Grid.ClickRowIndicator (5, skCtrl)

VBScript

Sub Main
  Dim p, Grid

  ' Obtain the grid object
  Set p = Sys.Process("csdemos")
  p.VCLObject("FrmLauncher").VCLObject("BtnViews").ClickButton
  Set Grid = p.VCLObject("FrmViewDemo").VCLObject("Panel2").VCLObject("DBGrid1")

  ' Enable multiple selection
  Grid.Options = Grid.Options & ",dgMultiSelect"

  ' Select several rows
  Grid.Keys ("[Esc]")
  Call Grid.ClickRowIndicator (2, skCtrl)
  Call Grid.ClickRowIndicator (3, skCtrl)
  Call Grid.ClickRowIndicator (5, skCtrl)
End Sub

DelphiScript

procedure Main;
var p, Grid : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('csdemos');
  p.VCLObject('FrmLauncher').VCLObject('BtnViews').ClickButton;
  Grid := p.VCLObject('FrmViewDemo').VCLObject('Panel2').VCLObject('DBGrid1');

  // Enable multiple selection
  Grid.Options := Grid.Options + ',dgMultiSelect';

  // Select several rows
  Grid.Keys ('[Esc]');
  Grid.ClickRowIndicator (2, skCtrl);
  Grid.ClickRowIndicator (3, skCtrl);
  Grid.ClickRowIndicator (5, skCtrl);
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys["Process"]("csdemos");
  p["VCLObject"]("FrmLauncher")["VCLObject"]("BtnViews")["ClickButton"]();
  Grid = p["VCLObject"]("FrmViewDemo")["VCLObject"]("Panel2")["VCLObject"]("DBGrid1");

  // Enable multiple selection
  Grid["Options"] += ",dgMultiSelect";

  // Select several rows
  Grid["Keys"]("[Esc]");
  Grid["ClickRowIndicator"](2, skCtrl);
  Grid["ClickRowIndicator"](3, skCtrl);
  Grid["ClickRowIndicator"](5, skCtrl);
}

Using TDBGrid Internal Properties

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

Example

View description

JavaScript, JScript

function Main()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys.Process("csdemos");
  p.VCLObject("FrmLauncher").VCLObject("BtnViews").ClickButton();
  Grid = p.VCLObject("FrmViewDemo").VCLObject("Panel2").VCLObject("DBGrid1");

  // Enable multiple selection
  Grid.Options += ",dgMultiSelect";

  // Select several rows
  Grid.Keys ("[Esc]");
  SelectRow (Grid, 2);
  SelectRow (Grid, 3);
  SelectRow (Grid, 5);
}

// Select a row by its index (zero-based)
function SelectRow (GridObject, Row)
{
  ActivateRow (GridObject, Row);
  GridObject.SelectedRows.CurrentRowSelected = true;
}

// Activate a row by its index (zero-based)
function ActivateRow (GridObject, RowIndex)
{
  GridObject.DataSource.DataSet.First();
  GridObject.DataSource.DataSet.MoveBy (RowIndex);
}

Python

def Main2():

  # Obtain the grid object
  p = Sys.Process("csdemos")
  p.VCLObject("FrmLauncher").VCLObject("BtnViews").ClickButton()
  Grid = p.VCLObject("FrmViewDemo").VCLObject("Panel2").VCLObject("DBGrid1")

  # Enable multiple selection
  Grid.Options += ",dgMultiSelect"

  # Select several rows
  Grid.Keys ("[Esc]")
  SelectRow (Grid, 2)
  SelectRow (Grid, 3)
  SelectRow (Grid, 5)

# Select a row by its index (zero-based)
def SelectRow (GridObject, Row):
  ActivateRow (GridObject, Row)
  GridObject.SelectedRows.CurrentRowSelected = True

# Activate a row by its index (zero-based)
def ActivateRow (GridObject, RowIndex):
  GridObject.DataSource.DataSet.First()
  GridObject.DataSource.DataSet.MoveBy (RowIndex)

VBScript

Sub Main
  Dim p, Grid

  ' Obtain the grid object
  Set p = Sys.Process("csdemos")
  p.VCLObject("FrmLauncher").VCLObject("BtnViews").ClickButton
  Set Grid = p.VCLObject("FrmViewDemo").VCLObject("Panel2").VCLObject("DBGrid1")

  ' Enable multiple selection
  Grid.Options = Grid.Options & ",dgMultiSelect"

  ' Select several rows
  Grid.Keys ("[Esc]")
  Call SelectRow (Grid, 2)
  Call SelectRow (Grid, 3)
  Call SelectRow (Grid, 4)
End Sub

' Select a row by its index (zero-based)
Sub SelectRow (GridObject, Row)
  Call ActivateRow (GridObject, Row)
  GridObject.SelectedRows.CurrentRowSelected = True
End Sub

' Activate a row by its index (zero-based)
Sub ActivateRow (GridObject, RowIndex)
  GridObject.DataSource.DataSet.First
  GridObject.DataSource.DataSet.MoveBy (RowIndex)
End Sub

DelphiScript

// Activate a row by its index (zero-based)
procedure ActivateRow (GridObject, RowIndex);
begin
  GridObject.DataSource.DataSet.First;
  GridObject.DataSource.DataSet.MoveBy(RowIndex);
end;

// Select a row by its index (zero-based)
procedure SelectRow (GridObject, Row);
begin
  ActivateRow (GridObject, Row);
  GridObject.SelectedRows.CurrentRowSelected := true;
end;

procedure Main;
var
  p, Grid : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('csdemos');
  p.VCLObject('FrmLauncher').VCLObject('BtnViews').ClickButton;
  Grid := p.VCLObject('FrmViewDemo').VCLObject('Panel2').VCLObject('DBGrid1');

  // Enable multiple selection
  Grid.Options := Grid.Options + ',dgMultiSelect';

  // Select several rows
  Grid.Keys ('[Esc]');
  SelectRow (Grid, 2);
  SelectRow (Grid, 3);
  SelectRow (Grid, 5);
end;

C++Script, C#Script

function Main()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys["Process"]("csdemos");
  p["VCLObject"]("FrmLauncher")["VCLObject"]("BtnViews")["ClickButton"]();
  Grid = p["VCLObject"]("FrmViewDemo")["VCLObject"]("Panel2")["VCLObject"]("DBGrid1");

  // Enable multiple selection
  Grid["Options"] += ",dgMultiSelect";

  // Select several rows
  Grid["Keys"]("[Esc]");
  SelectRow (Grid, 2);
  SelectRow (Grid, 3);
  SelectRow (Grid, 5);
}

// Select a row by its index (zero-based)
function SelectRow(GridObject, Row)
{
  ActivateRow (GridObject, Row);
  GridObject["SelectedRows"]["CurrentRowSelected"] = true;
}

// Activate a row by its index (zero-based)
function ActivateRow (GridObject, RowIndex)
{
  GridObject["DataSource"]["DataSet"]["First"]();
  GridObject["DataSource"]["DataSet"]["MoveBy"](RowIndex);
}

See Also

Working With Borland TDBGrid
ClickRowIndicator Action (Grid Controls)
Obtaining Selected Rows in Borland TDBGrid
Selecting Cells in Borland TDBGrid

Highlight search results