Obtaining Selected Rows in Borland TDBGrid

Applies to TestComplete 15.45, last modified on December 01, 2022

When working with TDBGrid controls, you may need to simulate the selection of several grid rows. This topic explains how to obtain values from the selected rows.

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 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.

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 an Open Application. For more information on iterating, see Iterating Through Rows in Borland TDBGrid.

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 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

To obtain cell values, you can use the wValue property of a BorlandTDBGrid object that corresponds to the tested TDBGRid control. Alternatively, you can get cell values via the grid’s dataset. For more information, see Obtaining and Setting Cell Values in Borland TDBGrid.

Below is the sample code that demonstrates how you can obtain the selected rows’ cell values.

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]");
  Grid.ClickRowIndicator (2, skCtrl);
  Grid.ClickRowIndicator (4, skCtrl);
  Grid.ClickRowIndicator (7, skCtrl);

  // Iterate through selected rows
  GetSelectedRows (Grid);
}

// Iterate through the selected rows
function GetSelectedRows (GridObject)
{
  var RowNumber, s1, s2;

  Log.AppendFolder("Selected Rows:");

  // Activate the first record in the dataset
  GridObject.DataSource.DataSet.First();

  // Iterate through records
  RowNumber = 0;
  while (! aqConvert.VarToBool(GridObject.DataSource.DataSet.EOF))
  {
    // Check if the record is selected
    if(GridObject.SelectedRows.CurrentRowSelected)
    {
      // If the row is selected, then ...

      // ... obtain the values
      s1 = GridObject.wValue(RowNumber, "FirstName");
      s2 = GridObject.wValue(RowNumber, "LastName");
      // ... post the values to the log
      Log.Message ("Row " + RowNumber + ": " + s1 + " " + s2);
    }

    // Increase the row counter
    RowNumber++;

    // Activate the next record
    GridObject.DataSource.DataSet.Next();
  }

  Log.PopLogFolder();
}

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 (4, skCtrl)
  Grid.ClickRowIndicator (7, skCtrl)

  # Iterate through selected rows
  GetSelectedRows (Grid)

# Iterate through the selected rows
def GetSelectedRows (GridObject):

  Log.AppendFolder("Selected Rows:")

  # Activate the first record in the dataset
  GridObject.DataSource.DataSet.First()

  # Iterate through records
  RowNumber = 0
  while not aqConvert.VarToBool(GridObject.DataSource.DataSet.EOF):
    # Check if the record is selected
    if GridObject.SelectedRows.CurrentRowSelected:
      # If the row is selected, then ...

      # ... obtain the values
      s1 = GridObject.wValue(RowNumber, "FirstName")
      s2 = GridObject.wValue(RowNumber, "LastName")
      # ... post the values to the log
      Log.Message ("Row " + RowNumber + ": " + s1 + " " + s2)

    # Increase the row counter
    RowNumber+=1

    # Activate the next record
    GridObject.DataSource.DataSet.Next()

  Log.PopLogFolder()

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 (4, skCtrl)
  Call Grid.ClickRowIndicator (7, skCtrl)

  ' Iterate through selected rows
  GetSelectedRows (Grid)
End Sub

' Iterate through the selected rows
Sub GetSelectedRows (GridObject)
  Dim RowNumber, s1, s2

  Log.AppendFolder ("Selected Rows:")

  ' Activate the first record in the dataset
  GridObject.DataSource.DataSet.First

  ' Iterate through records
  RowNumber = 0
  While Not aqConvert.VarToBool(GridObject.DataSource.DataSet.EOF)
    ' Check if the record is selected
    If GridObject.SelectedRows.CurrentRowSelected Then
      ' If the row is selected, then ...

      ' ... obtain the values
      s1 = GridObject.wValue(RowNumber, "FirstName")
      s2 = GridObject.wValue(RowNumber, "LastName")
      ' ... post the values to the log
      Log.Message ("Row " & RowNumber & ": " & CStr(s1) & " " & CStr(s2))
    End If

    ' Increase the row counter
    RowNumber = RowNumber + 1

    ' Activate the next record
    GridObject.DataSource.DataSet.Next
  Wend

  Log.PopLogFolder
End Sub

DelphiScript

// Iterate through the selected rows
procedure GetSelectedRows (GridObject);
var RowNumber, s1, s2 : OleVariant;
begin
  Log.AppendFolder ('Selected Rows:');

  // Activate the first record in the dataset
  GridObject.DataSource.DataSet.First;

  // Iterate through records
  RowNumber := 0;
  while not aqConvert.VarToBool(GridObject.DataSource.DataSet.EOF) do
  begin
    // Check if the record is selected
    if GridObject.SelectedRows.CurrentRowSelected then
    begin
      // If the row is selected, then ...

      // ... obtain the values
      s1 := aqConvert.VarToStr( GridObject.wValue[RowNumber, 'FirstName'] );
      s2 := aqConvert.VarToStr( GridObject.wValue[RowNumber, 'LastName'] );
      // ... post the values to the log
      Log.Message ('Row ' + aqConvert.VarToStr(RowNumber) + ': ' + s1 + ' ' + s2);
    end;

    // Increase the row counter
    RowNumber := RowNumber + 1;

    // Activate the next record
    GridObject.DataSource.DataSet.Next;
  end;

  Log.PopLogFolder;
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]');
  Grid.ClickRowIndicator (2, skCtrl);
  Grid.ClickRowIndicator (4, skCtrl);
  Grid.ClickRowIndicator (7, skCtrl);

  // Iterate through selected rows
  GetSelectedRows (Grid);
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"](4, skCtrl);
  Grid["ClickRowIndicator"](7, skCtrl);

  // Iterate through selected rows
  GetSelectedRows (Grid);
}

// Iterate through the selected rows
function GetSelectedRows (GridObject)
{
  var RowNumber, s1, s2;

  Log["AppendFolder"]("Selected Rows:");

  // Activate the first record in the dataset
  GridObject["DataSource"]["DataSet"]["First"]();

  // Iterate through records
  RowNumber = 0;
  while (! aqConvert["VarToBool"](GridObject["DataSource"]["DataSet"]["EOF"]))
  {
    // Check if the record is selected
    if(GridObject["SelectedRows"]["CurrentRowSelected"])
    {
      // If the row is selected, then ...

      // ... obtain the values
      s1 = GridObject["wValue"](RowNumber, "FirstName");
      s2 = GridObject["wValue"](RowNumber, "LastName");
      // ... post the values to the log
      Log["Message"]("Row " + RowNumber + ": " + s1 + " " + s2);
    }

    // Increase the row counter
    RowNumber++;

    // Activate the next record
    GridObject["DataSource"]["DataSet"]["Next"]();
  }

  Log["PopLogFolder"]();
}

See Also

Working With Borland TDBGrid
Selecting Multiple Rows in Borland TDBGrid
Obtaining and Setting Cell Values in Borland TDBGrid

Highlight search results