Obtaining and Setting Cell Values in Borland TDBGrid

Applies to TestComplete 15.47, last modified on January 20, 2023

This topic describes how you can obtain and change values displayed in the cells of the TDBGrid control.

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.

Getting Cell Values

In order to obtain a TDBGrid cell value, you need to know the desired cell’s position within the grid. For example, you can search for it using the methods described here: Searching for Records in Borland TDBGrid. After you have located the cell’s row and column, you can retrieve the cell value.

To get the desired cell value, you can use the wValue property of the BorlandTDBGrid object. The property has the Row and Column parameters, which specify the row and column that contain the cell. The code snippet below demonstrates how you get the TDBGrid cell values via the wValue property:

JavaScript, JScript

function Main ()
{
  var p, Grid, Col;

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

  // Post values from the first row to the log
  for (Col=0; Col<Grid.wColumnCount; Col++)
    Log.Message ("Cell (0, " + Col + ") value: " + Grid.wValue(0, Col));
}

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

  # Post values from the first row to the log
  for Col in range(0, Grid.wColumnCount):
    Log.Message ("Cell (0, " + Col + ") value: " + Grid.wValue[0, Col])

VBScript

Sub Main
  Dim p, Grid, Col

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

  ' Post values from the first row to the log
  For Col = 0 To Grid.wColumnCount-1
    Log.Message ("Cell (0, " & Col & ") value: " & CStr(Grid.wValue(0, Col)))
  Next
End Sub

DelphiScript

procedure Main;
var p, Grid, Col : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('csdemos');
  p.VCLObject('FrmLauncher').VCLObject('BtnViews').ClickButton;
  Grid := p.VCLObject('FrmViewDemo').VCLObject('Panel2').VCLObject('DBGrid1');
  
  // Post values from the first row to the log
  for Col := 0 to Grid.wColumnCount-1 do
    Log.Message ('Cell (0, ' + aqConvert.VarToStr(Col) + ') value: ' + aqConvert.VarToStr(Grid.wValue[0, Col]));
end;

C++Script, C#Script

function Main ()
{
  var p, Grid, Col;

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

  // Post values from the first row to the log
  for (Col=0; Col<Grid["wColumnCount"]; Col++)
    Log["Message"]("Cell (0, " + Col + ") value: " + Grid["wValue"](0, Col));
}

Alternatively, you can obtain the cell’s value by retrieving the value that is stored in the dataset field, whose data the cell displays. In VCL, the 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.

The following code demonstrates how to obtain the cell’s value by the row and column indexes of the cell.

Example

View description

JavaScript, JScript

function Main()
{
  var p, Grid, s;
  
  // Obtain the grid object
  p = Sys.Process("csdemos");
  Grid = p.VCLObject("FrmViewDemo").VCLObject("Panel2").VCLObject("DBGrid1");
  
  // Get the cell value
  s = GetCell(Grid, 4, 1);
  Log.Message(s);
}

// Get the cell by its row and column indexes (zero-based)
function GetCell(GridObject, Row, Col)
{
  var FldName, Fld;
  
  // Get the field name
  FldName = GridObject.Columns.Items(Col).FieldName;
  // Activate the row
  ActivateRow(GridObject, Row);
  // Get the field text
  Fld = GridObject.DataSource.DataSet.FieldByName(FldName);
  return Fld.AsString;
}

// 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")
  Grid = p.VCLObject("FrmViewDemo").VCLObject("Panel2").VCLObject("DBGrid1")
  
  # Get the cell value
  s = GetCell(Grid, 4, 1)
  Log.Message(s)

# Get the cell by its row and column indexes (zero-based) 
def GetCell(GridObject, Row, Col):
  
  # Get the field name
  FldName = GridObject.Columns.Items(Col).FieldName
  # Activate the row
  ActivateRow(GridObject, Row)
  # Get the field text 
  Fld = GridObject.DataSource.DataSet.FieldByName(FldName)
  return Fld.AsString

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

VBScript

Sub Main
  ' Obtain the grid object
  Set p = Sys.Process("csdemos")
  Set Grid = p.VCLObject("FrmViewDemo").VCLObject("Panel2").VCLObject("DBGrid1")
  
  ' Get the cell value
  s = GetCell(Grid, 4, 1)
  Log.Message s
End Sub

' Get the cell by its row and column indexes (zero-based)
Function GetCell(GridObject, Row, Col)
  ' Get the field name
  FldName = GridObject.Columns.Items(Col).FieldName
  ' Activate the row
  Call ActivateRow(GridObject, Row)
  ' Get the field text
  Set Fld = GridObject.DataSource.DataSet.FieldByName(FldName)
  GetCell = Fld.AsString
End Function

' Activate a row by its index (zero-based)
Sub ActivateRow(GridObject, RowIndex)
  Call GridObject.DataSource.DataSet.First()
  Call 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;

// Get the cell by its row and column indexes (zero-based)
function GetCell(GridObject, Row, Col);
var
  FldName, Fld : OleVariant;
