When testing an application with the Syncfusion GridDataBoundGrid control, you may need to copy or paste grid data to and from the clipboard. For example, you can use this to specify the same values in multiple cells or organize a data exchange between the tested application and another one. With TestComplete, you can directly access the clipboard contents using the Sys.Clipboard
property. This way, you can get the text copied to the clipboard as well as set the clipboard contents in order to paste it to the application under test.
This topic describes the two approaches that you can use to copy and paste GridDataBoundGrid data from scripts; simulating the corresponding user actions over the grid control and using the grid’s internal properties and methods. In your scripts, you can use the approach that better suits your needs. Depending on the approach you choose, you may need to locate the desired cell in the grid and select it in order to be able to copy or paste the cell value. For more information on how you can obtain and modify grid data, see Obtaining and Setting Cell Values.
To perform these actions, TestComplete should have access to internal objects, properties and methods of the GridDataBoundGrid control. For this purpose, the .NET Application Support plugin must be installed and enabled. When testing Syncfusion GridDataBoundGrid controls, use specific methods and properties of the corresponding |
Simulating User Actions Over the GridDataBoundGrid Control
You can copy the cell value to the clipboard or paste it from the clipboard using the Ctrl+C and Ctrl+V keyboard shortcuts. You can simulate these shortcuts using the Keys
action applied to the grid control.
Before performing the copy or paste operation, you need to locate the desired cell in the grid, select it and activate its in-place editor. After the cell’s edit mode is activated, you should specify the cell contents to be copied or replaced. For example, to select the entire cell value, you can simulate the successive Home and Shift+End key presses (Home moves the caret to the beginning of the cell text and Shift+End selects the cell text from the caret position to the end). After performing the pasting operation, you should apply the changes by simulating the Enter keypress.
Note: | The grid control may be configured so that it selects the cell contents right after the cell becomes selected. For example, this happens if the grid’s ActivateCurrentCellBehavior property is set to “SelectAll”. In this case, you do not need to perform any actions to select the cell text to be copied or replaced. |
Below in an example that illustrates how you can copy the GridDataBoundGrid cells' values to and paste them from the clipboard.
Example
JavaScript, JScript
function Main ()
{
var p, Grid, i;
// Obtain the application process and the grid object
p = Sys.Process ("DataBoundSortByDisplayMember");
Grid = p.WinFormsObject("Form1").WinFormsObject("gridDataBoundGrid1");
// Copy the cell value and paste it to other cells
CopyCellValue (Grid, 1, 2);
Log.Message ("Copied value: " + Sys.Clipboard);
for (i=3; i<=6; i++)
PasteCellValue (Grid, i, 2);
// Set the clipboard contents directly and paste it into the cell
Sys.Clipboard = "Ipoh Coffee";
PasteCellValue (Grid, 10, 2);
}
function CopyCellValue (Grid, RowIndex, ColumnId)
{
// Select the cell and activate its in-place editor
ClickCell (Grid, RowIndex, ColumnId);
ActivateCellEditor (Grid);
// Select the cell contents and copy it to the clipboard
Grid.Keys ("[Home]![End]^c");
// Close the cell's editor
CancelEdit (Grid);
}
function PasteCellValue (Grid, RowIndex, ColumnId)
{
// Select the cell and activate its in-place editor
ClickCell (Grid, RowIndex, ColumnId);
ActivateCellEditor (Grid);
// Replace the cell value with the clipboard contents
Grid.Keys ("[Home]![End]^v");
// Save the changes
CloseCellEditor (Grid);
}
function ClickCell (Grid, RowIndex, ColumnId)
{
var ColIndex, rect;
// Convert the column's identifier to its absolute index
ColIndex = GetColIndexById (Grid, ColumnId);
// Make cell visible
Grid.ScrollCellInView_3 (RowIndex, ColIndex);
// Get the cell coordinates
rect = Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell(RowIndex, ColIndex));
Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2);
}
function GetColIndexById (Grid, ColumnId)
{
if (aqObject.GetVarType(ColumnId) == varOleStr)
return Grid.NameToColIndex(ColumnId)
else
return ColumnId;
}
function ActivateCellEditor (Grid)
{
if (!Grid.CurrentCell.IsEditing)
Grid.Keys ("[F2]")
}
function CloseCellEditor (Grid)
{
Grid.Keys ("[Enter]");
}
function CancelEdit (Grid)
{
Grid.Keys ("[Esc]");
}
Python
def Main ():
# Obtain the application process and the grid object
p = Sys.Process ("DataBoundSortByDisplayMember");
Grid = p.WinFormsObject("Form1").WinFormsObject("gridDataBoundGrid1");
# Copy the cell value and paste it to other cells
CopyCellValue (Grid, 1, 2);
Log.Message ("Copied value: " + Sys.Clipboard);
for i in range(3, 6):
PasteCellValue (Grid, i, 2);
# Set the clipboard contents directly and paste it into the cell
Sys.Clipboard = "Ipoh Coffee";
PasteCellValue (Grid, 10, 2);
def CopyCellValue (Grid, RowIndex, ColumnId):
# Select the cell and activate its in-place editor
ClickCell (Grid, RowIndex, ColumnId);
ActivateCellEditor (Grid);
# Select the cell contents and copy it to the clipboard
Grid.Keys ("[Home]![End]^c");
# Close the cell's editor
CancelEdit (Grid);
def PasteCellValue (Grid, RowIndex, ColumnId):
# Select the cell and activate its in-place editor
ClickCell (Grid, RowIndex, ColumnId);
ActivateCellEditor (Grid);
# Replace the cell value with the clipboard contents
Grid.Keys ("[Home]![End]^v");
# Save the changes
CloseCellEditor (Grid);
def ClickCell (Grid, RowIndex, ColumnId):
# Convert the column's identifier to its absolute index
ColIndex = GetColIndexById (Grid, ColumnId);
# Make cell visible
Grid.ScrollCellInView_3 (RowIndex, ColIndex);
# Get the cell coordinates
rect = Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell[RowIndex, ColIndex]);
Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2);
def GetColIndexById (Grid, ColumnId):
if (aqObject.GetVarType(ColumnId) == varOleStr):
return Grid.NameToColIndex(ColumnId)
else:
return ColumnId;
def ActivateCellEditor (Grid):
if not Grid.CurrentCell.IsEditing:
Grid.Keys ("[F2]")
def CloseCellEditor (Grid):
Grid.Keys ("[Enter]");
def CancelEdit (Grid):
Grid.Keys ("[Esc]");
VBScript
Sub Main
Dim p, Grid, i
' Obtain the application process and the grid object
Set p = Sys.Process ("DataBoundSortByDisplayMember")
Set Grid = p.WinFormsObject("Form1").WinFormsObject("gridDataBoundGrid1")
' Copy the cell value and paste it to other cells
Call CopyCellValue (Grid, 1, 2)
Call Log.Message ("Copied value: " & Sys.Clipboard)
For i=3 To 6
Call PasteCellValue (Grid, i, 2)
Next
' Set the clipboard contents directly and paste it into the cell
Sys.Clipboard = "Ipoh Coffee"
Call PasteCellValue (Grid, 10, 2)
End Sub
Sub CopyCellValue (Grid, RowIndex, ColumnId)
' Select the cell and activate its in-place editor
Call ClickCell (Grid, RowIndex, ColumnId)
Call ActivateCellEditor (Grid)
' Select the cell contents and copy it to the clipboard
Call Grid.Keys ("[Home]![End]^c")
' Close the cell's editor
Call CancelEdit (Grid)
End Sub
Sub PasteCellValue (Grid, RowIndex, ColumnId)
' Select the cell and activate its in-place editor
Call ClickCell (Grid, RowIndex, ColumnId)
Call ActivateCellEditor (Grid)
' Replace the cell value with the clipboard contents
Call Grid.Keys ("[Home]![End]^v")
' Save the changes
Call CloseCellEditor (Grid)
End Sub
Sub ClickCell (Grid, RowIndex, ColumnId)
Dim ColIndex, rect
' Convert the column's identifier to its index
ColIndex = GetColIndexById (Grid, ColumnId)
' Make cell visible
Call Grid.ScrollCellInView_3 (RowIndex, ColIndex)
' Get the cell coordinates
Set rect = Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell(RowIndex, ColIndex))
Call Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2)
End Sub
Function GetColIndexById (Grid, ColumnId)
If aqObject.GetVarType(ColumnId) = varOleStr Then
GetColIndexById = Grid.NameToColIndex(ColumnId)
Else
GetColIndexById = ColumnId
End If
End Function
Sub ActivateCellEditor (Grid)
If Not Grid.CurrentCell.IsEditing Then
Call Grid.Keys ("[F2]")
End If
End Sub
Sub CloseCellEditor (Grid)
Call Grid.Keys ("[Enter]")
End Sub
Sub CancelEdit (Grid)
Call Grid.Keys ("[Esc]")
End Sub
DelphiScript
procedure CopyCellValue (Grid, RowIndex, ColumnId); forward;
procedure PasteCellValue (Grid, RowIndex, ColumnId); forward;
procedure ClickCell (Grid, RowIndex, ColumnId); forward;
function GetColIndexById (Grid, ColumnId); forward;
procedure ActivateCellEditor (Grid); forward;
procedure CloseCellEditor (Grid); forward;
procedure CancelEdit (Grid); forward;
procedure Main;
var p, Grid, i : OleVariant;
begin
// Obtain the application process and the grid object
p := Sys.Process ('DataBoundSortByDisplayMember');
Grid := p.WinFormsObject('Form1').WinFormsObject('gridDataBoundGrid1');
// Copy the cell value and paste it to other cells
CopyCellValue (Grid, 1, 2);
Log.Message ('Copied value: ' + Sys.Clipboard);
for i:=3 to 6 do
PasteCellValue (Grid, i, 2);
// Set the clipboard contents directly and paste it into the cell
Sys.Clipboard := 'Ipoh Coffee';
PasteCellValue (Grid, 10, 2);
end;
procedure CopyCellValue (Grid, RowIndex, ColumnId);
begin
// Select the cell and activate its in-place editor
ClickCell (Grid, RowIndex, ColumnId);
ActivateCellEditor (Grid);
// Select the cell contents and copy it to the clipboard
Grid.Keys ('[Home]![End]^c');
// Close the cell's editor
CancelEdit (Grid);
end;
procedure PasteCellValue (Grid, RowIndex, ColumnId);
begin
// Select the cell and activate its in-place editor
ClickCell (Grid, RowIndex, ColumnId);
ActivateCellEditor (Grid);
// Replace the cell value with the clipboard contents
Grid.Keys ('[Home]![End]^v');
// Save the changes
CloseCellEditor (Grid);
end;
procedure ClickCell (Grid, RowIndex, ColumnId);
var ColIndex, rect : OleVariant;
begin
// Convert the column's identifier to its index
ColIndex := GetColIndexById (Grid, ColumnId);
// Make cell visible
Grid.ScrollCellInView_3 (RowIndex, ColIndex);
// Get the cell coordinates
rect := Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell(RowIndex, ColIndex));
Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2);
end;
function GetColIndexById (Grid, ColumnId);
begin
if aqObject.GetVarType(ColumnId) = varOleStr then
Result := Grid.NameToColIndex(ColumnId)
else
Result := ColumnId;
end;
procedure ActivateCellEditor (Grid);
begin
if not Grid.CurrentCell.IsEditing then
Grid.Keys ('[F2]')
end;
procedure CloseCellEditor (Grid);
begin
Grid.Keys ('[Enter]');
end;
procedure CancelEdit (Grid);
begin
Grid.Keys ('[Esc]');
end;
C++Script, C#Script
function Main ()
{
var p, Grid, i;
// Obtain the application process and the grid object
p = Sys["Process"]("DataBoundSortByDisplayMember");
Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("gridDataBoundGrid1");
// Copy the cell value and paste it to other cells
CopyCellValue (Grid, 1, 2);
Log["Message"]("Copied value: " + Sys["Clipboard"]);
for (i=3; i<=6; i++)
PasteCellValue (Grid, i, 2);
// Set the clipboard contents directly and paste it into the cell
Sys["Clipboard"] = "Ipoh Coffee";
PasteCellValue (Grid, 10, 2);
}
function CopyCellValue (Grid, RowIndex, ColumnId)
{
// Select the cell and activate its in-place editor
ClickCell (Grid, RowIndex, ColumnId);
ActivateCellEditor (Grid);
// Select the cell contents and copy it to the clipboard
Grid["Keys"]("[Home]![End]^c");
// Close the cell's editor
CancelEdit (Grid);
}
function PasteCellValue (Grid, RowIndex, ColumnId)
{
// Select the cell and activate its in-place editor
ClickCell (Grid, RowIndex, ColumnId);
ActivateCellEditor (Grid);
// Replace the cell value with the clipboard contents
Grid["Keys"]("[Home]![End]^v");
// Save the changes
CloseCellEditor (Grid);
}
function ClickCell (Grid, RowIndex, ColumnId)
{
var ColIndex, rect;
// Convert the column's identifier to its absolute index
ColIndex = GetColIndexById (Grid, ColumnId);
// Make cell visible
Grid["ScrollCellInView_3"](RowIndex, ColIndex);
// Get the cell coordinates
rect = Grid["RangeInfoToRectangle"](Grid["GridCellsRange"]["Cell"](RowIndex, ColIndex));
Grid["Click"](rect["X"] + rect["Width"]/2, rect["Y"] + rect["Height"]/2);
}
function GetColIndexById (Grid, ColumnId)
{
if (aqObject["GetVarType"](ColumnId) == varOleStr)
return Grid["NameToColIndex"](ColumnId)
else
return ColumnId;
}
function ActivateCellEditor (Grid)
{
if (! Grid["CurrentCell"]["IsEditing"])
Grid["Keys"]("[F2]")
}
function CloseCellEditor (Grid)
{
Grid["Keys"]("[Enter]");
}
function CancelEdit (Grid)
{
Grid["Keys"]("[Esc]");
}
The GridDataBoundGrid control may also have a context menu allowing the users to copy or paste grid cell values, so you may want to perform clipboard operations using this menu. To call the context menu, you can use the ClickR
action applied to the grid control, or send the Application key press to the grid control using the Keys
action. After the context menu is displayed on the screen, you can obtain it using the PopupMenu
property added to the GridDataBoundGrid control by TestComplete. To select items in the context menu, use the Click
or Select
method of the resulting Menu
object.
Using the GridDataBoundGrid Internal Methods
It is possible to copy and paste GridDataBoundGrid cell values by using the grid control's internal methods. The grid’s Model.CutPaste
property provides access to the special GridModelCutPaste
object that lets you perform various clipboard operations with the grid data. For example, to copy the selected cell’s value to the clipboard and paste it from the clipboard, you can use the following statements:
GridObj.Model.CutPaste.Copy()
GridObj.Model.CutPaste.Paste()
GridObj is the scripting object that corresponds to the GridDataBoundGrid control. Note that these methods only work with the currently selected cell. To be able to copy or paste the cell value you need to locate the cell in the grid and select it.
The example below demonstrates how you can copy and paste cell data via the GridDataBoundGrid.Model.CutPaste
object in scripts:
Example
JavaScript, JScript
function Main ()
{
var p, Grid, i;
// Obtain the application process and the grid object
p = Sys.Process ("DataBoundSortByDisplayMember");
Grid = p.WinFormsObject("Form1").WinFormsObject("gridDataBoundGrid1");
// Copy the cell value and paste it to other cells
CopyCellValue (Grid, 1, 2);
Log.Message ("Copied value: " + Sys.Clipboard);
for (i=3; i<=6; i++)
PasteCellValue (Grid, i, 2);
// Set the clipboard contents directly and paste it into the cell
Sys.Clipboard = "Ipoh Coffee";
PasteCellValue (Grid, 10, 2);
}
function CopyCellValue (Grid, RowIndex, ColumnId)
{
SelectCell (Grid, RowIndex, ColumnId);
Grid.Model.CutPaste.Copy();
}
function PasteCellValue (Grid, RowIndex, ColumnId)
{
SelectCell (Grid, RowIndex, ColumnId);
Grid.Model.CutPaste.Paste();
}
function SelectCell (Grid, RowIndex, ColumnId)
{
// Try to navigate to the specified cell
if (Grid.CurrentCell.MoveTo_3 (RowIndex, GetColIndexById(Grid, ColumnId)))
// Make the selected cell visible
Grid.CurrentCell.ScrollInView()
else
Log.Error ("Cell (" + RowIndex + ", " + ColumnId + ") could not be selected.");
}
function GetColIndexById (Grid, ColumnId)
{
if (aqObject.GetVarType(ColumnId) == varOleStr)
return Grid.NameToColIndex(ColumnId)
else
return ColumnId;
}
Python
def Main ():
# Obtain the application process and the grid object
p = Sys.Process ("DataBoundSortByDisplayMember");
Grid = p.WinFormsObject("Form1").WinFormsObject("gridDataBoundGrid1");
# Copy the cell value and paste it to other cells
CopyCellValue (Grid, 1, 2);
Log.Message ("Copied value: " + Sys.Clipboard);
for i in range(3, 6):
PasteCellValue (Grid, i, 2);
# Set the clipboard contents directly and paste it into the cell
Sys.Clipboard = "Ipoh Coffee";
PasteCellValue (Grid, 10, 2);
def CopyCellValue (Grid, RowIndex, ColumnId):
SelectCell (Grid, RowIndex, ColumnId);
Grid.Model.CutPaste.Copy();
def PasteCellValue (Grid, RowIndex, ColumnId):
SelectCell (Grid, RowIndex, ColumnId);
Grid.Model.CutPaste.Paste();
def SelectCell (Grid, RowIndex, ColumnId):
# Try to navigate to the specified cell
if (Grid.CurrentCell.MoveTo_3 (RowIndex, GetColIndexById(Grid, ColumnId))):
# Make the selected cell visible
Grid.CurrentCell.ScrollInView()
else:
Log.Error ("Cell (" + RowIndex + ", " + ColumnId + ") could not be selected.");
def GetColIndexById (Grid, ColumnId):
if (aqObject.GetVarType(ColumnId) == varOleStr):
return Grid.NameToColIndex(ColumnId)
else:
return ColumnId;
VBScript
Sub Main
Dim p, Grid, i
' Obtain the application process and the grid object
Set p = Sys.Process ("DataBoundSortByDisplayMember")
Set Grid = p.WinFormsObject("Form1").WinFormsObject("gridDataBoundGrid1")
' Copy the cell value and paste it to other cells
Call CopyCellValue (Grid, 1, 2)
Call Log.Message ("Copied value: " & Sys.Clipboard)
For i=3 To 6
Call PasteCellValue (Grid, i, 2)
Next
' Set the clipboard contents directly and paste it into the cell
Sys.Clipboard = "Ipoh Coffee"
Call PasteCellValue (Grid, 10, 2)
End Sub
Sub CopyCellValue (Grid, RowIndex, ColumnId)
Call SelectCell (Grid, RowIndex, ColumnId)
Grid.Model.CutPaste.Copy
End Sub
Sub PasteCellValue (Grid, RowIndex, ColumnId)
Call SelectCell (Grid, RowIndex, ColumnId)
Grid.Model.CutPaste.Paste
End Sub
Sub SelectCell (Grid, RowIndex, ColumnId)
' Try to navigate to the specified cell
If Grid.CurrentCell.MoveTo_3 (RowIndex, GetColIndexById(Grid, ColumnId)) Then
' Make the selected cell visible
Grid.CurrentCell.ScrollInView
Else
Call Log.Error ("Cell (" & RowIndex & ", " & ColumnId & ") could not be selected.")
End If
End Sub
Function GetColIndexById (Grid, ColumnId)
If aqObject.GetVarType(ColumnId) = varOleStr Then
GetColIndexById = Grid.NameToColIndex(ColumnId)
Else
GetColIndexById = ColumnId
End If
End Function
DelphiScript
procedure CopyCellValue (Grid, RowIndex, ColumnId); forward;
procedure PasteCellValue (Grid, RowIndex, ColumnId); forward;
procedure SelectCell (Grid, RowIndex, ColumnId); forward;
function GetColIndexById (Grid, ColumnId); forward;
procedure Main;
var p, Grid, i : OleVariant;
begin
// Obtain the application process and the grid object
p := Sys.Process ('DataBoundSortByDisplayMember');
Grid := p.WinFormsObject('Form1').WinFormsObject('gridDataBoundGrid1');
// Copy the cell value and paste it to other cells
CopyCellValue (Grid, 1, 2);
Log.Message ('Copied value: ' + Sys.Clipboard);
for i:=3 to 6 do
PasteCellValue (Grid, i, 2);
// Set the clipboard contents directly and paste it into the cell
Sys.Clipboard := 'Ipoh Coffee';
PasteCellValue (Grid, 10, 2);
end;
procedure CopyCellValue (Grid, RowIndex, ColumnId);
begin
SelectCell (Grid, RowIndex, ColumnId);
Grid.Model.CutPaste.Copy;
end;
procedure PasteCellValue (Grid, RowIndex, ColumnId);
begin
SelectCell (Grid, RowIndex, ColumnId);
Grid.Model.CutPaste.Paste;
end;
procedure SelectCell (Grid, RowIndex, ColumnId);
begin
// Try to navigate to the specified cell
if Grid.CurrentCell.MoveTo_3 (RowIndex, GetColIndexById(Grid, ColumnId)) then
// Make the selected cell visible
Grid.CurrentCell.ScrollInView
else
Log.Error ('Cell (' + aqConvert.VarToStr(RowIndex) + ', ' + aqConvert.VarToStr(ColumnId) + ') could not be selected.');
end;
function GetColIndexById (Grid, ColumnId);
begin
if aqObject.GetVarType(ColumnId) = varOleStr then
Result := Grid.NameToColIndex(ColumnId)
else
Result := ColumnId;
end;
C++Script, C#Script
function Main ()
{
var p, Grid, i;
// Obtain the application process and the grid object
p = Sys["Process"]("DataBoundSortByDisplayMember");
Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("gridDataBoundGrid1");
// Copy the cell value and paste it to other cells
CopyCellValue (Grid, 1, 2);
Log["Message"]("Copied value: " + Sys["Clipboard"]);
for (i=3; i<=6; i++)
PasteCellValue (Grid, i, 2);
// Set the clipboard contents directly and paste it into the cell
Sys["Clipboard"] = "Ipoh Coffee";
PasteCellValue (Grid, 10, 2);
}
function CopyCellValue (Grid, RowIndex, ColumnId)
{
SelectCell (Grid, RowIndex, ColumnId);
Grid["Model"]["CutPaste"]["Copy"]();
}
function PasteCellValue (Grid, RowIndex, ColumnId)
{
SelectCell (Grid, RowIndex, ColumnId);
Grid["Model"]["CutPaste"]["Paste"]();
}
function SelectCell (Grid, RowIndex, ColumnId)
{
// Try to navigate to the specified cell
if (Grid["CurrentCell"]["MoveTo_3"](RowIndex, GetColIndexById(Grid, ColumnId)))
// Make the selected cell visible
Grid["CurrentCell"]["ScrollInView"]()
else
Log["Error"]("Cell (" + RowIndex + ", " + ColumnId + ") could not be selected.");
}
function GetColIndexById (Grid, ColumnId)
{
if (aqObject["GetVarType"](ColumnId) == varOleStr)
return Grid["NameToColIndex"](ColumnId)
else
return ColumnId;
}
See Also
Working With Syncfusion GridDataBoundGrid
Selecting Cells in Syncfusion GridDataBoundGrid
Activating and Closing In-place Editors in Syncfusion GridDataBoundGrid
Obtaining and Setting Cell Values in Syncfusion GridDataBoundGrid