Obtaining and Setting Cell Values in Developer Express XtraGrid

Applies to TestComplete 14.60, last modified on April 22, 2021

This topic describes the various approaches that you can use to obtain and change values stored in the XtraGrid cells. The explanation applies to all XtraGrid layouts - grid, card, banded and others.

Note that before getting or setting the cell value, you need to know in which row (card) and column (card field) the desired cell resides. For example, you can 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 XtraGrid control. For this purpose, the .NET Application Support and Developer Express Control Support plugins must be installed and enabled. The latter lets you work with the XtraGrid control using methods and properties of the DevExpressXtraGrid object. Without this plugin, you will not be able to work with XtraGrid controls using their internal methods and properties.

When testing Developer Express XtraGrid controls, use specific methods and properties of the corresponding DevExpressXtraGrid 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 an XtraGrid 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 DevExpressXtraGrid or DevExpressXtraGridView objects. The wValue property of a DevExpressXtraGrid provides access to a cell’s value of the main data view, while the property of a DevExpressXtraGridView relates to the cells of a corresponding child data view. Note that this property returns the “native” .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. Others, such as String, Decimal, DateTime and enumeration values are made OLE-compatible via the OleValue property added to these objects by TestComplete. To work with complex object values, use their internal properties and methods.

Example

The example works with the GridTutorials application.

How to get the application

View example description

JavaScript

function MainObtain ()
{
  var p, frmMain, Grid;

  // Obtain the application process and its main form
  p = Sys.Process("GridTutorials");
  frmMain = p.WinFormsObject("frmMain");
  // Select the "Card View" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Card View";
  // Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1");

  // Post data of the first row to the log
  LogRowData (Grid, 0);
}

function LogRowData (ActiveViewObj, RowIndex)
{
  var CellValue;
  for (let Col=0; Col<ActiveViewObj.wColumnCount; Col++)
  {
    CellValue = ActiveViewObj.wValue(RowIndex, Col);
    if (equal(aqObject.GetVarType(CellValue), varDispatch))
      Log.Message (ActiveViewObj.wColumn(Col) + ": " + CellValue.ToString().OleValue)
    else
      Log.Message (ActiveViewObj.wColumn(Col) + ": " + CellValue);
  }
}

JScript

function MainObtain ()
{
  var p, frmMain, Grid;

  // Obtain the application process and its main form
  p = Sys.Process("GridTutorials");
  frmMain = p.WinFormsObject("frmMain");
  // Select the "Card View" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Card View";
  // Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1");

  // Post data of the first row to the log
  LogRowData (Grid, 0);
}

function LogRowData (ActiveViewObj, RowIndex)
{
  var CellValue, Col;
  for (Col=0; Col<ActiveViewObj.wColumnCount; Col++)
  {
    CellValue = ActiveViewObj.wValue(RowIndex, Col);
    if (aqObject.GetVarType(CellValue) == varDispatch)
      Log.Message (ActiveViewObj.wColumn(Col) + ": " + CellValue.ToString().OleValue)
    else
      Log.Message (ActiveViewObj.wColumn(Col) + ": " + CellValue);
  }
}

Python

def MainObtain ():

  # Obtain the application process and its main form
  p = Sys.Process("GridTutorials")
  frmMain = p.WinFormsObject("frmMain")
  # Select the "Card View" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Card View"
  # Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  # Post data of the first row to the log
  LogRowData (Grid, 0)

def LogRowData (ActiveViewObj, RowIndex):
  for Col in range (0, ActiveViewObj.wColumnCount-1):
    CellValue = ActiveViewObj.wValue[RowIndex, Col]
    if (aqObject.GetVarType(CellValue) == varDispatch):
      Log.Message (ActiveViewObj.wColumn(Col) + ": " + CellValue.ToString().OleValue)
    else:
      Log.Message (ActiveViewObj.wColumn(Col) + ": " + CellValue)

VBScript

Sub Main
  Dim p, frmMain, Grid

  ' Obtain the application process and its main form
  Set p = Sys.Process("GridTutorials")
  Set frmMain = p.WinFormsObject("frmMain")
  ' Select the "Card View" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Card View"
  ' Obtain the grid object
  Set Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  ' Post data of the first row to the log
  Call LogRowData (Grid, 0)
End Sub

Sub LogRowData (ActiveViewObj, RowIndex)
  Dim CellValue, Col
  For Col = 0 To ActiveViewObj.wColumnCount-1
    If aqObject.GetVarType(ActiveViewObj.wValue(RowIndex, Col)) = varDispatch Then
      Set CellValue = ActiveViewObj.wValue(RowIndex, Col)
      Log.Message (ActiveViewObj.wColumn(Col) & ": " & CellValue.ToString().OleValue)
    Else
      CellValue = ActiveViewObj.wValue(RowIndex, Col)
      Log.Message (ActiveViewObj.wColumn(Col) & ": " & CellValue)
    End If
  Next
End Sub

DelphiScript

procedure LogRowData (ActiveViewObj: OleVariant, RowIndex: integer);forward;

procedure Main;
var p, frmMain, Grid: OleVariant;
begin
  // Obtain the application process and its main form
  p := Sys.Process('GridTutorials');
  frmMain := p.WinFormsObject('frmMain');
  // Select the "Card View" demo
  frmMain.WinFormsObject('gcNavigations').WinFormsObject('listBoxControl1').SelectedItem := 'Card View';
  // Obtain the grid object
  Grid := frmMain.WinFormsObject('pcMain').WinFormsObject('gcContainer').WinFormsObject('Form1').WinFormsObject('gridControl1');

  // Post data of the first row to the log
  LogRowData (Grid, 0);
end;

procedure LogRowData (ActiveViewObj: OleVariant, RowIndex: integer);
var CellValue: OleVariant;
    Col: integer;
begin
  for Col := 0 to ActiveViewObj.wColumnCount-1 do
  begin
    CellValue := ActiveViewObj.wValue[RowIndex, Col];
    if aqObject.GetVarType(CellValue) = varDispatch then
      Log.Message (ActiveViewObj.wColumn[Col] + ': ' + CellValue.ToString.OleValue)
    else
      Log.Message (ActiveViewObj.wColumn[Col] + ': ' + CellValue);
  end
end;

