This topic describes how you can obtain and change values displayed in the cells of the QuantumGrid control (TcxGrid object) by Developer Express.
Requirements
-
The tested application must be compiled with debug information included (see Preparing Application With QuantumGrid Controls for TestComplete).
-
The Delphi and C++Builder Application Support and Developer Express Control Support plugins must be installed and enabled in TestComplete. The former plugin make Delphi and C++Builder applications Open to TestComplete. The latter plugin adds the
DevExpressQuantumGrid
object that provides support for QuantumGrid (TcxGrid) controls; without it you will not be able to work with QuantumGrid controls using their internal methods and properties.
Note also, that the compiler can exclude methods and properties that are not called in the application’s source code from the application’s binary code, so these methods and properties are unavailable to TestComplete (see Object Properties, Fields and Methods That Are Unavailable to TestComplete). To solve the problem, make sure that the desired methods and properties are used in the application’s source code. For instance, you can add a virtual method to your application that calls the desired methods and properties (the compiler does not exclude virtual methods).
When testing Developer Express QuantumGrid controls, use specific methods and properties of the corresponding DevExpressQuantumGrid
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 QuantumGrid 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.
Using the DevExpressQuantumGrid.wValue Property
In order to obtain a QuantumGrid 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 Developer Express QuantumGrid. 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 DevExpressQuantumGrid
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 QuantumGrid cell values via the wValue
property:
JavaScript, JScript
function Main ()
{
var p, Grid, Col;
// Obtain the grid object
p = Sys.Process("MySampleApp");
Grid = p.VCLObject("FormName").VCLObject("GridName");
// Post values from the first row to the log
// Make sure, your application is compiled with debug information
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("MySampleApp")
Grid = p.VCLObject("FormName").VCLObject("GridName")
# Post values from the first row to the log
# Make sure, your application is compiled with debug information
for Col in range(0, Grid.wColumnCount-1):
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("MySampleApp")
Set Grid = p.VCLObject("FormName").VCLObject("GridName")
' Post values from the first row to the log
' Make sure, your application is compiled with debug information
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('MySampleApp');
Grid := p.VCLObject('FormName').VCLObject('GridName');
// Post values from the first row to the log
// Make sure, your application is compiled with debug information
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"]("MySampleApp");
Grid = p["VCLObject"]("FormName")["VCLObject"]("GridName");
// Post values from the first row to the log
// Make sure, your application is compiled with debug information
for (Col=0; Col<Grid["wColumnCount"]; Col++)
Log["Message"]("Cell (0, " + Col + ") value: " + Grid["wValue"](0, Col));
}
Using TcxGrid Internal Methods and Properties
Alternatively, you can use the grid’s native methods and properties exposed by TestComplete to obtain or set the cell values.
Obtaining Cell Values
You can retrieve the text displayed in a particular cell from scripts by reading the ViewObj.ViewData.Rows(rowIndex).DisplayTexts(columnIndex)
property of the internal TcxGridDBTableView
class. See the Getting Views in Developer Express QuantumGrid topic to learn how to obtain the TcxGridDBTableView
instance that corresponds to the desired view.
The example below demonstrates how to obtain the cell value using internal properties.
Example
JavaScript
function Main()
{
var p, Grid, s;
p = Sys.Process("MySampleApp");
Grid = p.VCLObject("FormName").VCLObject("GridName");
s = GetCellValue(Grid, null, 2, 2);
Log.Message(s);
}
// Get cell value
function GetCellValue(grid, view, rowIndex, columnIndex)
{
// Obtain the view object
if(strictEqual(view, null))
view = grid.ActiveView;
// Get cell text
return view.ViewData.Rows(rowIndex).DisplayTexts(columnIndex);
}
JScript
function Main()
{
var p, Grid, s;
p = Sys.Process("MySampleApp");
Grid = p.VCLObject("FormName").VCLObject("GridName");
s = GetCellValue(Grid, null, 2, 2);
Log.Message(s);
}
// Get cell value
function GetCellValue(grid, view, rowIndex, columnIndex)
{
// Obtain the view object
if(view == null)
view = grid.ActiveView;
// Get cell text
return view.ViewData.Rows(rowIndex).DisplayTexts(columnIndex);
}
Python
def Main():
p = Sys.Process("MySampleApp")
Grid = p.VCLObject("FormName").VCLObject("GridName")
s = GetCellValue(Grid, None, 2, 2)
Log.Message(s)
# Get cell value
def GetCellValue(grid, view, rowIndex, columnIndex):
# Obtain the view object
if(view == None):
view = grid.ActiveView
# Get cell text
return view.ViewData.Rows(rowIndex).DisplayTexts(columnIndex)
VBScript
Sub Main
Set p = Sys.Process("MySampleApp")
Set Grid = p.VCLObject("FormName").VCLObject("GridName")
s = GetCellValue(Grid, Nothing, 2, 2)
Call Log.Message(s)
End Sub
' Get cell value
Function GetCellValue(grid, view, rowIndex, columnIndex)
' Obtain the view object
If view Is Nothing Then
Set view = grid.ActiveView
End If
' Get cell text
GetCellValue = view.ViewData.Rows(rowIndex).DisplayTexts(columnIndex)
End Function
DelphiScript
function GetCellValue(grid, view, rowIndex, columnIndex) : OleVariant; forward;
procedure Main;
var
p, Grid, s : OleVariant;
begin
p := Sys.Process('MySampleApp');
Grid := p.VCLObject('FormName').VCLObject('GridName');
s := GetCellValue(Grid, nil, 2, 2);
Log.Message(s);
end;
// Get cell value
function GetCellValue(grid, view, rowIndex, columnIndex) : OleVariant;
begin
// Obtain the view object
if view = nil then
view := grid.ActiveView;
// Get cell text
Result := view.ViewData.Rows(rowIndex).DisplayTexts(columnIndex);
end;
C++Script, C#Script
function Main()
{
var p, Grid, s;
p = Sys["Process"]("MySampleApp");
Grid = p["VCLObject"]("FormName")["VCLObject"]("GridName");
s = GetCellValue(Grid, null, 2, 2);
Log["Message"](s);
}
// Get cell value
function GetCellValue(grid, view, rowIndex, columnIndex)
{
// Obtain the view object
if(view == null)
view = grid["ActiveView"];
// Get cell text
return view["ViewData"]["Rows"](rowIndex)["DisplayTexts"](columnIndex);
}
Setting Cell Values
You can assign a cell value from scripts in any of the following ways:
- By using the
ViewObj.DataController.SetEditValue
method. - By activating the cell’s in-place editor and simulating user input.
Below is code sample that demonstrates how to use the SetEditValue
method:
Example
JavaScript
function Main()
{
var p, Grid;
p = Sys.Process("MySampleApp");
Grid = p.VCLObject("FormName").VCLObject("GridName");
// Set the cell value
SetCellValue(Grid, null, 4, 2, "My new value");
}
// Set the cell value
function SetCellValue(grid, view, rowIndex, columnIndex, newValue)
{
var data;
// Obtain the view object
if (strictEqual(view, null))
view = grid.ActiveView;
// Obtain the datacontroller
data = view.DataController;
// Select the grid row
data.FocusedRowIndex = rowIndex;
// Set the cell value
data.SetEditValue(columnIndex, newValue, 1);
}
JScript
function Main()
{
var p, Grid;
p = Sys.Process("MySampleApp");
Grid = p.VCLObject("FormName").VCLObject("GridName");
// Set the cell value
SetCellValue(Grid, null, 4, 2, "My new value");
}
// Set the cell value
function SetCellValue(grid, view, rowIndex, columnIndex, newValue)
{
var data;
// Obtain the view object
if (view == null)
view = grid.ActiveView;
// Obtain the datacontroller
data = view.DataController;
// Select the grid row
data.FocusedRowIndex = rowIndex;
// Set the cell value
data.SetEditValue(columnIndex, newValue, 1);
}
Python
def Main():
p = Sys.Process("MySampleApp")
Grid = p.VCLObject("FormName").VCLObject("GridName")
# Set the cell value
SetCellValue(Grid, None, 4, 2, "My new value")
# Set the cell value
def SetCellValue(grid, view, rowIndex, columnIndex, newValue):
# Obtain the view object
if (view == None):
view = grid.ActiveView
# Obtain the datacontroller
data = view.DataController
# Select the grid row
data.FocusedRowIndex = rowIndex
# Set the cell value
data.SetEditValue(columnIndex, newValue, 1)
VBScript
Sub Main
Set p = Sys.Process("MySampleApp")
Set Grid = p.VCLObject("FormName").VCLObject("GridName")
' Set the cell value
Call SetCellValue(Grid, Nothing, 4, 2, "My new value")
End Sub
' Set the cell value
Sub SetCellValue(grid, view, rowIndex, columnIndex, newValue)
' Obtain the view object
If view Is Nothing Then
Set view = grid.ActiveView
End If
' Obtain the datacontroller
Set data = view.DataController
' Select the grid row
data.FocusedRowIndex = rowIndex
' Set the cell value
Call data.SetEditValue(columnIndex, newValue, 1)
End Sub
DelphiScript
procedure Main;
var
p, Grid : OleVariant;
begin
p := Sys.Process('MySampleApp');
Grid := p.VCLObject('FormName').VCLObject('GridName');
// Set the cell value
SetCellValue(Grid, nil, 4, 2, 'My new value');
end;
// Set the cell value
procedure SetCellValue(grid, view, rowIndex, columnIndex, newValue);
var
data : OleVariant;
begin
// Obtain the view object
if view = nil then
view := grid.ActiveView;
// Obtain the datacontroller
data := view.DataController;
// Select the grid row
data.FocusedRowIndex := rowIndex;
// Set the cell value
data.SetEditValue(columnIndex, newValue, 1);
end;
C++Script, C#Script
function Main()
{
var p, Grid;
p = Sys["Process"]("MySampleApp");
Grid = p["VCLObject"]("FormName")["VCLObject"]("GridName");
// Set the cell value
SetCellValue(Grid, null, 4, 2, "My new value");
}
// Set the cell value
function SetCellValue(grid, view, rowIndex, columnIndex, newValue)
{
var data;
// Obtain the view object
if (view == null)
view = grid.ActiveView;
// Obtain the datacontroller
data = view["DataController"];
// Select the grid row
data["FocusedRowIndex"] = rowIndex;
// Set the cell value
data["SetEditValue"](columnIndex, newValue, 1);
}
The second way to set a grid cell value is to input 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 into the cell by simulating keystrokes. The sample below demonstrates how you can do this. The sample code implies that the cell contains a box, to which text can be entered. For information on working with cells that use specific editors (check boxes, combo boxes, drop-down lists and some others), see Working With Specific In-place Editors in Developer Express QuantumGrid.
Example
JavaScript
function Main()
{
var p, Grid;
p = Sys.Process("MySampleApp");
Grid = p.VCLObject("FormName").VCLObject("GridName");
// Set the cell value
SetCellValue(Grid, null, 4, 2, "My new value");
}
// Set the cell value
function SetCellValue(grid, view, rowIndex, columnId, newValue)
{
// Obtain the view object
if (strictEqual(view, null))
view = grid.ActiveView;
// Activate the inplace editor
ActivateCellInplaceEditor(grid, view, rowIndex, columnId);
// Select all of the cell's data
grid.Keys("^A");
// Enter new data into the cell and confirm the input
grid.Keys(newValue + "[Enter]");
}
// Activate inplace editor
function ActivateCellInplaceEditor(grid, view, rowIndex, columnId)
{
// Select the cell
SelectGridCell(grid, view, rowIndex, columnId);
// Simulate the keystroke
grid.Keys("[F2]");
}
// Select grid cell
function SelectGridCell(grid, view, rowIndex, columnId)
{
// Obtain the view object
if (strictEqual(view, null))
view = grid.ActiveView;
// Select the column
view.Controller.FocusedColumn = GetColumn(grid, view, columnId);
// Select the row
view.Controller.FocusedRowIndex = rowIndex;
}
// Obtain column object
function GetColumn(grid, view, columnId)
{
// Get the view
if (strictEqual(view, null))
view = grid.ActiveView;
// Check type of the columnId parameter
if (equal(aqObject.GetVarType(columnId), varOleStr))
{
// If columnId is a string,
// then search for column by its caption
for (let i = 0; i < view.ColumnCount; i++)
if (equal(view.Columns(i).Caption, columnId))
return view.Columns(i);
}
else
{
// If columnId is an integer,
// then search for column by its index
for (let i = 0; i < view.ColumnCount; i++)
if (equal(view.Columns(i).VisibleIndex, columnId))
return view.Columns(i);
}
}
JScript
function Main()
{
var p, Grid;
p = Sys.Process("MySampleApp");
Grid = p.VCLObject("FormName").VCLObject("GridName");
// Set the cell value
SetCellValue(Grid, null, 4, 2, "My new value");
}
// Set the cell value
function SetCellValue(grid, view, rowIndex, columnId, newValue)
{
// Obtain the view object
if (view == null)
view = grid.ActiveView;
// Activate the inplace editor
ActivateCellInplaceEditor(grid, view, rowIndex, columnId);
// Select all of the cell's data
grid.Keys("^A");
// Enter new data into the cell and confirm the input
grid.Keys(newValue + "[Enter]");
}
// Activate inplace editor
function ActivateCellInplaceEditor(grid, view, rowIndex, columnId)
{
// Select the cell
SelectGridCell(grid, view, rowIndex, columnId);
// Simulate the keystroke
grid.Keys("[F2]");
}
// Select grid cell
function SelectGridCell(grid, view, rowIndex, columnId)
{
// Obtain the view object
if (view == null)
view = grid.ActiveView;
// Select the column
view.Controller.FocusedColumn = GetColumn(grid, view, columnId);
// Select the row
view.Controller.FocusedRowIndex = rowIndex;
}
// Obtain column object
function GetColumn(grid, view, columnId)
{
var i;
// Get the view
if (view == null)
view = grid.ActiveView;
// Check type of the columnId parameter
if (aqObject.GetVarType(columnId) == varOleStr)
{
// If columnId is a string,
// then search for column by its caption
for (i = 0; i < view.ColumnCount; i++)
if (view.Columns(i).Caption == columnId)
return view.Columns(i);
}
else
{
// If columnId is an integer,
// then search for column by its index
for (i = 0; i < view.ColumnCount; i++)
if (view.Columns(i).VisibleIndex == columnId)
return view.Columns(i);
}
}
Python
def Main():
p = Sys.Process("MySampleApp")
Grid = p.VCLObject("FormName").VCLObject("GridName")
# Set the cell value
SetCellValue(Grid, None, 4, 2, "My new value")
# Set the cell value
def SetCellValue(grid, view, rowIndex, columnId, newValue):
# Obtain the view object
if (view == None):
view = grid.ActiveView
# Activate the inplace editor
ActivateCellInplaceEditor(grid, view, rowIndex, columnId)
# Select all of the cell's data
grid.Keys("^A")
# Enter new data into the cell and confirm the input
grid.Keys(newValue + "[Enter]")
# Activate inplace editor
def ActivateCellInplaceEditor(grid, view, rowIndex, columnId):
# Select the cell
SelectGridCell(grid, view, rowIndex, columnId)
# Simulate the keystroke
grid.Keys("[F2]")
# Select grid cell
def SelectGridCell(grid, view, rowIndex, columnId):
# Obtain the view object
if (view == None):
view = grid.ActiveView
# Select the column
view.Controller.FocusedColumn = GetColumn(grid, view, columnId)
# Select the row
view.Controller.FocusedRowIndex = rowIndex
# Obtain column object
def GetColumn(grid, view, columnId):
# Get the view
if (view == None):
view = grid.ActiveView
# Check type of the columnId parameter
if (aqObject.GetVarType(columnId) == varOleStr):
# If columnId is a string,
# then search for column by its caption
for i in range(0, view.ColumnCount-1):
if (view.Columns[i].Caption == columnId):
return view.Columns[i]
else:
# If columnId is an integer,
# then search for column by its index
for i in range(0, view.ColumnCount-1):
if (view.Columns[i].VisibleIndex == columnId):
return view.Columns[i]
VBScript
Sub Main
Set p = Sys.Process("MySampleApp")
Set Grid = p.VCLObject("FormName").VCLObject("GridName")
' Set the cell value
Call SetCellValue(Grid, Nothing, 4, 3, "My new value")
End Sub
' Set the cell value
Sub SetCellValue(grid, view, rowIndex, columnId, newValue)
' Obtain the view object
If view Is Nothing Then
Set view = grid.ActiveView
End If
' Activate the inplace editor
Call ActivateCellInplaceEditor(grid, view, rowIndex, columnId)
' Select all of the cell's data
Call grid.Keys("^A")
' Enter new data into the cell and confirm the input
Call grid.Keys(newValue + "[Enter]")
End Sub
' Activate inplace editor
Sub ActivateCellInplaceEditor(grid, view, rowIndex, columnId)
' Select the cell
Call SelectGridCell(grid, view, rowIndex, columnId)
' Simulate the keystroke
Call grid.Keys("[F2]")
End Sub
' Select grid cell
Sub SelectGridCell(grid, view, rowIndex, columnId)
' Obtain the view object
If view Is Nothing Then
Set view = grid.ActiveView
End If
' Select the column
view.Controller.FocusedColumn = GetColumn(grid, view, columnId)
' Select the row
view.Controller.FocusedRowIndex = rowIndex
End Sub
' Obtain column object
Function GetColumn(grid, view, columnId)
Dim Columns, i
' Get the view
If view Is Nothing Then
Set view = grid.ActiveView
End If
' Check type of the columnId parameter
If aqObject.GetVarType(columnId) = varOleStr Then
' If columnId is a string,
' then search for column by its caption
For i = 0 to view.ColumnCount - 1
If view.Columns(i).Caption = columnId Then
Set GetColumn = view.Columns(i)
Exit For
End If
Next
Else
' If columnId is an integer,
' then search for column by its index
For i = 0 to view.ColumnCount - 1
If view.Columns(i).VisibleIndex = columnId Then
Set GetColumn = view.Columns(i)
Exit For
End If
Next
End If
End Function
DelphiScript
procedure SetCellValue(grid, view, rowIndex, columnId, newValue); forward;
procedure ActivateCellInplaceEditor(grid, view, rowIndex, columnId); forward;
procedure SelectGridCell(grid, view, rowIndex, columnId); forward;
function GetColumn(grid, view, columnId): Variant; forward;
procedure Main;
var
p, Grid : OleVariant;
begin
p := Sys.Process('MySampleApp');
Grid := p.VCLObject('FormName').VCLObject('GridName');
// Set the cell value
SetCellValue(Grid, nil, 4, 2, 'My new value');
end;
// Set the cell value
procedure SetCellValue(grid, view, rowIndex, columnId, newValue);
var
Column, Row: Variant;
begin
// Obtain the view object
if view = nil then
view := grid.ActiveView;
// Activate the inplace editor
ActivateCellInplaceEditor(grid, view, rowIndex, columnId);
// Simulate the Ctrl-A keystroke
grid.Keys('^A');
// Enter new data into the cell and confirm the input
grid.Keys(newValue + '[Enter]');
end;
// Activate inplace editor
procedure ActivateCellInplaceEditor(grid, view, rowIndex, columnId);
begin
// Select the cell
SelectGridCell(grid, view, rowIndex, columnId);
// Simulate the keystroke
grid.Keys('[F2]');
end;
// Select grid cell
procedure SelectGridCell(grid, view, rowIndex, columnId);
begin
// Obtain the view object
if view = nil then
view := grid.ActiveView;
// Select the column
view.Controller.FocusedColumn := GetColumn(grid, view, columnId);
// Select the row
view.Controller.FocusedRowIndex := rowIndex;
end;
// Obtain column object
function GetColumn(grid, view, columnId): Variant;
var
i: Variant;
begin
// Get the view
if view = nil then
view := grid.ActiveView;
// Check type of the columnId parameter
if aqObject.GetVarType(columnId) = varOleStr then
begin
// If columnId is a string,
// then search for column by its caption
for i := 0 to view.ColumnCount - 1 do
if view.Columns[i].Caption = columnId then
Result := view.Columns[i];
end
else
// If columnId is an integer,
// then search for column by its index
for i := 0 to view.ColumnCount - 1 do
if view.Columns[i].VisibleIndex = columnId then
Result := view.Columns[i];
end;
C++Script, C#Script
function Main()
{
var p, Grid;
p = Sys["Process"]("MySampleApp");
Grid = p["VCLObject"]("FormName")["VCLObject"]("GridName");
// Set the cell value
SetCellValue(Grid, null, 4, 2, "My new value");
}
// Set the cell value
function SetCellValue(grid, view, rowIndex, columnId, newValue)
{
// Obtain the view object
if (view == null)
view = grid.ActiveView;
// Activate the inplace editor
ActivateCellInplaceEditor(grid, view, rowIndex, columnId);
// Select all of the cell's data
grid["Keys"]("^A");
// Enter new data into the cell and confirm the input
grid["Keys"](newValue + "[Enter]");
}
// Activate inplace editor
function ActivateCellInplaceEditor(grid, view, rowIndex, columnId)
{
// Select the cell
SelectGridCell(grid, view, rowIndex, columnId);
// Simulate the keystroke
grid["Keys"]("[F2]");
}
// Select grid cell
function SelectGridCell(grid, view, rowIndex, columnId)
{
// Obtain the view object
if (view == null)
view = grid["ActiveView"];
// Select the column
view["Controller"]["FocusedColumn"] = GetColumn(grid, view, columnId);
// Select the row
view["Controller"]["FocusedRowIndex"] = rowIndex;
}
// Obtain column object
function GetColumn(grid, view, columnId)
{
var i;
// Get the view
if (view == null)
view = grid["ActiveView"];
// Check type of the columnId parameter
if (aqObject["GetVarType"](columnId) == varOleStr)
{
// If columnId is a string,
// then search for column by its caption
for (i = 0; i < view["ColumnCount"]; i++)
if (view["Columns"](i)["Caption"] == columnId)
return view["Columns"](i);
}
else
{
// If columnId is an integer,
// then search for column by its index
for (i = 0; i < view["ColumnCount"]; i++)
if (view["Columns"](i)["VisibleIndex"] == columnId)
return view["Columns"](i);
}
}
See Also
Working With Developer Express QuantumGrid
Selecting Cells in Developer Express QuantumGrid
Activating In-place Editors in Developer Express QuantumGrid
Working With Specific In-place Editors in Developer Express QuantumGrid