begin
  // Get the field name
  FldName := GridObject.Columns.Items[Col].FieldName;
  // Activate the row
  ActivateRow(GridObject, Row);
  // Get the field text
  Fld := GridObject.DataSource.DataSet.FieldByName[FldName];
  Result := Fld.AsString;
end;

procedure Main;
var
  p, Grid, s : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('csdemos');
  Grid := p.VCLObject('FrmViewDemo').VCLObject('Panel2').VCLObject('DBGrid1');
  
  // Get the cell value
  s := GetCell(Grid, 4, 1);
  Log.Message(s);
end;

C++Script, C#Script

function Main()
{
  var p, Grid, s;
  
  // Obtain the grid object
  p = Sys["Process"]("csdemos");
  Grid = p["VCLObject"]("FrmViewDemo")["VCLObject"]("Panel2")["VCLObject"]("DBGrid1");
  
  // Get the cell value
  s = GetCell(Grid, 4, 1);
  Log["Message"](s);
}

// Get the cell by its row and column indexes (zero-based)
function GetCell(GridObject, Row, Col)
{
  var FldName, Fld;
  
  // Get the field name
  FldName = GridObject["Columns"]["Items"](Col)["FieldName"];
  // Activate the row
  ActivateRow(GridObject, Row);
  // Get the field text
  Fld = GridObject["DataSource"]["DataSet"]["FieldByName"](FldName);
  return Fld["AsString"];
}

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

Note that VCL’s TField and TDBGrid objects include special events that let programmers modify the field text before the application displays it on screen. The example above will return the field’s text after it is modified by the events of the TField object. However, if a developer changes the displayed text using the TDBGrid object’s events, the returned text will differ from what you see in a grid cell.

Setting Cell Values

You can modify a grid cell value by inputting the desired value directly into the cell. In order for you to be able to do that, you first need to locate the desired cell within the grid, select it and activate its in-place editor. After the cell’s in-place editor is activated, you can “type” data in the cell by simulating keystrokes. Below is a sample that demonstrates how you can do this.

Note: After activating the in-place editor, the cell’s data is selected, so, the sample script will replace the cell value with the new value.
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");

  // Input new data
  SetCellByKeys(Grid, 2, "LastName", "Clemens");
  SetCellByKeys(Grid, 2, "HireDate", "5/2/2007");
}

// Assign a value to the specified cell
function SetCellByKeys(GridObject, RowIndex, ColumnId, NewValue)
{
  // Select the cell
  GridObject.ClickCell (RowIndex, ColumnId);
  // Activate the in-place editor and input the new value
  GridObject.Keys ("[Enter]" + NewValue + "[Enter]");
}

Python

def Main3():

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

  # Input new data
  SetCellByKeys(Grid, 2, "LastName", "Clemens")
  SetCellByKeys(Grid, 2, "HireDate", "5/2/2007")

# Assign a value to the specified cell 
def SetCellByKeys(GridObject, RowIndex, ColumnId, NewValue):
  # Select the cell
  GridObject.ClickCell (RowIndex, ColumnId)
  # Activate the in-place editor and input the new value
  GridObject.Keys ("[Enter]" + NewValue + "[Enter]")

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

  ' Input new data
  Call SetCellByKeys (Grid, 2, "LastName", "Clemens")
  Call SetCellByKeys (Grid, 2, "HireDate", "5/2/2007")
End Sub

' Assign a value to the specified cell
Sub SetCellByKeys(GridObject, RowIndex, ColumnId, NewValue)
  ' Select the cell
  Call GridObject.ClickCell (RowIndex, ColumnId)
  ' Activate the in-place editor and input the new value
  GridObject.Keys ("[Enter]" & NewValue & "[Enter]")
End Sub

DelphiScript

// Assign a value to the specified cell
procedure SetCellByKeys(GridObject, RowIndex, ColumnId, NewValue);
begin
  // Select the cell
  GridObject.ClickCell (RowIndex, ColumnId);
  // Activate the in-place editor and input the new value
  GridObject.Keys ('[Enter]' + NewValue + '[Enter]');
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');

  // Input new data
  SetCellByKeys (Grid, 2, 'LastName', 'Clemens');
  SetCellByKeys (Grid, 2, 'HireDate', '5/2/2007');
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");

  // Input new data
  SetCellByKeys(Grid, 2, "LastName", "Clemens");
  SetCellByKeys(Grid, 2, "HireDate", "5/2/2007");
}

// Assign a value to the specified cell
function SetCellByKeys(GridObject, RowIndex, ColumnId, NewValue)
{
  // Select the cell
  GridObject["ClickCell"](RowIndex, ColumnId);
  // Activate the in-place editor and input the new value
  GridObject["Keys"]("[Enter]" + NewValue + "[Enter]");
}

A possible alternative to typing data into the in-place editor is pasting the clipboard’s contents into the cell. For more information on this, see Copying and Pasting Cell Values in Borland TDBGrid.

See Also

Working With Borland TDBGrid
wValue Property (Grid Controls)
Selecting Cells in Borland TDBGrid
Activating In-place Editors in Borland TDBGrid
Copying and Pasting Cell Values in Borland TDBGrid

Highlight search results