C++Script, C#Script

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

  // Obtain the application process and its main form
  p = Sys["Process"]("GridTutorials");
  frmMain = p["WinFormsObject"]("frmMain");
  // Select the "Card View" demo
  frmMain["WinFormsObject"]("gcNavigations")["WinFormsObject"]("listBoxControl1")["SelectedItem"] = "Card View";
  // Obtain the grid object
  Grid = frmMain["WinFormsObject"]("pcMain")["WinFormsObject"]("gcContainer")["WinFormsObject"]("Form1")["WinFormsObject"]("gridControl1");

  // Post data of the first row to the log
  LogRowData (Grid, 0);
}

function LogRowData (ActiveViewObj, RowIndex)
{
  var CellValue, Col;
  for (Col=0; Col<ActiveViewObj["wColumnCount"]; Col++)
  {
    CellValue = ActiveViewObj["wValue"](RowIndex, Col);
    if (aqObject["GetVarType"](CellValue) == varDispatch)
      Log["Message"] (ActiveViewObj["wColumn"](Col) + ": " + CellValue["ToString"]()["OleValue"])
    else
      Log["Message"] (ActiveViewObj["wColumn"](Col) + ": " + CellValue);
  }
}

Alternatively, you can get the grid cell values using internal methods of the view in which the cell resides:

  • viewObj.GetRowCellValue (RowIndexColumnObj): Returns the value of a cell specified by the row (card) index and the column object.
  • viewObj.GetRowCellValue_2 (RowIndexFieldName): Returns the value of a cell specified by the row (card) index and the column’s dataset field name.

In certain cases, the cell value may significantly differ from the actual text displayed in the cell. For example, the XtraGrid 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. Another example is a grid that uses a lookup (foreign key) combo box that stores values of one dataset field, but displays the corresponding values of another field. For example, the column may hold values of the EmployeeID dataset field but, for usability purposes, display values of the EmployeeName field corresponding to the given EmployeeID value.

In the cases described above, you will most likely want to get the actual text displayed in grid cells rather than the cell values. The following methods of the grid’s view lets you obtain the cell text:

  • viewObj.GetRowCellDisplayText (RowIndexColumnObj): Returns the text in the cell specified by the row (card) index and the column object.
  • viewObj.GetRowCellDisplayText_2 (RowIndexFieldName): Returns the text in the cell specified by the row (card) index and the column’ dataset field name.
Example

The example works with the GridTutorials application.

How to get the application

View example description

JavaScript

function Main ()
{
  var p, frmMain, Grid, nColumns;

  // Obtain the application process and its main form
  p = Sys.Process("GridTutorials");
  frmMain = p.WinFormsObject("frmMain");
  // Select the "Column Format" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Column Format";
  // Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1");

  nColumns = GetColumnCount (Grid, null);

  // Post grid cell values to the log
  Log.AppendFolder ("Cell values in row 0:");
  for (let Col=0; Col<nColumns; Col++)
    Log.Message (aqConvert.VarToStr(GetCellValue (Grid, null, 0, Col)));
  Log.PopLogFolder();

  // Post grid cell text to the log
  Log.AppendFolder ("Cell text in row 0:");
  for (let Col=0; Col<nColumns; Col++)
    Log.Message (GetCellText (Grid, null, 0, Col));
  Log.PopLogFolder();
}

function GetCellValue (Grid, View, RowIndex, ColumnId)
{
  // Get the grid view, if it is not specified
  if (strictEqual(View, null))
    View = Grid.MainView;
  // Get the column object
  let Column = GetColumn (Grid, View, ColumnId);
  // Get the cell value
  return View.GetRowCellValue(RowIndex, Column);
}

function GetCellText (Grid, View, RowIndex, ColumnId)
{
  // Get the grid view, if it is not specified
  if (strictEqual(View, null))
    View = Grid.MainView;
  // Get the column object
  let Column = GetColumn (Grid, View, ColumnId);
  // Get the cell text
  return View.GetRowCellDisplayText(RowIndex, Column).OleValue;
}

function GetColumnCount (Grid, View)
{
  if (strictEqual(View, null))
    return Grid.MainView.Columns.Count
  else
    return View.Columns.Count;
}

function GetColumn (Grid, View, ColumnId)
{
  var Columns, Col;

  // Get the grid view object, if it is not specified
  if (strictEqual(View, null))
    View = Grid.MainView;

  Columns = View.Columns;
  // 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<Columns.Count; i++)
    {
      Col = Columns.Item_2 (i);
      if (equal(Col.Caption.OleValue, ColumnId))
        return Col; // The column is found
    }
    return null; // The column is not found
  }
  else
    // The column is specified by index
    return Columns.Item_2 (ColumnId);
}

JScript

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

  // Obtain the application process and its main form
  p = Sys.Process("GridTutorials");
  frmMain = p.WinFormsObject("frmMain");
  // Select the "Column Format" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Column Format";
  // Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1");

  nColumns = GetColumnCount (Grid, null);

  // Post grid cell values to the log
  Log.AppendFolder ("Cell values in row 0:");
  for (Col=0; Col<nColumns; Col++)
    Log.Message (aqConvert.VarToStr(GetCellValue (Grid, null, 0, Col)));
  Log.PopLogFolder();

  // Post grid cell text to the log
  Log.AppendFolder ("Cell text in row 0:");
  for (Col=0; Col<nColumns; Col++)
    Log.Message (GetCellText (Grid, null, 0, Col));
  Log.PopLogFolder();
}

function GetCellValue (Grid, View, RowIndex, ColumnId)
{
  // Get the grid view, if it is not specified
  if (View == null)
    View = Grid.MainView;
  // Get the column object
  var Column = GetColumn (Grid, View, ColumnId);
  // Get the cell value
  return View.GetRowCellValue(RowIndex, Column);
}

function GetCellText (Grid, View, RowIndex, ColumnId)
{
  // Get the grid view, if it is not specified
  if (View == null)
    View = Grid.MainView;
  // Get the column object
  var Column = GetColumn (Grid, View, ColumnId);
  // Get the cell text
  return View.GetRowCellDisplayText(RowIndex, Column).OleValue;
}

function GetColumnCount (Grid, View)
{
  if (View == null)
    return Grid.MainView.Columns.Count
  else
    return View.Columns.Count;
}

function GetColumn (Grid, View, ColumnId)
{
  var Columns, Col, i;

  // Get the grid view object, if it is not specified
  if (View == null)
    View = Grid.MainView;

  Columns = View.Columns;
  // 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<Columns.Count; i++)
    {
      Col = Columns.Item_2 (i);
      if (Col.Caption.OleValue == ColumnId)
        return Col; // The column is found
    }
    return null; // The column is not found
  }
  else
    // The column is specified by index
    return Columns.Item_2 (ColumnId);
}

