Obtaining and Setting Cell Values in Microsoft DataGridView

Applies to TestComplete 12.60, last modified on September 17, 2018

This topic describes the various approaches that you can use to obtain and change values assigned to the DataGridView 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 DataGridView control. For this purpose, the .NET Application Support and Microsoft Control Support plugins must be installed and enabled.

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

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

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

  // Post data of the 1st row to the log
  for (let Col = 0; Col < Grid.wColumnCount; Col++)
  {
    // Get the cell value
    CellValue = Grid.wValue (0, Col);
    // Check if the value is of the simple or complex type
    if (equal(aqObject.GetVarType (CellValue)), varDispatch)
      Log.Message ("Cell (0, " + Col + ") value: " + CellValue.ToString().OleValue)
    else
      Log.Message ("Cell (0, " + Col + ") value: " + CellValue)
  }
}

JScript

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

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

  // Post data of the 1st row to the log
  for (Col = 0; Col < Grid.wColumnCount; Col++)
  {
    // Get the cell value
    CellValue = Grid.wValue (0, Col);
    // Check if the value is of the simple or complex type
    if (aqObject.GetVarType (CellValue) == varDispatch)
      Log.Message ("Cell (0, " + Col + ") value: " + CellValue.ToString().OleValue)
    else
      Log.Message ("Cell (0, " + Col + ") value: " + CellValue)
  }
}

Python

def Main ():

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

  # Post data of the 1st row to the log
  for Col in range(0, Grid.wColumnCount):
    # Get the cell value
    CellValue = Grid.wValue [0, Col]
    # Check if the value is of the simple or complex type
    if (aqObject.GetVarType (CellValue) == varDispatch):
      Log.Message ("Cell (0, " + Col + ") value: " + CellValue.ToString().OleValue)
    else:
      Log.Message ("Cell (0, " + Col + ") value: " + CellValue)

VBScript

Sub Main
  Dim p, Grid, Col, CellValue

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

  ' Post data of the 1st row to the log
  For Col = 0 To Grid.wColumnCount-1
    ' Check if the value is of the simple or complex type
    If aqObject.GetVarType (Grid.wValue (0, Col)) = varDispatch Then
      Set CellValue = Grid.wValue (0, Col)
      Log.Message ("Cell (0, " & Col & ") value: " & CellValue.ToString().OleValue)
    Else
      CellValue = Grid.wValue (0, Col)
      Log.Message ("Cell (0, " & Col + ") value: " & CellValue)
    End If
  Next
End Sub

DelphiScript

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

  // Post data of the 1st row to the log
  for Col := 0 to Grid.wColumnCount-1 do
  begin
    // Get the cell value
    CellValue := Grid.wValue [0, Col];
    // Check if the value is of the simple or complex type
    if aqObject.GetVarType (CellValue) = varDispatch then
      Log.Message ('Cell (0, ' + aqConvert.VarToStr(Col) + ') value: ' + CellValue.ToString().OleValue)
    else
      Log.Message ('Cell (0, ' + aqConvert.VarToStr(Col) + ') value: ' + aqConvert.VarToStr(CellValue))
  end
end;

C++Script, C#Script

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

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

  // Post data of the 1st row to the log
  for (Col = 0; Col < Grid["wColumnCount"]; Col++)
  {
    // Get the cell value
    CellValue = Grid["wValue"](0, Col);
    // Check if the value is of the simple or complex type
    if (aqObject["GetVarType"] (CellValue) == varDispatch)
      Log["Message"]("Cell (0, " + Col + ") value: " + CellValue["ToString"]()["OleValue"])
    else
      Log["Message"]("Cell (0, " + Col + ") value: " + CellValue)
  }
}

Alternatively, you can get the value of a particular grid cell using the Value property of a DataGridView cell object:

gridObj.Item( ColIndex, RowIndex ).Value

In these statements, gridObj is the object corresponding to the DataGridView control; RowIndex and ColIndex are the zero-based indexes of the cell’s row and column.

In certain cases, the cell value may significantly differ from the actual text displayed in the cell. For example, the DataGridView control may display values with a special formatting applied: date values can be displayed 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 FormattedValue property of the grid cell object:

gridObj.Item( ColIndex, RowIndex ).FormattedValue

