This topic describes the various approaches that you can use to obtain and change values stored in the GridControl cells. Note that before getting or setting the cell value, you need to know in which row and column the desired cell resides. For example, you can search for the record containing the desired cell within the grid. For more information on identifying rows and columns, see Accessing Rows, Columns and Cells in Syncfusion GridControl.
To perform these actions, TestComplete should have access to internal objects, properties and methods of the GridControl object. For this purpose, the .NET Application Support plugin must be installed and enabled. When testing Syncfusion GridControl controls, use specific methods and properties of the corresponding |
Obtaining Cell Values
To get the value stored in a particular grid cell, you can use the following statements:
GridObj.Item( RowIndex, ColIndex ).CellValue
GridObj.Item( RowIndex, ColIndex ).Text.OleValue
GridObj is the scripting object corresponding to the GridControl, RowIndex and ColIndex are zero-based absolute indexes of the cell’s row and column. For more information on identifying rows and columns, see Accessing Rows, Columns and Cells in Syncfusion GridControl.
The CellValue
property returns the .NET object corresponding to the cell value. Some “simple” values, for example, Integer
, Boolean
and others, are OLE-compatible and thus can be used in scripts directly. To get OLE-compatible values of String
, Decimal
, DateTime
and enumeration values, you should use the OleValue
property added to these objects by TestComplete. To work with complex object values, use their internal properties and methods.
The Text
property, in its turn, returns the text representation of the cell value. We use the OleValue
property of the returned string to make it OLE-compatible. Note, however, that in certain cases the text representation of the cell value may significantly differ from the actual text displayed in the cell. For example, the GridControl may display values with a special formatting applied. Also, it may be configured to display date values as “Tuesday, March 19, 2007” instead of “3/19/2007”, currency values as $123,456.78 instead of 123456.78, and so on. In this case, you will most likely want to get the actual text displayed in grid cells rather than the cell values. You can do this using the FormattedText
property:
GridObj.Item( RowIndex, ColIndex ).FormattedText.OleValue
Below is an example that demonstrates how you can get the GridControl cell values and display text.
Example
JavaScript, JScript
function Main ()
{
var p, Grid;
// Obtain the application process and the grid object
p = Sys.Process("GridControlSort");
Grid = p.WinFormsObject("Form1").WinFormsObject("gridControl1");;
// Log values and text of some cells
Log.Message ("Numeric cell value: " + GetCellValue(Grid, 1, 1).ToString().OleValue);
Log.Message ("Numeric cell text: " + GetCellText(Grid, 1, 1));
Log.Message ("DateTime cell value: " + GetCellValue(Grid, 1, 2).ToString().OleValue);
Log.Message ("DateTime cell text: " + GetCellText(Grid, 1, 2));
Log.Message ("FormulaCell cell value: " + GetCellValue(Grid, 1, 4).ToString().OleValue);
Log.Message ("FormulaCell cell text: " + GetCellText(Grid, 1, 4));
}
function GetCellValue (Grid, RowIndex, ColIndex)
{
return Grid.Item(RowIndex, ColIndex).CellValue;
}
function GetCellText (Grid, RowIndex, ColIndex)
{
return Grid.Item(RowIndex, ColIndex).FormattedText.OleValue;
}
Python
def Main ():
# Obtain the application process and the grid object
p = Sys.Process("GridControlSort")
Grid = p.WinFormsObject("Form1").WinFormsObject("gridControl1")
# Log values and text of some cells
Log.Message ("Numeric cell value: " + GetCellValue(Grid, 1, 1).ToString().OleValue)
Log.Message ("Numeric cell text: " + GetCellText(Grid, 1, 1))
Log.Message ("DateTime cell value: " + GetCellValue(Grid, 1, 2).ToString().OleValue)
Log.Message ("DateTime cell text: " + GetCellText(Grid, 1, 2))
Log.Message ("FormulaCell cell value: " + GetCellValue(Grid, 1, 4).ToString().OleValue)
Log.Message ("FormulaCell cell text: " + GetCellText(Grid, 1, 4))
def GetCellValue (Grid, RowIndex, ColIndex):
return Grid.Item[RowIndex, ColIndex].CellValue
def GetCellText (Grid, RowIndex, ColIndex):
return Grid.Item[RowIndex, ColIndex].FormattedText.OleValue
VBScript
Sub Main
Dim p, Grid
' Obtain the application process and the grid object
Set p = Sys.Process("GridControlSort")
Set Grid = p.WinFormsObject("Form1").WinFormsObject("gridControl1")
' Log values and text of some cells
Call Log.Message ("Numeric cell value: " & GetCellValue(Grid, 1, 1).ToString().OleValue)
Call Log.Message ("Numeric cell text: " & GetCellText(Grid, 1, 1))
Call Log.Message ("DateTime cell value: " & GetCellValue(Grid, 1, 2).ToString().OleValue)
Call Log.Message ("DateTime cell text: " & GetCellText(Grid, 1, 2))
Call Log.Message ("FormulaCell cell value: " & GetCellValue(Grid, 1, 4).ToString().OleValue)
Call Log.Message ("FormulaCell cell text: " & GetCellText(Grid, 1, 4))
End Sub
Function GetCellValue (Grid, RowIndex, ColIndex)
Set GetCellValue = Grid.Item(RowIndex, ColIndex).CellValue
End Function
Function GetCellText (Grid, RowIndex, ColIndex)
GetCellText = Grid.Item(RowIndex, ColIndex).FormattedText.OleValue
End Function
DelphiScript
function GetCellValue (Grid, RowIndex, ColIndex);
begin
Result := Grid.Item[RowIndex, ColIndex].CellValue;
end;
function GetCellText (Grid, RowIndex, ColIndex);
begin
Result := Grid.Item[RowIndex, ColIndex].FormattedText.OleValue;
end;
procedure Main;
var p, Grid : OleVariant;
begin
// Obtain the application process and the grid object
p := Sys.Process('GridControlSort');
Grid := p.WinFormsObject('Form1').WinFormsObject('gridControl1');
// Log values and text of some cells
Log.Message ('Numeric cell value: ' + GetCellValue(Grid, 1, 1).ToString().OleValue);
Log.Message ('Numeric cell text: ' + GetCellText(Grid, 1, 1));
Log.Message ('DateTime cell value: ' + GetCellValue(Grid, 1, 2).ToString().OleValue);
Log.Message ('DateTime cell text: ' + GetCellText(Grid, 1, 2));
Log.Message ('FormulaCell cell value: ' + GetCellValue(Grid, 1, 4).ToString().OleValue);
Log.Message ('FormulaCell cell text: ' + GetCellText(Grid, 1, 4));
end;
C++Script, C#Script
function Main ()
{
var p, Grid;
// Obtain the application process and the grid object
p = Sys["Process"]("GridControlSort");
Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("gridControl1");;
// Log values and text of some cells
Log["Message"]("Numeric cell value: " + GetCellValue(Grid, 1, 1)["ToString"]()["OleValue"]);
Log["Message"]("Numeric cell text: " + GetCellText(Grid, 1, 1));
Log["Message"]("DateTime cell value: " + GetCellValue(Grid, 1, 2)["ToString"]()["OleValue"]);
Log["Message"]("DateTime cell text: " + GetCellText(Grid, 1, 2));
Log["Message"]("FormulaCell cell value: " + GetCellValue(Grid, 1, 4)["ToString"]()["OleValue"]);
Log["Message"]("FormulaCell cell text: " + GetCellText(Grid, 1, 4));
}
function GetCellValue (Grid, RowIndex, ColIndex)
{
return Grid["Item"](RowIndex, ColIndex)["CellValue"];
}
function GetCellText (Grid, RowIndex, ColIndex)
{
return Grid["Item"](RowIndex, ColIndex)["FormattedText"]["OleValue"];
}
Setting Cell Values
There are two general approaches for modifying GridControl cell values:
- Simulating the user actions over the grid cell’s in-place editor, for example, “typing” the new value into the cell.
- Using GridControl’s internal properties and methods.
Detailed information and script samples for both approaches are provided below. These approaches work well for most types of in-place editors. However, if the tested application's GridControl uses specific in-place editors, for example, check boxes, radio buttons, text boxes with embedded buttons, drop-down editors and others, you may need to work with them in a custom way. For more information on how to work with these cells, see Working With Specific Cells in Syncfusion GridControl.
Simulating User Input
To modify grid cell values, you can input the new values directly in grid cells. Note that before that, you should locate the row and column in which the cell resides. For example, you can search for the row containing the cell with the desired value. After you have determined the cell position, you should select the desired cell within the grid and activate the cell’s in-place editor. When the cell’s is in the edit mode, you can “type” the desired value into it using the Keys
action applied to the grid control.
Below is an example that illustrates how you can do it. It “types” new values into grid cells.
Example
JavaScript, JScript
function Main ()
{
var p, Grid;
// Obtain the application process and the grid object
p = Sys.Process("GridControlSort");
Grid = p.WinFormsObject("Form1").WinFormsObject("gridControl1");
// Modify cell values
InputCellValue (Grid, 1, 1, "1234");
InputCellValue (Grid, 1, 2, "20 Apr 2007");
InputCellValue (Grid, 1, 3, "567");
InputCellValue (Grid, 1, 5, "New Text");
}
function InputCellValue (Grid, RowIndex, ColIndex, Value)
{
// Select the cell and activate its in-place editor
ClickCell (Grid, RowIndex, ColIndex);
ActivateCellEditor (Grid);
// Type the new value
Grid.Keys ("[Home]![End]" + Value);
// Save the changes
CloseCellEditor (Grid);
}
function ClickCell (Grid, RowIndex, ColIndex)
{
// Make cell visible
Grid.ScrollCellInView_3 (RowIndex, ColIndex);
// Get the cell coordinates
var rect = Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell(RowIndex, ColIndex));
Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2);
}
function ActivateCellEditor (Grid)
{
if (! Grid.CurrentCell.IsEditing)
Grid.Keys ("[F2]")
}
function CloseCellEditor (Grid)
{
Grid.Keys ("[Enter]");
}
Python
def Main ():
# Obtain the application process and the grid object
p = Sys.Process("GridControlSort")
Grid = p.WinFormsObject("Form1").WinFormsObject("gridControl1")
# Modify cell values
InputCellValue (Grid, 1, 1, "1234")
InputCellValue (Grid, 1, 2, "20 Apr 2007")
InputCellValue (Grid, 1, 3, "567")
InputCellValue (Grid, 1, 5, "New Text")
def InputCellValue (Grid, RowIndex, ColIndex, Value):
# Select the cell and activate its in-place editor
ClickCell (Grid, RowIndex, ColIndex)
ActivateCellEditor (Grid)
# Type the new value
Grid.Keys ("[Home]![End]" + Value)
# Save the changes
CloseCellEditor (Grid)
def ClickCell (Grid, RowIndex, ColIndex):
# 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 ActivateCellEditor (Grid):
if not Grid.CurrentCell.IsEditing:
Grid.Keys ("[F2]")
def CloseCellEditor (Grid):
Grid.Keys ("[Enter]")
VBScript
Sub Main
Dim p, Grid
' Obtain the application process and the grid object
Set p = Sys.Process("GridControlSort")
Set Grid = p.WinFormsObject("Form1").WinFormsObject("gridControl1")
' Modify cell values
Call InputCellValue (Grid, 1, 1, "1234")
Call InputCellValue (Grid, 1, 2, "20 Apr 2007")
Call InputCellValue (Grid, 1, 3, "567")
Call InputCellValue (Grid, 1, 5, "New Text")
End Sub
Sub InputCellValue (Grid, RowIndex, ColIndex, Value)
' Select the cell and activate its in-place editor
Call ClickCell (Grid, RowIndex, ColIndex)
Call ActivateCellEditor (Grid)
' Type the new value
Call Grid.Keys ("[Home]![End]" & Value)
' Save the changes
Call CloseCellEditor (Grid)
End Sub
Sub ClickCell (Grid, RowIndex, ColIndex)
Dim rect
' 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
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
DelphiScript
procedure InputCellValue (Grid, RowIndex, ColIndex, Value); forward;
procedure ClickCell (Grid, RowIndex, ColIndex); forward;
procedure ActivateCellEditor (Grid); forward;
procedure CloseCellEditor (Grid); forward;
procedure Main;
var p, Grid : OleVariant;
begin
// Obtain the application process and the grid object
p := Sys.Process('GridControlSort');
Grid := p.WinFormsObject('Form1').WinFormsObject('gridControl1');
// Modify cell values
InputCellValue (Grid, 1, 1, '1234');
InputCellValue (Grid, 1, 2, '20 Apr 2007');
InputCellValue (Grid, 1, 3, '567');
InputCellValue (Grid, 1, 5, 'New Text');
end;
procedure InputCellValue (Grid, RowIndex, ColIndex, Value);
begin
// Select the cell and activate its in-place editor
ClickCell (Grid, RowIndex, ColIndex);
ActivateCellEditor (Grid);
// Type the new value
Grid.Keys ('[Home]![End]' + Value);
// Save the changes
CloseCellEditor (Grid);
end;
procedure ClickCell (Grid, RowIndex, ColIndex);
var rect : OleVariant;
begin
// 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;
procedure ActivateCellEditor (Grid);
begin
if not Grid.CurrentCell.IsEditing then
Grid.Keys ('[F2]');
end;
procedure CloseCellEditor (Grid);
begin
Grid.Keys ('[Enter]');
end;
C++Script, C#Script
function Main ()
{
var p, Grid;
// Obtain the application process and the grid object
p = Sys["Process"]("GridControlSort");
Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("gridControl1");
// Modify cell values
InputCellValue (Grid, 1, 1, "1234");
InputCellValue (Grid, 1, 2, "20 Apr 2007");
InputCellValue (Grid, 1, 3, "567");
InputCellValue (Grid, 1, 5, "New Text");
}
function InputCellValue (Grid, RowIndex, ColIndex, Value)
{
// Select the cell and activate its in-place editor
ClickCell (Grid, RowIndex, ColIndex);
ActivateCellEditor (Grid);
// Type the new value
Grid["Keys"]("[Home]![End]" + Value);
// Save the changes
CloseCellEditor (Grid);
}
function ClickCell (Grid, RowIndex, ColIndex)
{
// Make cell visible
Grid["ScrollCellInView_3"](RowIndex, ColIndex);
// Get the cell coordinates
var rect = Grid["RangeInfoToRectangle"](Grid["GridCellsRange"]["Cell"](RowIndex, ColIndex));
Grid["Click"](rect["X"] + rect["Width"]/2, rect["Y"] + rect["Height"]/2);
}
function ActivateCellEditor (Grid)
{
if (! Grid["CurrentCell"]["IsEditing"])
Grid["Keys"]("[F2]")
}
function CloseCellEditor (Grid)
{
Grid["Keys"]("[Enter]");
}
A possible alternative to typing data into the cell is to paste it from the clipboard. For more information on how to do this, see Copying and Pasting Cell Values in Syncfusion GridControl.
Using GridControl Internal Methods
It is possible to set grid cell values using internal properties of the GridControl object. The CellValue
, Text
and FormattedText
properties described in the Obtaining Cell Values section above can be used to get the cell values (displayed text) as well as to modify them. So, you can use the Value
property to specify the cell value, the Text
property to specify the cell value by its string representation, and the FormattedText
property to set the new cell value that corresponds to the specified formatted text.
Note that when using the Value
property to modify the cell value of a complex type, such as System.DateTime
, System.Drawing.Color
and others, you need to create an instance of the corresponding .NET class. For more information on how to do this, see Calling Functions From .NET Assemblies. As an alternative, you can use the Text
or FormattedText
properties to specify the desired value by its text representation. The specified string will be parsed and converted to the cell value.
Below is an example that demonstrates how you can set grid cell values from scripts.
Example
JavaScript, JScript
function Main ()
{
var p, Grid;
// Obtain the application process and the grid object
p = Sys.Process("GridControlSort");
Grid = p.WinFormsObject("Form1").WinFormsObject("gridControl1");
// Modify the cells' values by parsing the specified formatted text
SetCellText (Grid, 1, 1, "1234");
SetCellText (Grid, 1, 2, "20 Apr 2007");
SetCellText (Grid, 1, 3, "567");
SetCellText (Grid, 1, 4, "= (A0 - C0) * F0");
SetCellText (Grid, 1, 5, "New Text");
// Modify the cells' values themselves
SetCellValue (Grid, 2, 1, 256);
SetCellValue (Grid, 2, 2, CreateDateTime (2007, 5, 1, 0, 0, 0));
SetCellValue (Grid, 2, 5, "Another text");
}
function SetCellValue (Grid, RowIndex, ColIndex, Value)
{
Grid.Item(RowIndex, ColIndex).CellValue = Value;
}
function SetCellText (Grid, RowIndex, ColIndex, Text)
{
Grid.Item(RowIndex, ColIndex).FormattedText = Text;
}
function CreateDateTime (Year, Month, Day, Hour, Minute, Second)
{
return dotNET.System.DateTime.zctor_5 (Year, Month, Day, Hour, Minute, Second);
}
Python
def Main ():
# Obtain the application process and the grid object
p = Sys.Process("GridControlSort")
Grid = p.WinFormsObject("Form1").WinFormsObject("gridControl1")
# Modify the cells' values by parsing the specified formatted text
SetCellText (Grid, 1, 1, "1234")
SetCellText (Grid, 1, 2, "20 Apr 2007")
SetCellText (Grid, 1, 3, "567")
SetCellText (Grid, 1, 4, "= (A0 - C0) * F0")
SetCellText (Grid, 1, 5, "New Text")
# Modify the cells' values themselves
SetCellValue (Grid, 2, 1, 256)
SetCellValue (Grid, 2, 2, CreateDateTime (2007, 5, 1, 0, 0, 0))
SetCellValue (Grid, 2, 5, "Another text")
def SetCellValue (Grid, RowIndex, ColIndex, Value):
Grid.Item[RowIndex, ColIndex].CellValue = Value
def SetCellText (Grid, RowIndex, ColIndex, Text):
Grid.Item[RowIndex, ColIndex].FormattedText = Text
def CreateDateTime (Year, Month, Day, Hour, Minute, Second):
return dotNET.System.DateTime.zctor_5 (Year, Month, Day, Hour, Minute, Second)
VBScript
Sub Main
Dim p, Grid
' Obtain the application process and the grid object
Set p = Sys.Process("GridControlSort")
Set Grid = p.WinFormsObject("Form1").WinFormsObject("gridControl1")
' Modify the cells' values by parsing the specified formatted text
Call SetCellText (Grid, 1, 1, "1234")
Call SetCellText (Grid, 1, 2, "20 Apr 2007")
Call SetCellText (Grid, 1, 3, "567")
Call SetCellText (Grid, 1, 4, "= (A0 - C0) * F0")
Call SetCellText (Grid, 1, 5, "New Text")
' Modify the cells' values themselves
Call SetCellValue (Grid, 2, 1, 256)
Call SetCellValue (Grid, 2, 2, CreateDateTime (2007, 5, 1, 0, 0, 0))
Call SetCellValue (Grid, 2, 5, "Another text")
End Sub
Sub SetCellValue (Grid, RowIndex, ColIndex, Value)
If aqObject.GetVarType (Value) = varOleDispatch Then
Set Grid.Item(RowIndex, ColIndex).CellValue = Value
Else
Grid.Item(RowIndex, ColIndex).CellValue = Value
End If
End Sub
Sub SetCellText (Grid, RowIndex, ColIndex, Text)
Grid.Item(RowIndex, ColIndex).FormattedText = Text
End Sub
Function CreateDateTime (Year, Month, Day, Hour, Minute, Second)
Set CreateDateTime = dotNET.System.DateTime.zctor_5 (Year, Month, Day, Hour, Minute, Second)
End Function
DelphiScript
procedure SetCellValue (Grid, RowIndex, ColIndex, Value); forward;
procedure SetCellText (Grid, RowIndex, ColIndex, Text); forward;
function CreateDateTime (Year, Month, Day, Hour, Minute, Second); forward;
procedure Main;
var p, Grid : OleVariant;
begin
// Obtain the application process and the grid object
p := Sys.Process('GridControlSort');
Grid := p.WinFormsObject('Form1').WinFormsObject('gridControl1');
// Modify the cells' values by parsing the specified formatted text
SetCellText (Grid, 1, 1, '1234');
SetCellText (Grid, 1, 2, '20 Apr 2007');
SetCellText (Grid, 1, 3, '567');
SetCellText (Grid, 1, 4, '= (A0 - C0) * F0');
SetCellText (Grid, 1, 5, 'New Text');
// Modify the cells' values themselves
SetCellValue (Grid, 2, 1, 256);
SetCellValue (Grid, 2, 2, CreateDateTime (2007, 5, 1, 0, 0, 0));
SetCellValue (Grid, 2, 5, 'Another text');
end;
procedure SetCellValue (Grid, RowIndex, ColIndex, Value);
begin
Grid.Item[RowIndex, ColIndex].CellValue := Value;
end;
procedure SetCellText (Grid, RowIndex, ColIndex, Text);
begin
Grid.Item[RowIndex, ColIndex].FormattedText := Text;
end;
function CreateDateTime (Year, Month, Day, Hour, Minute, Second);
begin
Result := dotNET.System.DateTime.zctor_5 (Year, Month, Day, Hour, Minute, Second);
end;
C++Script, C#Script
function Main ()
{
var p, Grid;
// Obtain the application process and the grid object
p = Sys["Process"]("GridControlSort");
Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("gridControl1");
// Modify the cells' values by parsing the specified formatted text
SetCellText (Grid, 1, 1, "1234");
SetCellText (Grid, 1, 2, "20 Apr 2007");
SetCellText (Grid, 1, 3, "567");
SetCellText (Grid, 1, 4, "= (A0 - C0) * F0");
SetCellText (Grid, 1, 5, "New Text");
// Modify the cells' values themselves
SetCellValue (Grid, 2, 1, 256);
SetCellValue (Grid, 2, 2, CreateDateTime (2007, 5, 1, 0, 0, 0));
SetCellValue (Grid, 2, 5, "Another text");
}
function SetCellValue (Grid, RowIndex, ColIndex, Value)
{
Grid["Item"](RowIndex, ColIndex)["CellValue"] = Value;
}
function SetCellText (Grid, RowIndex, ColIndex, Text)
{
Grid["Item"](RowIndex, ColIndex)["FormattedText"] = Text;
}
function CreateDateTime (Year, Month, Day, Hour, Minute, Second)
{
return dotNET["System"]["DateTime"]["zctor_5"](Year, Month, Day, Hour, Minute, Second);
}
Note: | Modifying the cell value programmatically using GridControl internal properties and methods does not involve any user interaction with the grid control and thus will not trigger the corresponding events. That is why you may find that simulating user actions over the grid is more suitable to your needs. |
See Also
Working With Syncfusion GridControl
Accessing Rows, Columns and Cells in Syncfusion GridControl
Selecting Cells in Syncfusion GridControl
Activating and Closing In-place Editors in Syncfusion GridControl
Working With Specific Cells in Syncfusion GridControl
Copying and Pasting Cell Values in Syncfusion GridControl