Python

def Main ():

  # Obtain the application process and its main form
  p = Sys.Process("GridTutorials")
  frmMain = p.WinFormsObject("frmMain")
  # Select the "Column Format" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Column Format"
  # Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  nColumns = GetColumnCount (Grid, None)

  # Post grid cell values to the log
  Log.AppendFolder ("Cell values in row 0:")
  for Col in range(0, nColumns-1):
    Log.Message (aqConvert.VarToStr(GetCellValue (Grid, None, 0, Col)))
  Log.PopLogFolder()

  # Post grid cell text to the log
  Log.AppendFolder ("Cell text in row 0:")
  for Col in range (0, nColumns-1):
    Log.Message (GetCellText (Grid, None, 0, Col))
  Log.PopLogFolder()

def GetCellValue (Grid, View, RowIndex, ColumnId):
  # Get the grid view, if it is not specified
  if (View == None):
    View = Grid.MainView
  # Get the column object
  Column = GetColumn (Grid, View, ColumnId)
  # Get the cell value
  return View.GetRowCellValue(RowIndex, Column)

def GetCellText (Grid, View, RowIndex, ColumnId):
  # Get the grid view, if it is not specified
  if (View == None):
    View = Grid.MainView
  # Get the column object
  Column = GetColumn (Grid, View, ColumnId)
  # Get the cell text
  return View.GetRowCellDisplayText(RowIndex, Column).OleValue

def GetColumnCount (Grid, View):
  if (View == None):
    return Grid.MainView.Columns.Count
  else:
    return View.Columns.Count 

def GetColumn (Grid, View, ColumnId):

  # Get the grid view object, if it is not specified
  if (View == None):
    View = Grid.MainView

  Columns = View.Columns
  # 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, Columns.Count):
      Col = Columns.Item_2 [i]
      if (Col.Caption.OleValue == ColumnId):
        return Col # The column is found
    return None # The column is not found
  else:
    # The column is specified by index
    return Columns.Item_2 [ColumnId]

VBScript

Sub Main
  Dim p, frmMain, Grid, nColumns, Col

  ' Obtain the application process and its main form
  Set p = Sys.Process("GridTutorials")
  Set frmMain = p.WinFormsObject("frmMain")
  ' Select the "Column Format" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Column Format"
  ' Obtain the grid object
  Set Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  nColumns = GetColumnCount (Grid, Nothing)

  ' Post grid cell values to the log
  Log.AppendFolder "Cell values in row 0:"
  For Col = 0 To nColumns-1
    Log.Message aqConvert.VarToStr(GetCellValue (Grid, Nothing, 0, Col))
  Next
  Log.PopLogFolder

  ' Post grid cell text to the log
  Log.AppendFolder "Cell text in row 0:"
  For Col = 0 To nColumns-1
    Log.Message GetCellText (Grid, Nothing, 0, Col)
  Next
  Log.PopLogFolder
End Sub

Function GetCellValue (Grid, View, RowIndex, ColumnId)
  Dim Column
  ' Get the grid view, if it is not specified
  If View Is Nothing Then
    Set View = Grid.MainView
  End If
  ' Get the column object
  Set Column = GetColumn (Grid, View, ColumnId)
  ' Get the cell value
  Set GetCellValue = View.GetRowCellValue(RowIndex, Column)
End Function

Function GetCellText (Grid, View, RowIndex, ColumnId)
  Dim Column
  ' Get the grid view, if it is not specified
  If View Is Nothing Then
    Set View = Grid.MainView
  End If
  ' Get the column object
  Set Column = GetColumn (Grid, View, ColumnId)
  ' Get the cell text
  GetCellText = View.GetRowCellDisplayText(RowIndex, Column).OleValue
End Function

Function GetColumnCount (Grid, View)
  If View Is Nothing Then
    GetColumnCount = Grid.MainView.Columns.Count
  Else
    GetColumnCount = View.Columns.Count
  End If
End Function

Function GetColumn (Grid, View, ColumnId)
  Dim Columns, Col, i

  ' Get the grid view, if it is not specified
  If View Is Nothing Then
    Set View = Grid.MainView
  End If

  Set Columns = View.Columns
  ' 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 Columns.Count-1
      Set Col = Columns.Item_2 (i)
      If Col.Caption.OleValue = ColumnId Then
        Set GetColumn = Col ' The column is found
        Exit Function
      End If
    Next
    Set GetColumn = Nothing' The column is not found
  Else
    ' The column is specified by index
    Set GetColumn = Columns.Item_2 (ColumnId)
  End If
End Function

DelphiScript

function GetCellValue (Grid, View, RowIndex, ColumnId); forward;
function GetCellText (Grid, View, RowIndex, ColumnId); forward;
function GetColumn (Grid, View, ColumnId); forward;
function GetColumnCount (Grid, View); forward;

procedure Main;
var p, frmMain, Grid, nColumns, Col : OleVariant;
begin
  // Obtain the application process and its main form
  p := Sys.Process('GridTutorials');
  frmMain := p.WinFormsObject('frmMain');
  // Select the 'Column Format' demo
  frmMain.WinFormsObject('gcNavigations').WinFormsObject('listBoxControl1').SelectedItem := 'Column Format';
  // Obtain the grid object
  Grid := frmMain.WinFormsObject('pcMain').WinFormsObject('gcContainer').WinFormsObject('Form1').WinFormsObject('gridControl1');

  nColumns := GetColumnCount (Grid, nil);

  // Post grid cell values to the log
  Log.AppendFolder ('Cell values in row 0:');
  for Col := 0 to nColumns-1 do
    Log.Message (aqConvert.VarToStr (GetCellValue(Grid, nil, 0, Col)));
  Log.PopLogFolder;

  // Post grid cell text to the log
  Log.AppendFolder ('Cell text in row 0:');
  for Col := 0 to nColumns-1 do
    Log.Message (GetCellText(Grid, nil, 0, Col));
  Log.PopLogFolder;
end;

function GetCellValue (Grid, View, RowIndex, ColumnId);
var Column : OleVariant;
begin
  // Get the grid view, if it is not specified
  if View = nil then
    View := Grid.MainView;
  // Get the column object
  Column := GetColumn (Grid, View, ColumnId);
  // Get the cell value
  Result := View.GetRowCellValue (RowIndex, Column);