The example below demonstrates how you can use this property to get the cell’s display text.

Example

View description

JavaScript

function Main ()
{
  var p, Grid;

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

  // Post data of the first row to the log
  for (let Col = 0; Col < Grid.wColumnCount; Col++)
    Log.Message ("Cell (0, " + Col + ") text: " + GetCellText (Grid, 0, Col));
}

function GetCellText (Grid, RowIndex, ColumnId)
{
  // Get the column index
  let ColIndex = GetColIndexById (Grid, ColumnId);
  // Get the cell value
  return Grid.Item(ColIndex, RowIndex).FormattedValue.ToString().OleValue;
}

function GetColIndexById (Grid, ColumnId)
{
  // Check if the column is specified by caption or index
  if (equal(aqObject.GetVarType(ColumnId), varOleStr))
  {
    // Search for the column by its caption
    for (let i=0; i<Grid.ColumnCount; i++)
      if (equal(Grid.Columns.Item(i).HeaderText.OleValue, ColumnId))
        return i; // Column is found

    return -1; // Column is not found
  }
  else
    // The column is specified by index; return this index
    return ColumnId;
}

JScript

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

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

  // Post data of the first row to the log
  for (Col = 0; Col < Grid.wColumnCount; Col++)
    Log.Message ("Cell (0, " + Col + ") text: " + GetCellText (Grid, 0, Col));
}

function GetCellText (Grid, RowIndex, ColumnId)
{
  // Get the column index
  var ColIndex = GetColIndexById (Grid, ColumnId);
  // Get the cell value
  return Grid.Item(ColIndex, RowIndex).FormattedValue.ToString().OleValue;
}

function GetColIndexById (Grid, ColumnId)
{
  // Check if the column is specified by caption or index
  if (aqObject.GetVarType(ColumnId) == varOleStr)
  {
    // Search for the column by its caption
    for (i=0; i<Grid.ColumnCount; i++)
      if (Grid.Columns.Item(i).HeaderText.OleValue == ColumnId)
        return i; // Column is found

    return -1; // Column is not found
  }
  else
    // The column is specified by index; return this index
    return ColumnId;
}

Python

def Main ():

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

  # Post data of the first row to the log
  for Col in range(0, Grid.wColumnCount-1):
    Log.Message ("Cell (0, " + Col + ") text: " + GetCellText (Grid, 0, Col))

def GetCellText (Grid, RowIndex, ColumnId):
  # Get the column index
  ColIndex = GetColIndexById (Grid, ColumnId)
  # Get the cell value
  return Grid.Item(ColIndex, RowIndex).FormattedValue.ToString().OleValue

def GetColIndexById (Grid, ColumnId):
  # Check if the column is specified by caption or index
  if (aqObject.GetVarType(ColumnId) == varOleStr):
    # Search for the column by its caption
    for i in range(0, Grid.ColumnCount-1):
      if (Grid.Columns.Item[i].HeaderText.OleValue == ColumnId):
        return i # Column is found

    return -1 # Column is not found
  else:
    # The column is specified by index return this index
    return ColumnId

VBScript

Sub Main
  Dim p, Grid, Col

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

  ' Post data of the first row to the log
  For Col = 0 To Grid.wColumnCount-1
    Log.Message ("Cell (0, " & Col & ") text: " & GetCellText (Grid, 0, Col))
  Next
End Sub

Function GetCellText (Grid, RowIndex, ColumnId)
  Dim ColIndex
  ' Get the column index
  ColIndex = GetColIndexById (Grid, ColumnId)
  ' Get the cell value
  GetCellText = Grid.Item(ColIndex, RowIndex).FormattedValue.ToString().OleValue
End Function

Function GetColIndexById (Grid, ColumnId)
  ' Check if the column is specified by caption or index
  If aqObject.GetVarType(ColumnId) = varOleStr Then
  
    ' Search for the column by its caption
    For i=0 To Grid.ColumnCount-1
      If Grid.Columns.Item(i).HeaderText.OleValue = ColumnId Then
        GetColIndexById = i ' Column is found
        Exit Function
      End If
    Next
    GetColIndexById = -1 ' Column is not found
  Else
    ' The column is specified by index return this index
    GetColIndexById = ColumnId
  End If
