Obtaining and Setting Cell Values in Microsoft DataGrid

Applies to TestComplete 15.46, last modified on January 09, 2023

This topic describes the various approaches that you can use to obtain and change values assigned to the DataGrid cells. Note that before getting or setting the cell value, you need to know in which row and column the desired cell resides. To determine the cell position, you may need to search for the record containing the desired cell within the grid.

To perform these actions, TestComplete should have access to internal objects, properties and methods of the DataGrid control. For this purpose, the .NET Application Support and Microsoft Control Support plugins must be installed and enabled.

When testing Microsoft DataGrid controls, use specific methods and properties of the corresponding MicrosoftDataGrid 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 DataGrid 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.

Obtaining Cell Values

To get the value from a particular grid cell, you can use the wValue property of the MicrosoftDataGrid object. Some .NET values such as Integer, Double, Boolean are OLE-compatible and can be used in scripts directly. Others, such as String, Decimal and DateTime are made OLE-compatible via the OleValue property added to these objects by TestComplete. To work with complex object values, you can use their own properties and methods.

JavaScript, JScript

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

  // Obtain the grid object
  p = Sys.Process ("DataGridSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1");

  // Post data of the 1st row to the log
  for (Col = 0; Col < Grid.wColumnCount; Col++)
    Log.Message ("Cell (0, " + Col + ") value: " +
                 Grid.wValue(0, Col).ToString().OleValue)
}

Python

def Main ():

  # Obtain the grid object
  p = Sys.Process ("DataGridSample")
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1")

  # Post data of the 1st row to the log
  for Col in range(0, Grid.wColumnCount-1):
    Log.Message ("Cell (0, " + Col + ") value: " + \
                 Grid.wValue[0, Col].ToString().OleValue)

VBScript

Sub Main
  Dim p, Grid, Col

  ' Obtain the grid object
  Set p = Sys.Process ("DataGridSample")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1")

  ' Post data of the 1st row to the log
  For Col = 0 To Grid.wColumnCount-1
    Log.Message ("Cell (0, " & Col & ") value: " & _
                 Grid.wValue(0, Col).ToString.OleValue)
  Next
End Sub

DelphiScript

procedure Main;
var p, Grid, Col : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process ('DataGridSample');
  Grid := p.WinFormsObject('Form1').WinFormsObject('dataGrid1');

  // Post data of the 1st row to the log
  for Col := 0 to Grid.wColumnCount-1 do
    Log.Message ('Cell (0, ' + aqConvert.VarToStr(Col) + ') value: ' +
                 Grid.wValue[0, Col].ToString.OleValue)
end;

C++Script, C#Script

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

  // Obtain the grid object
  p = Sys["Process"]("DataGridSample");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("dataGrid1");

  // Post data of the 1st row to the log
  for (Col = 0; Col < Grid["wColumnCount"]; Col++)
    Log["Message"]("Cell (0, " + Col + ") value: " +
                 Grid["wValue"](0, Col)["ToString"]()["OleValue"])
}

Setting Cell Values

There are two general approaches for modifying DataGrid cell values:

  • Simulating the user actions over the cell’s in-place editor, for example, “typing” the new value into the cell.
  • Assign the new value using the MicrosoftDataGrid.wValue property.

Detailed information and script samples for both approaches are provided below.

Simulating the 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. After that, you can “type” the desired value into it using the Keys action applied to the grid control.

The example below demonstrates how you can do this. It “types” new values into grid’s edit boxes.

Example

View description

JavaScript, JScript

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys.Process ("DataGridSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1");

  // Modify the 1st record
  InputCellValue (Grid, 0, "Customer Name", "Robert King");
  InputCellValue (Grid, 0, "Product", "FamilyAlbum");
  InputCellValue (Grid, 0, "Quantity", "2");
  InputCellValue (Grid, 0, "Order Date", "5/21/2007");
  InputCellValue (Grid, 0, "Street", "123 Home Lane");
  InputCellValue (Grid, 0, "City", "Greenville, MS");
  InputCellValue (Grid, 0, "State", "US");
  InputCellValue (Grid, 0, "Zip Code", "12345");
  InputCellValue (Grid, 0, "Card", "VISA");
  InputCellValue (Grid, 0, "Card No", "123123454321");
}

function InputCellValue (Grid, RowIndex, ColumnId, Text)
{
  // Click the cell
  Grid.ClickCell (RowIndex, ColumnId);
  // Input the new value
  Grid.Keys (Text);
}

Python