end;

function GetCellText (Grid, View, RowIndex, ColumnId);
var Column : OleVariant;
begin
  // Get the grid view, if it is not specified
  if View = nil then
    View := Grid.MainView;
  // Get the column object
  Column := GetColumn (Grid, View, ColumnId);
  // Get the cell text
  Result := View.GetRowCellDisplayText(RowIndex, Column).OleValue;
end;

function GetColumnCount (Grid, View);
begin
  if View = nil then
    Result := Grid.MainView.Columns.Count
  else
    Result := View.Columns.Count
end;

function GetColumn (Grid, View, ColumnId);
var Columns, Col, i : OleVariant;
begin
  // Get the grid view object, if it is not specified
  if View = nil then
    View := Grid.MainView;

  Columns := View.Columns;
  // 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 Columns.Count-1 do
    begin
      Col := Columns.Item_2[i];
      if Col.Caption.OleValue = ColumnId then
      begin
        Result := Col; // The column is found
        Exit
      end
    end;
    Result := nil; // The column is not found
  end
  else
    // The column is specified by index
    Result := Columns.Item_2[ColumnId];
end;

C++Script, C#Script

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

  // Obtain the application process and its main form
  p = Sys["Process"]("GridTutorials");
  frmMain = p["WinFormsObject"]("frmMain");
  // Select the "Column Format" demo
  frmMain["WinFormsObject"]("gcNavigations")["WinFormsObject"]("listBoxControl1")["SelectedItem"] = "Column Format";
  // Obtain the grid object
  Grid = frmMain["WinFormsObject"]("pcMain")["WinFormsObject"]("gcContainer")["WinFormsObject"]("Form1")["WinFormsObject"]("gridControl1");

  nColumns = GetColumnCount (Grid, null);

  // Post grid cell values to the log
  Log["AppendFolder"]("Cell values in row 0:");
  for (Col=0; Col<nColumns; Col++)
    Log["Message"](aqConvert["VarToStr"](GetCellValue (Grid, null, 0, Col)));
  Log["PopLogFolder"]();

  // Post grid cell text to the log
  Log["AppendFolder"]("Cell text in row 0:");
  for (Col=0; Col<nColumns; Col++)
    Log["Message"](GetCellText (Grid, null, 0, Col));
  Log["PopLogFolder"]();
}

function GetCellValue (Grid, View, RowIndex, ColumnId)
{
  // Get the grid view, if it is not specified
  if (View == null)
    View = Grid["MainView"];
  // Get the column object
  var Column = GetColumn (Grid, View, ColumnId);
  // Get the cell value
  return View["GetRowCellValue"](RowIndex, Column);
}

function GetCellText (Grid, View, RowIndex, ColumnId)
{
  // Get the grid view, if it is not specified
  if (View == null)
    View = Grid["MainView"];
  // Get the column object
  var Column = GetColumn (Grid, View, ColumnId);
  // Get the cell text
  return View["GetRowCellDisplayText"](RowIndex, Column)["OleValue"];
}

function GetColumnCount (Grid, View)
{
  if (View == null)
    return Grid["MainView"]["Columns"]["Count"]
  else
    return View["Columns"]["Count"];
}

function GetColumn (Grid, View, ColumnId)
{
  var Columns, Col, i;

  // Get the grid view object, if it is not specified
  if (View == null)
    View = Grid["MainView"];

  Columns = View["Columns"];
  // 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<Columns["Count"]; i++)
    {
      Col = Columns["Item_2"](i);
      if (Col["Caption"]["OleValue"] == ColumnId)
        return Col; // The column is found
    }
    return null; // The column is not found
  }
  else
    // The column is specified by index
    return Columns["Item_2"](ColumnId);
}

Setting Cell Values

There are two general approaches for modifying XtraGrid cell values:

  • Simulating the user actions over the cell’s in-place editor, for example, “typing” the new value into the cell.
  • Assigning the new value using the wValue property or internal methods and properties of the XtraGrid 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 XtraGrid control in the tested application uses specific in-place editors (for example, check boxes, text boxes with embedded buttons, lookup editors 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 Developer Express XtraGrid.

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 (card) and column (card field) in which the cell resides. For example, you can search for the row (card) 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’s edit boxes.

Example

The example works with the GridTutorials application.

How to get the application

View example description

JavaScript

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

  // Obtain the application process and its main form
  p = Sys.Process("GridTutorials");
  frmMain = p.WinFormsObject("frmMain");
  // Select the "Multi Editors" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Editors";
  // Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1");

  // Change column values
  InputCellValue (Grid, null, 0, "Product1", "Chang");
  InputCellValue (Grid, null, 1, "Product1", "Beverages");
  InputCellValue (Grid, null, 2, "Product1", "Exotic Liquids");
  InputCellValue (Grid, null, 3, "Product1", "24 - 12 oz bottles");
  InputCellValue (Grid, null, 4, "Product1", "15.2");
  InputCellValue (Grid, null, 5, "Product1", "17");
  InputCellValue (Grid, null, 7, "Product1", "3/19/2007");
}

function InputCellValue (Grid, ActiveViewObj, RowIndex, ColumnId, Text)
{
  // Get the grid view, if it is not specified
  if (strictEqual(ActiveViewObj, null))
    ActiveViewObj = Grid;

  // Select the cell
  ActiveViewObj.ClickCell (RowIndex, ColumnId);
  // Activate the edit mode
  ActivateCellEditor (Grid);
  // Type the new value
  Grid.Keys ("^a[Del]" + Text);
  // Save the changes
  CloseCellEditor (Grid);
}

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

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

JScript

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

  // Obtain the application process and its main form
  p = Sys.Process("GridTutorials");
  frmMain = p.WinFormsObject("frmMain");
  // Select the "Multi Editors" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Editors";
  // Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1");

  // Change column values
  InputCellValue (Grid, null, 0, "Product1", "Chang");
  InputCellValue (Grid, null, 1, "Product1", "Beverages");
  InputCellValue (Grid, null, 2, "Product1", "Exotic Liquids");
  InputCellValue (Grid, null, 3, "Product1", "24 - 12 oz bottles");
  InputCellValue (Grid, null, 4, "Product1", "15.2");
  InputCellValue (Grid, null, 5, "Product1", "17");
  InputCellValue (Grid, null, 7, "Product1", "3/19/2007");
}