End Function

DelphiScript

function GetCellText (Grid, RowIndex, ColumnId); forward;
function GetColIndexById (Grid, ColumnId); forward;

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

  // Post data of the first row to the log
  for Col := 0 to Grid.wColumnCount-1 do
    Log.Message ('Cell (0, ' + aqConvert.VarToStr(Col) + ') text: ' + GetCellText (Grid, 0, Col));
end;

function GetCellText (Grid, RowIndex, ColumnId);
var ColIndex : OleVariant;
begin
  // Get the column index
  ColIndex := GetColIndexById (Grid, ColumnId);
  // Get the cell value
  Result := Grid.Item[ColIndex, RowIndex].FormattedValue.ToString().OleValue;
end;

function GetColIndexById (Grid, ColumnId);
var i: OleVariant;
begin
  // Check if the column is specified by caption or index
  if aqObject.GetVarType(ColumnId) = varOleStr then
  begin
    // Search for the column by its caption
    for i:=0 to Grid.ColumnCount-1 do
      if Grid.Columns.Item[i].HeaderText.OleValue = ColumnId then
      begin
        Result := i; // Column is found
        Exit;
      end;

    Result := -1; // Column is not found
  end
  else
    // The column is specified by index; return this index
    Result := ColumnId;
end;

C++Script, C#Script

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

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

  // Post data of the first row to the log
  for (Col = 0; Col < Grid["wColumnCount"]; Col++)
    Log["Message"]("Cell (0, " + Col + ") text: " + GetCellText (Grid, 0, Col));
}

function GetCellText (Grid, RowIndex, ColumnId)
{
  // Get the column index
  var ColIndex = GetColIndexById (Grid, ColumnId);
  // Get the cell value
  return Grid["Item"](ColIndex, RowIndex)["FormattedValue"]["ToString"]()["OleValue"];
}

function GetColIndexById (Grid, ColumnId)
{
  // Check if the column is specified by caption or index
  if (aqObject["GetVarType"](ColumnId) == varOleStr)
  {
    // Search for the column by its caption
    for (i=0; i<Grid["ColumnCount"]; i++)
      if (Grid["Columns"]["Item"](i)["HeaderText"]["OleValue"] == ColumnId)
        return i; // Column is found

    return -1; // Column is not found
  }
  else
    // The column is specified by index; return this index
    return ColumnId;
}

Setting Cell Values

There are two general approaches for modifying DataGridView 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 MicrosoftDataGridView.wValue property or internal properties and methods of the DataGridView control.

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 DataGridView control in the tested application uses specific in-place editors (for example, check boxes, combo boxes and others), you may need to work with them in a custom way. For more information on how to work with specific editors, see Working With Specific In-place Editors in Microsoft DataGridView.

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 and activate the cell’s in-place editor. When the cell is in the edit mode, 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, NewRowIndex;

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

  // Add a new record to the grid
  NewRowIndex = Grid.wRowCount-1;
  InputCellValue (Grid, NewRowIndex, "Customer Name", "Robert King");
  InputCellValue (Grid, NewRowIndex, "Product", "FamilyAlbum");
  InputCellValue (Grid, NewRowIndex, "Quantity", "2");
  InputCellValue (Grid, NewRowIndex, "Date", "5/21/2007");
  InputCellValue (Grid, NewRowIndex, "Street", "123 Home Lane");
  InputCellValue (Grid, NewRowIndex, "City", "Greenville, MS");
  InputCellValue (Grid, NewRowIndex, "State", "US");
  InputCellValue (Grid, NewRowIndex, "Zip Code", "12345");
  InputCellValue (Grid, NewRowIndex, "Card", "VISA");
  InputCellValue (Grid, NewRowIndex, "Card No", "123123454321");
}

function InputCellValue (Grid, RowIndex, ColumnId, Text)
{
  // Click the cell
  Grid.ClickCell (RowIndex, ColumnId);
  // Activate the cell's editor
  ActivateCellEditor (Grid);
  // Input the new value
  Grid.Keys (Text);
  // Save the changes
  CloseCellEditor (Grid);
}

function ActivateCellEditor (Grid)
{
  Grid.Keys ("[F2]");
}