def Main ():

  # Obtain the grid object
  p = Sys.Process ("DataGridSample")
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1")

  # Modify the 1st record
  InputCellValue (Grid, 0, "Customer Name", "Robert King")
  InputCellValue (Grid, 0, "Product", "FamilyAlbum")
  InputCellValue (Grid, 0, "Quantity", "2")
  InputCellValue (Grid, 0, "Order Date", "5/21/2007")
  InputCellValue (Grid, 0, "Street", "123 Home Lane")
  InputCellValue (Grid, 0, "City", "Greenville, MS")
  InputCellValue (Grid, 0, "State", "US")
  InputCellValue (Grid, 0, "Zip Code", "12345")
  InputCellValue (Grid, 0, "Card", "VISA")
  InputCellValue (Grid, 0, "Card No", "123123454321")

def InputCellValue (Grid, RowIndex, ColumnId, Text):
  # Click the cell
  Grid.ClickCell (RowIndex, ColumnId)
  # Input the new value
  Grid.Keys (Text)

VBScript

Sub Main
  Dim p, Grid

  ' Obtain the grid object
  Set p = Sys.Process ("DataGridSample")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1")

  ' Modify the 1st record
  Call InputCellValue (Grid, 0, "Customer Name", "Robert King")
  Call InputCellValue (Grid, 0, "Product", "FamilyAlbum")
  Call InputCellValue (Grid, 0, "Quantity", "2")
  Call InputCellValue (Grid, 0, "Order Date", "5/21/2007")
  Call InputCellValue (Grid, 0, "Street", "123 Home Lane")
  Call InputCellValue (Grid, 0, "City", "Greenville, MS")
  Call InputCellValue (Grid, 0, "State", "US")
  Call InputCellValue (Grid, 0, "Zip Code", "12345")
  Call InputCellValue (Grid, 0, "Card", "VISA")
  Call InputCellValue (Grid, 0, "Card No", "123123454321")
End Sub

Sub InputCellValue (Grid, RowIndex, ColumnId, Text)
  ' Click the cell
  Call Grid.ClickCell (RowIndex, ColumnId)
  ' Input the new value
  Grid.Keys (Text)
End Sub

DelphiScript

procedure InputCellValue (Grid, RowIndex, ColumnId, Text);
begin
  // Click the cell
  Grid.ClickCell (RowIndex, ColumnId);
  // Input the new value
  Grid.Keys (Text);
end;

procedure Main;
var p, Grid : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process ('DataGridSample');
  Grid := p.WinFormsObject('Form1').WinFormsObject('dataGrid1');

  // Modify the 1st record
  InputCellValue (Grid, 0, 'Customer Name', 'Robert King');
  InputCellValue (Grid, 0, 'Product', 'FamilyAlbum');
  InputCellValue (Grid, 0, 'Quantity', '2');
  InputCellValue (Grid, 0, 'Order Date', '5/21/2007');
  InputCellValue (Grid, 0, 'Street', '123 Home Lane');
  InputCellValue (Grid, 0, 'City', 'Greenville, MS');
  InputCellValue (Grid, 0, 'State', 'US');
  InputCellValue (Grid, 0, 'Zip Code', '12345');
  InputCellValue (Grid, 0, 'Card', 'VISA');
  InputCellValue (Grid, 0, 'Card No', '123123454321');
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys["Process"]("DataGridSample");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("dataGrid1");

  // Modify the 1st record
  InputCellValue (Grid, 0, "Customer Name", "Robert King");
  InputCellValue (Grid, 0, "Product", "FamilyAlbum");
  InputCellValue (Grid, 0, "Quantity", "2");
  InputCellValue (Grid, 0, "Order Date", "5/21/2007");
  InputCellValue (Grid, 0, "Street", "123 Home Lane");
  InputCellValue (Grid, 0, "City", "Greenville, MS");
  InputCellValue (Grid, 0, "State", "US");
  InputCellValue (Grid, 0, "Zip Code", "12345");
  InputCellValue (Grid, 0, "Card", "VISA");
  InputCellValue (Grid, 0, "Card No", "123123454321");
}

function InputCellValue (Grid, RowIndex, ColumnId, Text)
{
  // Click the cell
  Grid["ClickCell"](RowIndex, ColumnId);
  // Input the new value
  Grid["Keys"](Text);
}

Assigning the New Value Programmatically

Another way to modify grid cell values is to use the wValue property of the MicrosoftDataGrid object. This property is read-write, so it can be used to obtain grid cell values as well as to change them:

JavaScript

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys.Process ("DataGridSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1");

  // Modify data of the first row
  Grid.$set("wValue", 0, "Customer Name", "Robert King");
  Grid.$set("wValue", 0, "Product", "FamilyAlbum");
  Grid.$set("wValue", 0, "Quantity", 2);
  Grid.$set("wValue", 0, "Order Date", "5/21/2007");
  Grid.$set("wValue", 0, "Street", "123 Home Lane");
  Grid.$set("wValue", 0, "City", "Greenville, MS");
  Grid.$set("wValue", 0, "State", "US");
  Grid.$set("wValue", 0, "Zip Code", "12345");
  Grid.$set("wValue", 0, "Card", "VISA");
  Grid.$set("wValue", 0, "Card No", "123123454321");
}

JScript

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys.Process ("DataGridSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1");

  // Modify data of the first row
  Grid.wValue(0, "Customer Name") = "Robert King";
  Grid.wValue(0, "Product") = "FamilyAlbum";
  Grid.wValue(0, "Quantity") = 2;
  Grid.wValue(0, "Order Date") = "5/21/2007";
  Grid.wValue(0, "Street") = "123 Home Lane";
  Grid.wValue(0, "City") = "Greenville, MS";
  Grid.wValue(0, "State") = "US";
  Grid.wValue(0, "Zip Code") = "12345";
  Grid.wValue(0, "Card") = "VISA";
  Grid.wValue(0, "Card No") = "123123454321";
}

Python

def Main ():

  # Obtain the grid object
  p = Sys.Process ("DataGridSample")
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1")

  # Modify data of the first row
  Grid.wValue(0, "Customer Name") = "Robert King"
  Grid.wValue(0, "Product") = "FamilyAlbum"
  Grid.wValue(0, "Quantity") = 2
  Grid.wValue(0, "Order Date") = "5/21/2007"
  Grid.wValue(0, "Street") = "123 Home Lane"
  Grid.wValue(0, "City") = "Greenville, MS"
  Grid.wValue(0, "State") = "US"
  Grid.wValue(0, "Zip Code") = "12345"
  Grid.wValue(0, "Card") = "VISA"
  Grid.wValue(0, "Card No") = "123123454321"

VBScript

Sub Main
  Dim p, Grid

  ' Obtain the grid object
  Set p = Sys.Process ("DataGridSample")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1")

  ' Modify data of the first row
  Grid.wValue(0, "Customer Name") = "Robert King"
  Grid.wValue(0, "Product") = "FamilyAlbum"
  Grid.wValue(0, "Quantity") = 2
  Grid.wValue(0, "Order Date") = "5/21/2007"
  Grid.wValue(0, "Street") = "123 Home Lane"
  Grid.wValue(0, "City") = "Greenville, MS"
  Grid.wValue(0, "State") = "US"
  Grid.wValue(0, "Zip Code") = "12345"
  Grid.wValue(0, "Card") = "VISA"
  Grid.wValue(0, "Card No") = "123123454321"
End Sub

DelphiScript

procedure Main;
var p, Grid : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process ('DataGridSample');
  Grid := p.WinFormsObject('Form1').WinFormsObject('dataGrid1');

  // Modify data of the first row
  Grid.wValue[0, 'Customer Name'] := 'Robert King';
  Grid.wValue[0, 'Product'] := 'FamilyAlbum';
  Grid.wValue[0, 'Quantity'] := 2;
  Grid.wValue[0, 'Order Date'] := '5/21/2007';
  Grid.wValue[0, 'Street'] := '123 Home Lane';
  Grid.wValue[0, 'City'] := 'Greenville, MS';
  Grid.wValue[0, 'State'] := 'US';
  Grid.wValue[0, 'Zip Code'] := '12345';
  Grid.wValue[0, 'Card'] := 'VISA';
  Grid.wValue[0, 'Card No'] := '123123454321';
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys["Process"]("DataGridSample");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("dataGrid1");

  // Modify data of the first row
  Grid["wValue"](0, "Customer Name") = "Robert King";
  Grid["wValue"](0, "Product") = "FamilyAlbum";
  Grid["wValue"](0, "Quantity") = 2;
  Grid["wValue"](0, "Order Date") = "5/21/2007";
  Grid["wValue"](0, "Street") = "123 Home Lane";
  Grid["wValue"](0, "City") = "Greenville, MS";
  Grid["wValue"](0, "State") = "US";
  Grid["wValue"](0, "Zip Code") = "12345";
  Grid["wValue"](0, "Card") = "VISA";
  Grid["wValue"](0, "Card No") = "123123454321";
}

See Also

Working With Microsoft DataGrid
wValue Property (Grid Controls)
Selecting Cells in Microsoft DataGrid
Copying and Pasting Cell Values in Microsoft DataGrid

Highlight search results