function InputCellValue (Grid, ActiveViewObj, RowIndex, ColumnId, Text)
{
  // Get the grid view, if it is not specified
  if (ActiveViewObj == null)
    ActiveViewObj = Grid;

  // Select the cell
  ActiveViewObj.ClickCell (RowIndex, ColumnId);
  // Activate the edit mode
  ActivateCellEditor (Grid);
  // Type the new value
  Grid.Keys ("^a[Del]" + Text);
  // Save the changes
  CloseCellEditor (Grid);
}

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

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

Python

def Main ():

  # Obtain the application process and its main form
  p = Sys.Process("GridTutorials")
  frmMain = p.WinFormsObject("frmMain")
  # Select the "Multi Editors" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Editors"
  # Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  # Change column values
  InputCellValue (Grid, None, 0, "Product1", "Chang")
  InputCellValue (Grid, None, 1, "Product1", "Beverages")
  InputCellValue (Grid, None, 2, "Product1", "Exotic Liquids")
  InputCellValue (Grid, None, 3, "Product1", "24 - 12 oz bottles")
  InputCellValue (Grid, None, 4, "Product1", "15.2")
  InputCellValue (Grid, None, 5, "Product1", "17")
  InputCellValue (Grid, None, 7, "Product1", "3/19/2007")

def InputCellValue (Grid, ActiveViewObj, RowIndex, ColumnId, Text):
  # Get the grid view, if it is not specified
  if (ActiveViewObj == None):
    ActiveViewObj = Grid

  # Select the cell
  ActiveViewObj.ClickCell (RowIndex, ColumnId)
  # Activate the edit mode
  ActivateCellEditor (Grid)
  # Type the new value
  Grid.Keys ("^a[Del]" + Text)
  # Save the changes
  CloseCellEditor (Grid)

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

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

VBScript

Sub Main
  Dim p, frmMain, Grid

  ' Obtain the application process and its main form
  Set p = Sys.Process("GridTutorials")
  Set frmMain = p.WinFormsObject("frmMain")
  ' Select the "Multi Editors" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Editors"
  ' Obtain the grid object
  Set Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  ' Change column values
  Call InputCellValue (Grid, Nothing, 0, "Product1", "Chang")
  Call InputCellValue (Grid, Nothing, 1, "Product1", "Beverages")
  Call InputCellValue (Grid, Nothing, 2, "Product1", "Exotic Liquids")
  Call InputCellValue (Grid, Nothing, 3, "Product1", "24 - 12 oz bottles")
  Call InputCellValue (Grid, Nothing, 4, "Product1", "15.2")
  Call InputCellValue (Grid, Nothing, 5, "Product1", "17")
  Call InputCellValue (Grid, Nothing, 7, "Product1", "3/19/2007")
End Sub

Sub InputCellValue (Grid, ActiveViewObj, RowIndex, ColumnId, Text)
  ' Get the grid view, if it is not specified
  If ActiveViewObj Is Nothing Then
    Set ActiveViewObj = Grid
  End If 
  ' Select the cell
  Call ActiveViewObj.ClickCell (RowIndex, ColumnId)
  ' Activate the edit mode
  ActivateCellEditor Grid
  ' Type the new value
  Grid.Keys "^a[Del]" & 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, ActiveViewObj, RowIndex, ColumnId, Text); forward;
procedure ActivateCellEditor (Grid); forward;
procedure CloseCellEditor (Grid); forward;

procedure Main;
var p, frmMain, Grid : OleVariant;
begin
  // Obtain the application process and its main form
  p := Sys.Process('GridTutorials');
  frmMain := p.WinFormsObject('frmMain');
  // Select the 'Multi Editors' demo
  frmMain.WinFormsObject('gcNavigations').WinFormsObject('listBoxControl1').SelectedItem := 'Multi Editors';
  // Obtain the grid object
  Grid := frmMain.WinFormsObject('pcMain').WinFormsObject('gcContainer').WinFormsObject('Form1').WinFormsObject('gridControl1');

  // Change column values
  InputCellValue (Grid, nil, 0, 'Product1', 'Chang');
  InputCellValue (Grid, nil, 1, 'Product1', 'Beverages');
  InputCellValue (Grid, nil, 2, 'Product1', 'Exotic Liquids');
  InputCellValue (Grid, nil, 3, 'Product1', '24 - 12 oz bottles');
  InputCellValue (Grid, nil, 4, 'Product1', '15.2');
  InputCellValue (Grid, nil, 5, 'Product1', '17');
  InputCellValue (Grid, nil, 7, 'Product1', '3/19/2007');
end;

procedure InputCellValue (Grid, ActiveViewObj, RowIndex, ColumnId, Text);
begin
  // Get the grid view, if it is not specified
  if ActiveViewObj = nil then
    ActiveViewObj := Grid.MainView;

  // Select the cell
  ActiveViewObj.ClickCell (RowIndex, ColumnId);
  // Activate the edit mode
  ActivateCellEditor (Grid);
  // Type the new value
  Grid.Keys ('^a[Del]' + 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, frmMain, Grid;

  // Obtain the application process and its main form
  p = Sys["Process"]("GridTutorials");
  frmMain = p["WinFormsObject"]("frmMain");
  // Select the "Multi Editors" demo
  frmMain["WinFormsObject"]("gcNavigations")["WinFormsObject"]("listBoxControl1")["SelectedItem"] = "Multi Editors";
  // Obtain the grid object
  Grid = frmMain["WinFormsObject"]("pcMain")["WinFormsObject"]("gcContainer")["WinFormsObject"]("Form1")["WinFormsObject"]("gridControl1");

  // Change column values
  InputCellValue (Grid, null, 0, "Product1", "Chang");
  InputCellValue (Grid, null, 1, "Product1", "Beverages");
  InputCellValue (Grid, null, 2, "Product1", "Exotic Liquids");
  InputCellValue (Grid, null, 3, "Product1", "24 - 12 oz bottles");
  InputCellValue (Grid, null, 4, "Product1", "15.2");
  InputCellValue (Grid, null, 5, "Product1", "17");
  InputCellValue (Grid, null, 7, "Product1", "3/19/2007");
}