function CloseCellEditor (Grid)
{
  Grid.Keys ("[Enter]");
}

Python

def Main ():

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

  # Add a new record to the grid
  NewRowIndex = Grid.wRowCount-1
  InputCellValue (Grid, NewRowIndex, "Customer Name", "Robert King")
  InputCellValue (Grid, NewRowIndex, "Product", "FamilyAlbum")
  InputCellValue (Grid, NewRowIndex, "Quantity", "2")
  InputCellValue (Grid, NewRowIndex, "Date", "5/21/2007")
  InputCellValue (Grid, NewRowIndex, "Street", "123 Home Lane")
  InputCellValue (Grid, NewRowIndex, "City", "Greenville, MS")
  InputCellValue (Grid, NewRowIndex, "State", "US")
  InputCellValue (Grid, NewRowIndex, "Zip Code", "12345")
  InputCellValue (Grid, NewRowIndex, "Card", "VISA")
  InputCellValue (Grid, NewRowIndex, "Card No", "123123454321")

def InputCellValue (Grid, RowIndex, ColumnId, Text):
  # Click the cell
  Grid.ClickCell (RowIndex, ColumnId)
  # Activate the cell's editor
  ActivateCellEditor (Grid)
  # Input the new value
  Grid.Keys (Text)
  # Save the changes
  CloseCellEditor (Grid)

def ActivateCellEditor (Grid):
  Grid.Keys ("[F2]")

def CloseCellEditor (Grid):
  Grid.Keys ("[Enter]")

VBScript

Sub Main
  Dim p, Grid, NewRowIndex

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

  ' Get the Add New row index
  NewRowIndex = Grid.wRowCount-1
  ' Add the new record to the grid
  Call InputCellValue (Grid, NewRowIndex, "Customer Name", "Robert King")
  Call InputCellValue (Grid, NewRowIndex, "Product", "FamilyAlbum")
  Call InputCellValue (Grid, NewRowIndex, "Quantity", "2")
  Call InputCellValue (Grid, NewRowIndex, "Date", "5/21/2007")
  Call InputCellValue (Grid, NewRowIndex, "Street", "123 Home Lane")
  Call InputCellValue (Grid, NewRowIndex, "City", "Greenville, MS")
  Call InputCellValue (Grid, NewRowIndex, "State", "US")
  Call InputCellValue (Grid, NewRowIndex, "Zip Code", "12345")
  Call InputCellValue (Grid, NewRowIndex, "Card", "VISA")
  Call InputCellValue (Grid, NewRowIndex, "Card No", "123123454321")
End Sub

Sub InputCellValue (Grid, RowIndex, ColumnId, Text)
  ' Click the cell
  Call Grid.ClickCell (RowIndex, ColumnId)
  ' Activate the cell's editor
  ActivateCellEditor Grid
  ' Input the new value
  Grid.Keys Text
  ' Save the changes
  CloseCellEditor Grid
End Sub

Sub ActivateCellEditor (Grid)
  Grid.Keys "[F2]"
End Sub

Sub CloseCellEditor (Grid)
  Grid.Keys "[Enter]"
End Sub

DelphiScript

procedure InputCellValue (Grid, RowIndex, ColumnId, Text); forward;
procedure ActivateCellEditor (Grid); forward;
procedure CloseCellEditor (Grid); forward;

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

  // Get the Add New row index
  NewRowIndex := Grid.wRowCount-1;
  // Add the new record to the grid
  InputCellValue (Grid, NewRowIndex, 'Customer Name', 'Robert King');
  InputCellValue (Grid, NewRowIndex, 'Product', 'FamilyAlbum');
  InputCellValue (Grid, NewRowIndex, 'Quantity', '2');
  InputCellValue (Grid, NewRowIndex, 'Date', '5/21/2007');
  InputCellValue (Grid, NewRowIndex, 'Street', '123 Home Lane');
  InputCellValue (Grid, NewRowIndex, 'City', 'Greenville, MS');
  InputCellValue (Grid, NewRowIndex, 'State', 'US');
  InputCellValue (Grid, NewRowIndex, 'Zip Code', '12345');
  InputCellValue (Grid, NewRowIndex, 'Card', 'VISA');
  InputCellValue (Grid, NewRowIndex, 'Card No', '123123454321');