function InputCellValue (Grid, ActiveViewObj, RowIndex, ColumnId, Text)
{
  // Get the grid view, if it is not specified
  if (ActiveViewObj == null)
    ActiveViewObj = Grid;

  // Select the cell
  ActiveViewObj["ClickCell"](RowIndex, ColumnId);
  // Activate the edit mode
  ActivateCellEditor (Grid);
  // Type the new value
  Grid["Keys"]("^a[Del]" + 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 DevExpressXtraGrid or DevExpressXtraGridView objects. 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, frmMain, Grid;

  // Obtain the application process and its main form
  p = Sys.Process("GridTutorials");
  frmMain = p.WinFormsObject("frmMain");
  frmMain.Activate();
  // Select the "Multi Editors" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Editors";
  // Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1");

  // Change column values
  Grid.$set("wValue", 0, "Product1", "Chang");
  Grid.$set("wValue", 1, "Product1", "1"); // CategoryID for "Beverages"
  Grid.$set("wValue", 2, "Product1", "Exotic Liquids");
  Grid.$set("wValue", 3, "Product1", "24 - 12 oz bottles");
  Grid.$set("wValue", 4, "Product1", 15.2);
  Grid.$set("wValue", 5, "Product1", 17);
  Grid.$set("wValue", 6, "Product1", false);
  Grid.$set("wValue", 7, "Product1", "3/19/2007");
  Grid.$set("wValue", 8, "Product1", 90);
}

JScript

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

  // Obtain the application process and its main form
  p = Sys.Process("GridTutorials");
  frmMain = p.WinFormsObject("frmMain");
  frmMain.Activate();
  // Select the "Multi Editors" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Editors";
  // Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1");

  // Change column values
  Grid.wValue (0, "Product1") = "Chang";
  Grid.wValue (1, "Product1") = "1"; // CategoryID for "Beverages"
  Grid.wValue (2, "Product1") = "Exotic Liquids";
  Grid.wValue (3, "Product1") = "24 - 12 oz bottles";
  Grid.wValue (4, "Product1") = 15.2;
  Grid.wValue (5, "Product1") = 17;
  Grid.wValue (6, "Product1") = false;
  Grid.wValue (7, "Product1") = "3/19/2007";
  Grid.wValue (8, "Product1") = 90;
}

Python

def Main ():

  # Obtain the application process and its main form
  p = Sys.Process("GridTutorials")
  frmMain = p.WinFormsObject("frmMain")
  frmMain.Activate()
  # Select the "Multi Editors" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Editors"
  # Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  # Change column values
  Grid.wValue (0, "Product1") = "Chang"
  Grid.wValue (1, "Product1") = "1" # CategoryID for "Beverages"
  Grid.wValue (2, "Product1") = "Exotic Liquids"
  Grid.wValue (3, "Product1") = "24 - 12 oz bottles"
  Grid.wValue (4, "Product1") = 15.2
  Grid.wValue (5, "Product1") = 17
  Grid.wValue (6, "Product1") = False
  Grid.wValue (7, "Product1") = "3/19/2007"
  Grid.wValue (8, "Product1") = 90

VBScript

Sub Main
  Dim p, frmMain, Grid

  ' Obtain the application process and its main form
  Set p = Sys.Process("GridTutorials")
  Set frmMain = p.WinFormsObject("frmMain")
  frmMain.Activate
  ' Select the "Multi Editors" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Editors"
  ' Obtain the grid object
  Set Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  ' Change column values
  Grid.wValue (0, "Product1") = "Chang"
  Grid.wValue (1, "Product1") = "1" ' CategoryID for "Beverages"
  Grid.wValue (2, "Product1") = "Exotic Liquids"
  Grid.wValue (3, "Product1") = "24 - 12 oz bottles"
  Grid.wValue (4, "Product1") = 15.2
  Grid.wValue (5, "Product1") = 17
  Grid.wValue (6, "Product1") = False
  Grid.wValue (7, "Product1") = "3/19/2007"
  Grid.wValue (8, "Product1") = 90
End Sub

DelphiScript

procedure Main;
var p, frmMain, Grid : OleVariant;
begin
  // Obtain the application process and its main form
  p := Sys.Process('GridTutorials');
  frmMain := p.WinFormsObject('frmMain');
  frmMain.Activate;
  // Select the 'Multi Editors' demo
  frmMain.WinFormsObject('gcNavigations').WinFormsObject('listBoxControl1').SelectedItem := 'Multi Editors';
  // Obtain the grid object
  Grid := frmMain.WinFormsObject('pcMain').WinFormsObject('gcContainer').WinFormsObject('Form1').WinFormsObject('gridControl1');

  // Change column values
  Grid.wValue[0, 'Product1'] := 'Chang';
  Grid.wValue[1, 'Product1'] := '1'; // CategoryID for 'Beverages'
  Grid.wValue[2, 'Product1'] := 'Exotic Liquids';
  Grid.wValue[3, 'Product1'] := '24 - 12 oz bottles';
  Grid.wValue[4, 'Product1'] := 15.2;
  Grid.wValue[5, 'Product1'] := 17;
  Grid.wValue[6, 'Product1'] := false;
  Grid.wValue[7, 'Product1'] := '3/19/2007';
  Grid.wValue[8, 'Product1'] := 90;
end;

C++Script, C#Script

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

  // Obtain the application process and its main form
  p = Sys["Process"]("GridTutorials");
  frmMain = p["WinFormsObject"]("frmMain");
  frmMain["Activate"]();
  // Select the "Multi Editors" demo
  frmMain["WinFormsObject"]("gcNavigations")["WinFormsObject"]("listBoxControl1")["SelectedItem"] = "Multi Editors";
  // Obtain the grid object
  Grid = frmMain["WinFormsObject"]("pcMain")["WinFormsObject"]("gcContainer")["WinFormsObject"]("Form1")["WinFormsObject"]("gridControl1");

  // Change column values
  Grid["wValue"](0, "Product1") = "Chang";
  Grid["wValue"](1, "Product1") = "1"; // CategoryID for "Beverages"
  Grid["wValue"](2, "Product1") = "Exotic Liquids";
  Grid["wValue"](3, "Product1") = "24 - 12 oz bottles";
  Grid["wValue"](4, "Product1") = 15.2;
  Grid["wValue"](5, "Product1") = 17;
  Grid["wValue"](6, "Product1") = false;
  Grid["wValue"](7, "Product1") = "3/19/2007";
  Grid["wValue"](8, "Product1") = 90;
}

It is also possible to change the value of a particular grid cell internal methods of the grid’ view where the cell resides:

  • viewObj.SetRowCellValue (RowIndexColumnObjValue): Assigns Value to the cell specified by the row index and the column object.
  • viewObj.SetRowCellValue_2 (RowIndexFieldNameValue): Assigns Value to the cell specified by the row index and the column’s dataset field name.

Below is an example that demonstrates how to use these methods in scripts.

Example

The example works with the GridTutorials application.

How to get the application

View example description

JavaScript

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

  // Obtain the application process and its main form
  p = Sys.Process("GridTutorials");
  frmMain = p.WinFormsObject("frmMain");
  frmMain.Activate();
  // Select the "Multi Editors" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Editors";
  // Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1");

  // Change column values
  SetCellValue (Grid, null, 0, "Product1", "Chang");
  SetCellValue (Grid, null, 1, "Product1", "1"); // CategoryID for "Beverages"
  SetCellValue (Grid, null, 2, "Product1", "Exotic Liquids");
  SetCellValue (Grid, null, 3, "Product1", "24 - 12 oz bottles");
  SetCellValue (Grid, null, 4, "Product1", 15.2);
  SetCellValue (Grid, null, 5, "Product1", 17);
  SetCellValue (Grid, null, 6, "Product1", false);
  SetCellValue (Grid, null, 7, "Product1", "3/19/2007");
  SetCellValue (Grid, null, 8, "Product1", 90);
}

function SetCellValue (Grid, View, RowIndex, ColumnId, Value)
{
  // Get the grid view object, if it is not specified
  if (strictEqual(View, null))
    View = Grid.MainView;
  // Get the column object
  var Column = GetColumn (Grid, View, ColumnId);
  // Change the cell value
  View.SetRowCellValue (RowIndex, Column, Value);
}

function GetColumn (Grid, View, ColumnId)
{
  var Columns, Col;

  // Get the grid view object, if it is not specified
  if (strictEqual(View, null))
    View = Grid.MainView;

  Columns = View.Columns;
  // 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<Columns.Count; i++)
    {
      Col = Columns.Item_2 (i);
      if (equal(Col.Caption.OleValue, ColumnId))
        return Col; // The column is found
    }
    return null; // The column is not found
  }
  else
    // The column is specified by index
    return Columns.Item_2 (ColumnId);
}

JScript

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

  // Obtain the application process and its main form
  p = Sys.Process("GridTutorials");
  frmMain = p.WinFormsObject("frmMain");
  frmMain.Activate();
  // Select the "Multi Editors" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Editors";
  // Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1");

  // Change column values
  SetCellValue (Grid, null, 0, "Product1", "Chang");
  SetCellValue (Grid, null, 1, "Product1", "1"); // CategoryID for "Beverages"
  SetCellValue (Grid, null, 2, "Product1", "Exotic Liquids");
  SetCellValue (Grid, null, 3, "Product1", "24 - 12 oz bottles");
  SetCellValue (Grid, null, 4, "Product1", 15.2);
  SetCellValue (Grid, null, 5, "Product1", 17);
  SetCellValue (Grid, null, 6, "Product1", false);
  SetCellValue (Grid, null, 7, "Product1", "3/19/2007");
  SetCellValue (Grid, null, 8, "Product1", 90);
}

function SetCellValue (Grid, View, RowIndex, ColumnId, Value)
{
  // Get the grid view object, if it is not specified
  if (View == null)
    View = Grid.MainView;
  // Get the column object
  var Column = GetColumn (Grid, View, ColumnId);
  // Change the cell value
  View.SetRowCellValue (RowIndex, Column, Value);
}

function GetColumn (Grid, View, ColumnId)
{
  var Columns, Col, i;

  // Get the grid view object, if it is not specified
  if (View == null)
    View = Grid.MainView;

  Columns = View.Columns;
  // 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<Columns.Count; i++)
    {
      Col = Columns.Item_2 (i);
      if (Col.Caption.OleValue == ColumnId)
        return Col; // The column is found
    }
    return null; // The column is not found
  }
  else
    // The column is specified by index
    return Columns.Item_2 (ColumnId);
}

Python

def Main ():

  # Obtain the application process and its main form
  p = Sys.Process("GridTutorials")
  frmMain = p.WinFormsObject("frmMain")
  frmMain.Activate()
  # Select the "Multi Editors" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Editors"
  # Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  # Change column values
  SetCellValue (Grid, None, 0, "Product1", "Chang")
  SetCellValue (Grid, None, 1, "Product1", "1") # CategoryID for "Beverages"
  SetCellValue (Grid, None, 2, "Product1", "Exotic Liquids")
  SetCellValue (Grid, None, 3, "Product1", "24 - 12 oz bottles")
  SetCellValue (Grid, None, 4, "Product1", 15.2)
  SetCellValue (Grid, None, 5, "Product1", 17)
  SetCellValue (Grid, None, 6, "Product1", False)
  SetCellValue (Grid, None, 7, "Product1", "3/19/2007")
  SetCellValue (Grid, None, 8, "Product1", 90)

def SetCellValue (Grid, View, RowIndex, ColumnId, Value):
  # Get the grid view object, if it is not specified
  if (View == None):
    View = Grid.MainView
  # Get the column object
  Column = GetColumn (Grid, View, ColumnId)
  # Change the cell value
  View.SetRowCellValue (RowIndex, Column, Value)

def GetColumn (Grid, View, ColumnId):

  # Get the grid view object, if it is not specified
  if (View == None):
    View = Grid.MainView

  Columns = View.Columns
  # 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, Columns.Count-1):
      Col = Columns.Item_2 [i]
      if (Col.Caption.OleValue == ColumnId):
        return Col # The column is found
    return None # The column is not found
  else:
    # The column is specified by index
    return Columns.Item_2 [ColumnId]

VBScript

Sub Main
  Dim p, frmMain, Grid

  ' Obtain the application process and its main form
  Set p = Sys.Process("GridTutorials")
  Set frmMain = p.WinFormsObject("frmMain")
  frmMain.Activate
  ' Select the "Multi Editors" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Editors"
  ' Obtain the grid object
  Set Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  ' Change column values
  Call SetCellValue (Grid, Nothing, 0, "Product1", "Chang")
  Call SetCellValue (Grid, Nothing, 1, "Product1", "1") ' CategoryID for "Beverages"
  Call SetCellValue (Grid, Nothing, 2, "Product1", "Exotic Liquids")
  Call SetCellValue (Grid, Nothing, 3, "Product1", "24 - 12 oz bottles")
  Call SetCellValue (Grid, Nothing, 4, "Product1", 15.2)
  Call SetCellValue (Grid, Nothing, 5, "Product1", 17)
  Call SetCellValue (Grid, Nothing, 6, "Product1", False)
  Call SetCellValue (Grid, Nothing, 7, "Product1", "3/19/2007")
  Call SetCellValue (Grid, Nothing, 8, "Product1", 90)