end;

procedure InputCellValue (Grid, RowIndex, ColumnId, Text);
begin
  // Click the cell
  Grid.ClickCell (RowIndex, ColumnId);
  // Activate the cell's editor
  ActivateCellEditor (Grid);
  // Input the new value
  Grid.Keys (Text);
  // Save the changes
  CloseCellEditor (Grid);
end;

procedure ActivateCellEditor (Grid);
begin
  Grid.Keys ('[F2]');
end;

procedure CloseCellEditor (Grid);
begin
  Grid.Keys ('[Enter]');
end;

C++Script, C#Script

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

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

  // Add a new record to the grid
  NewRowIndex = Grid["wRowCount"]-1;
  InputCellValue (Grid, NewRowIndex, "Customer Name", "Robert King");
  InputCellValue (Grid, NewRowIndex, "Product", "FamilyAlbum");
  InputCellValue (Grid, NewRowIndex, "Quantity", "2");
  InputCellValue (Grid, NewRowIndex, "Date", "5/21/2007");
  InputCellValue (Grid, NewRowIndex, "Street", "123 Home Lane");
  InputCellValue (Grid, NewRowIndex, "City", "Greenville, MS");
  InputCellValue (Grid, NewRowIndex, "State", "US");
  InputCellValue (Grid, NewRowIndex, "Zip Code", "12345");
  InputCellValue (Grid, NewRowIndex, "Card", "VISA");
  InputCellValue (Grid, NewRowIndex, "Card No", "123123454321");
}

function InputCellValue (Grid, RowIndex, ColumnId, Text)
{
  // Click the cell
  Grid["ClickCell"](RowIndex, ColumnId);
  // Activate the cell's editor
  ActivateCellEditor (Grid);
  // Input the new value
  Grid["Keys"](Text);
  // Save the changes
  CloseCellEditor (Grid);
}

function ActivateCellEditor (Grid)
{
  Grid["Keys"]("[F2]");
}

function CloseCellEditor (Grid)
{
  Grid["Keys"]("[Enter]");
}

Assigning the New Value Programmatically

Another way to modify grid cell values is to use the wValue property of the MicrosoftDataGridView 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 ("DataGridViewSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1");

  // 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, "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 ("DataGridViewSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1");

  // 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, "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 ("DataGridViewSample")
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1")

  # 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, "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 ("DataGridViewSample")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1")

  ' 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, "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 ('DataGridViewSample');
  Grid := p.WinFormsObject('Form1').WinFormsObject('dataGridView1');

  // 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, '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"]("DataGridViewSample");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("dataGridView1");

  // 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, "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";
}

It is also possible to change the value of a particular grid cell by assigning the desired value to the Value property of the object corresponding to this grid cell. The following example illustrates how this can be done.

Example

View description

JavaScript