End Sub

Sub SetCellValue (Grid, View, RowIndex, ColumnId, Value)
  Dim Column
  ' Get the grid view object, if it is not specified
  If View Is Nothing Then
    Set View = Grid.MainView
  End If 
  ' Get the column object
  Set Column = GetColumn (Grid, View, ColumnId)
  ' Change the cell value
  Call View.SetRowCellValue (RowIndex, Column, Value)
End Sub

Function GetColumn (Grid, View, ColumnId)
  Dim Columns, Col, i

  ' Get the grid view, if it is not specified
  If View Is Nothing Then
    Set View = Grid.MainView
  End If

  Set Columns = View.Columns
  ' 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 Columns.Count-1
      Set Col = Columns.Item_2 (i)
      If Col.Caption.OleValue = ColumnId Then
        Set GetColumn = Col ' The column is found
        Exit Function
      End If
    Next
    Set GetColumn = Nothing' The column is not found
  Else
    ' The column is specified by index
    Set GetColumn = Columns.Item_2 (ColumnId)
  End If
End Function

DelphiScript

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

procedure Main;
var p, frmMain, Grid : OleVariant;
begin
  // Obtain the application process and its main form
  p := Sys.Process('GridTutorials');
  frmMain := p.WinFormsObject('frmMain');
  frmMain.Activate;
  // Select the 'Multi Editors' demo
  frmMain.WinFormsObject('gcNavigations').WinFormsObject('listBoxControl1').SelectedItem := 'Multi Editors';
  // Obtain the grid object
  Grid := frmMain.WinFormsObject('pcMain').WinFormsObject('gcContainer').WinFormsObject('Form1').WinFormsObject('gridControl1');

  // Change column values
  SetCellValue (Grid, nil, 0, 'Product1', 'Chang');
  SetCellValue (Grid, nil, 1, 'Product1', '1'); // CategoryID for 'Beverages'
  SetCellValue (Grid, nil, 2, 'Product1', 'Exotic Liquids');
  SetCellValue (Grid, nil, 3, 'Product1', '24 - 12 oz bottles');
  SetCellValue (Grid, nil, 4, 'Product1', 15.2);
  SetCellValue (Grid, nil, 5, 'Product1', 17);
  SetCellValue (Grid, nil, 6, 'Product1', false);
  SetCellValue (Grid, nil, 7, 'Product1', '3/19/2007');
  SetCellValue (Grid, nil, 8, 'Product1', 90);
end;

procedure SetCellValue (Grid, View, RowIndex, ColumnId, Value);
var Column : OleVariant;
begin
  // Get the grid view object, if it is not specified
  if View = nil then
    View := Grid.MainView;
  // Get the column object
  Column := GetColumn (Grid, View, ColumnId);
  // Change the cell value
  View.SetRowCellValue (RowIndex, Column, Value);
end;

function GetColumn (Grid, View, ColumnId);
var Columns, Col, i : OleVariant;
begin
  // Get the grid view object, if it is not specified
  if View = nil then
    View := Grid.MainView;

  Columns := View.Columns;
  // 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 Columns.Count-1 do
    begin
      Col := Columns.Item_2[i];
      if Col.Caption.OleValue = ColumnId then
      begin
        Result := Col; // The column is found
        Exit
      end
    end;
    Result := nil; // The column is not found
  end
  else
    // The column is specified by index
    Result := Columns.Item_2[ColumnId];
end;

C++Script, C#Script

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

  // Obtain the application process and its main form
  p = Sys["Process"]("GridTutorials");
  frmMain = p["WinFormsObject"]("frmMain");
  frmMain["Activate"]();
  // Select the "Multi Editors" demo
  frmMain["WinFormsObject"]("gcNavigations")["WinFormsObject"]("listBoxControl1")["SelectedItem"] = "Multi Editors";
  // Obtain the grid object
  Grid = frmMain["WinFormsObject"]("pcMain")["WinFormsObject"]("gcContainer")["WinFormsObject"]("Form1")["WinFormsObject"]("gridControl1");

  // Change column values
  SetCellValue (Grid, null, 0, "Product1", "Chang");
  SetCellValue (Grid, null, 1, "Product1", "1"); // CategoryID for "Beverages"
  SetCellValue (Grid, null, 2, "Product1", "Exotic Liquids");
  SetCellValue (Grid, null, 3, "Product1", "24 - 12 oz bottles");
  SetCellValue (Grid, null, 4, "Product1", 15.2);
  SetCellValue (Grid, null, 5, "Product1", 17);
  SetCellValue (Grid, null, 6, "Product1", false);
  SetCellValue (Grid, null, 7, "Product1", "3/19/2007");
  SetCellValue (Grid, null, 8, "Product1", 90);
}

function SetCellValue (Grid, View, RowIndex, ColumnId, Value)
{
  // Get the grid view object, if it is not specified
  if (View == null)
    View = Grid["MainView"];
  // Get the column object
  var Column = GetColumn (Grid, View, ColumnId);
  // Change the cell value
  View["SetRowCellValue"](RowIndex, Column, Value);
}

function GetColumn (Grid, View, ColumnId)
{
  var Columns, Col, i;

  // Get the grid view object, if it is not specified
  if (View == null)
    View = Grid["MainView"];

  Columns = View["Columns"];
  // 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<Columns["Count"]; i++)
    {
      Col = Columns["Item_2"](i);
      if (Col["Caption"]["OleValue"] == ColumnId)
        return Col; // The column is found
    }
    return null; // The column is not found
  }
  else
    // The column is specified by index
    return Columns["Item_2"](ColumnId);
}

See Also

Working With Developer Express XtraGrid
Accessing Views in Developer Express XtraGrid
Selecting Cells in Developer Express XtraGrid
Activating and Closing In-place Editors in Developer Express XtraGrid
Working With Specific In-place Editors in Developer Express XtraGrid
wValue Property (Grid Controls)

Highlight search results