function Main ()
{
  var p, Grid;

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

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

function SetCellValue (Grid, RowIndex, ColumnId, Value)
{
  // Get the column index
  let ColIndex = GetColIndexById (Grid, ColumnId);
  // Assign the new value to the cell
  Grid.Item(ColIndex, RowIndex).Value = Value;
}

function GetColIndexById (Grid, ColumnId)
{
  // Check if the column is specified by caption or index
  if (equal(aqObject.GetVarType(ColumnId), varOleStr))
  {
    // Search for the column by its caption
    for (let i=0; i<Grid.ColumnCount; i++)
      if (equal(Grid.Columns.Item(i).HeaderText.OleValue, ColumnId))
        return i; // Column is found

    return -1; // Column is not found
  }
  else
    // The column is specified by index; return this index
    return ColumnId;
}

JScript

function Main ()
{
  var p, Grid;

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

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

function SetCellValue (Grid, RowIndex, ColumnId, Value)
{
  // Get the column index
  var ColIndex = GetColIndexById (Grid, ColumnId);
  // Assign the new value to the cell
  Grid.Item(ColIndex, RowIndex).Value = Value;
}

function GetColIndexById (Grid, ColumnId)
{
  // Check if the column is specified by caption or index
  if (aqObject.GetVarType(ColumnId) == varOleStr)
  {
    // Search for the column by its caption
    for (i=0; i<Grid.ColumnCount; i++)
      if (Grid.Columns.Item(i).HeaderText.OleValue == ColumnId)
        return i; // Column is found

    return -1; // Column is not found
  }
  else
    // The column is specified by index; return this index
    return ColumnId;
}

Python

def Main ():

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

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

def SetCellValue (Grid, RowIndex, ColumnId, Value):
  # Get the column index
  ColIndex = GetColIndexById (Grid, ColumnId)
  # Assign the new value to the cell
  Grid.Item(ColIndex, RowIndex).Value = Value

def GetColIndexById (Grid, ColumnId):
  # Check if the column is specified by caption or index
  if (aqObject.GetVarType(ColumnId) == varOleStr):
    # Search for the column by its caption
    for i in range(0, Grid.ColumnCount):
      if (Grid.Columns.Item[i].HeaderText.OleValue == ColumnId):
        return i # Column is found

    return -1 # Column is not found
  else:
    # The column is specified by index return this index
    return ColumnId

VBScript

Sub Main
  Dim p, Grid

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

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

Sub SetCellValue (Grid, RowIndex, ColumnId, Value)
  Dim ColIndex
  ' Get the column index
  ColIndex = GetColIndexById (Grid, ColumnId)
  ' Assign the new value to the cell
  If aqObject.GetVarType (Value) = varDispatch Then
    Set Grid.Item(ColIndex, RowIndex).Value = Value
  Else
    Grid.Item(ColIndex, RowIndex).Value = Value
  End If
End Sub

Function GetColIndexById (Grid, ColumnId)
  ' Check if the column is specified by caption or index
  If aqObject.GetVarType(ColumnId) = varOleStr Then
  
    ' Search for the column by its caption
    For i=0 To Grid.ColumnCount-1
      If Grid.Columns.Item(i).HeaderText.OleValue = ColumnId Then
        GetColIndexById = i ' Column is found
        Exit Function
      End If
    Next
    GetColIndexById = -1 ' Column is not found
  Else
    ' The column is specified by index return this index
    GetColIndexById = ColumnId
  End If
End Function

DelphiScript

procedure SetCellValue (Grid, RowIndex, ColumnId, Value); forward;
function GetColIndexById (Grid, ColumnId); forward;

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

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

procedure SetCellValue (Grid, RowIndex, ColumnId, Value);
var ColIndex : OleVariant;
begin
  // Get the column index
  ColIndex := GetColIndexById (Grid, ColumnId);
  // Assign the new value to the cell
  Grid.Item[ColIndex, RowIndex].Value := Value;
end;

function GetColIndexById (Grid, ColumnId);
var i: OleVariant;
begin
  // Check if the column is specified by caption or index
  if aqObject.GetVarType(ColumnId) = varOleStr then
  begin
    // Search for the column by its caption
    for i:=0 to Grid.ColumnCount-1 do
      if Grid.Columns.Item[i].HeaderText.OleValue = ColumnId then
      begin
        Result := i; // Column is found
        Exit;
      end;

    Result := -1; // Column is not found
  end
  else
    // The column is specified by index; return this index
    Result := ColumnId;
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

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

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

function SetCellValue (Grid, RowIndex, ColumnId, Value)
{
  // Get the column index
  var ColIndex = GetColIndexById (Grid, ColumnId);
  // Assign the new value to the cell
  Grid["Item"](ColIndex, RowIndex)["Value"] = Value;
}

function GetColIndexById (Grid, ColumnId)
{
  // Check if the column is specified by caption or index
  if (aqObject["GetVarType"](ColumnId) == varOleStr)
  {
    // Search for the column by its caption
    for (i=0; i<Grid["ColumnCount"]; i++)
      if (Grid["Columns"]["Item"](i)["HeaderText"]["OleValue"] == ColumnId)
        return i; // Column is found

    return -1; // Column is not found
  }
  else
    // The column is specified by index; return this index
    return ColumnId;
}

See Also

Working With Microsoft DataGridView
wValue Property (Grid Controls)
Selecting Cells in Microsoft DataGridView
Activating and Closing In-place Editors in Microsoft DataGridView
Working With Specific In-place Editors in Microsoft DataGridView
Copying and Pasting Cell Values in Microsoft DataGridView

Highlight search results