Selecting Cells in Developer Express XtraGrid

Applies to TestComplete 15.68, last modified on October 16, 2024

One of the actions that you will perform the most over a grid is selecting cells. This topic describes various approaches that can be used to select a particular cell in the XtraGrid control. Note that before selecting a cell you should identify the row and column in which the desired cell resides. For example, you can search for the row by the value or text in a particular column. To learn how you can search for grid records, see Searching for Records in Developer Express XtraGrid.

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.

Simulating Clicks on Cells

To simulate clicks on XtraGrid cells, you can use the ClickCell, ClickCellR, DblClickCell and similar actions of the DevExpressXtraGrid or DevExpressXtraGridView objects. The actions of the DevExpressXtraGrid relate to the grid’s main data view, while the actions of the DevExpressXtraGridView relate to corresponding child views. All of these mouse-actions have parameters that specify the row (card) and column (card field) that contain the desired cell. They also have an additional parameter that specifies the key or a combination of keys (Ctrl, Alt, Shift) that are pressed during the simulation of a click.

Example

The example works with the GridTutorials application.

How to get the application

View example description

JavaScript

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

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

  // Locate a row by its cell text
  RowIndex = FindRowByCellText (Grid, null, "Product", "Raclette Courdavault");
  // Change the row data
  Grid.ClickCell (RowIndex, "Product");
  Grid.Keys ("Mozzarella di Giovanni" + "[Enter]");
  Grid.ClickCell (RowIndex, "Unit Price");
  Grid.Keys ("^a[Del]" + "12.47" + "[Enter]");
  Grid.ClickCell (RowIndex, "Quantity");
  Grid.Keys ("^a[Del]" + "40" + "[Enter]");
}

// Locates a row by the cell text
function FindRowByCellText (Grid, View, ColumnId, Text)
{
  // Get the grid view, if it is not specified
  if (strictEqual(View, null))
    View = Grid.MainView;

  // Get the column object
  var Column = GetColumn (Grid, View, ColumnId);
  // Locate a row
  return View.LocateByDisplayText (0, Column, Text);
}

// Returns a column object specified by caption or index
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, RowIndex;

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

  // Locate a row by its cell text
  RowIndex = FindRowByCellText (Grid, null, "Product", "Raclette Courdavault");
  // Change the row data
  Grid.ClickCell (RowIndex, "Product");
  Grid.Keys ("Mozzarella di Giovanni" + "[Enter]");
  Grid.ClickCell (RowIndex, "Unit Price");
  Grid.Keys ("^a[Del]" + "12.47" + "[Enter]");
  Grid.ClickCell (RowIndex, "Quantity");
  Grid.Keys ("^a[Del]" + "40" + "[Enter]");
}

// Locates a row by the cell text
function FindRowByCellText (Grid, View, ColumnId, Text)
{
  // 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);
  // Locate a row
  return View.LocateByDisplayText (0, Column, Text);
}

// Returns a column object specified by caption or index
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 "Add Group Row (Grouped Mode)" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Add New Row (Grouped Mode)"
  # Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  # Locate a row by its cell text
  RowIndex = FindRowByCellText (Grid, None, "Product", "Raclette Courdavault")
  # Change the row data
  Grid.ClickCell (RowIndex, "Product")
  Grid.Keys ("Mozzarella di Giovanni" + "[Enter]")
  Grid.ClickCell (RowIndex, "Unit Price")
  Grid.Keys ("^a[Del]" + "12.47" + "[Enter]")
  Grid.ClickCell (RowIndex, "Quantity")
  Grid.Keys ("^a[Del]" + "40" + "[Enter]")

# Locates a row by the cell text
def FindRowByCellText (Grid, View, ColumnId, Text):
  # Get the grid view, if it is not specified
  if (View == None):
    View = Grid.MainView

  # Get the column object
  Column = GetColumn (Grid, View, ColumnId)
  # Locate a row
  return View.LocateByDisplayText (0, Column, Text)

# Returns a column object specified by caption or index
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, RowIndex

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

  ' Locate a row by its cell text
  RowIndex = FindRowByCellText (Grid, Nothing, "Product", "Raclette Courdavault")
  ' Change the row data
  Call Grid.ClickCell (RowIndex, "Product")
  Call Grid.Keys ("Mozzarella di Giovanni" & "[Enter]")
  Call Grid.ClickCell (RowIndex, "Unit Price")
  Call Grid.Keys ("^a[Del]" & "12.47" & "[Enter]")
  Call Grid.ClickCell (RowIndex, "Quantity")
  Call Grid.Keys ("^a[Del]" & "40" & "[Enter]")
End Sub

' Locates a row by the cell text
Function FindRowByCellText (Grid, View, ColumnId, Text)
  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)
  ' Locate a row
  FindRowByCellText = View.LocateByDisplayText (0, Column, Text)
End Function

' Returns a column object specified by caption or index
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 FindRowByCellText (Grid, View, ColumnId, Text); forward;
function GetColumn (Grid, View, ColumnId); forward;

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

  // Locate a row by its cell text
  RowIndex := FindRowByCellText (Grid, nil, 'Product', 'Raclette Courdavault');
  // Change the row data
  Grid.ClickCell (RowIndex, 'Product');
  Grid.Keys ('Mozzarella di Giovanni' + '[Enter]');
  Grid.ClickCell (RowIndex, 'Unit Price');
  Grid.Keys ('^a[Del]' + '12.47' + '[Enter]');
  Grid.ClickCell (RowIndex, 'Quantity');
  Grid.Keys ('^a[Del]' + '40' + '[Enter]');
end;

// Locates a row by the cell text
function FindRowByCellText (Grid, View, ColumnId, Text);
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);
  // Locate a row
  Result := View.LocateByDisplayText (0, Column, Text);
end;

// Returns a column object specified by caption or index
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, RowIndex;

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

  // Locate a row by its cell text
  RowIndex = FindRowByCellText (Grid, null, "Product", "Raclette Courdavault");
  // Change the row data
  Grid["ClickCell"](RowIndex, "Product");
  Grid["Keys"]("Mozzarella di Giovanni" + "[Enter]");
  Grid["ClickCell"](RowIndex, "Unit Price");
  Grid["Keys"]("^a[Del]" + "12.47" + "[Enter]");
  Grid["ClickCell"](RowIndex, "Quantity");
  Grid["Keys"]("^a[Del]" + "40" + "[Enter]");
}

// Locates a row by the cell text
function FindRowByCellText (Grid, View, ColumnId, Text)
{
  // 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);
  // Locate a row
  return View["LocateByDisplayText"](0, Column, Text);
}

// Returns a column object specified by caption or index
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);
}

Simulating Keyboard Shortcuts

The XtraGrid control supports various keyboard shortcuts that can be used to navigate through the grid. For example, pressing the Down Arrow key selects the next row, and Ctrl+Home navigates to the grid’s upper-left cell. For more information on what shortcuts can be used in the XtraGrid control, see the XtraGrid Suite documentation.

To simulate a keyboard shortcut, use the Keys action common to all onscreen objects. The following code snippet demonstrates how to simulate the Ctrl+Home shortcut in order to select the grid’s upper left cell:

JavaScript, JScript

Grid.Keys ("^[Home]");

Python

Grid.Keys ("^[Home]")

VBScript

Grid.Keys "^[Home]"

DelphiScript

Grid.Keys ('^[Home]');

C++Script, C#Script

Grid["Keys"]("^[Home]");

Using XtraGrid Internal Members

The XtraGrid control has a number of internal properties and methods that can be used to select a particular cell. They are listed in the following table:

Property (Method) Description
FocusedRowHandle Gets or sets the index (zero-based) of the currently selected row (card).
FocusedColumn Gets or sets the currently focused column (card field).
FocusedView Gets or sets the currently focused view.
MoveFirst() Selects the first visible row (card) within the view.
MoveNext() Selects the row (card) following the currently focused one.
MovePrev() Selects the row (card) preceding the currently focused one.
MoveLast() Selects the last row (card) within the view.
MoveLastVisible() Selects the last visible row (card) within the view.
MoveLastVisible(delta) Moves the row (card) focus by the specified number of rows (cards).

Below is an example that demonstrates how you can select a cell in the XtraGrid control using its internal methods. The SelectCell routine from this example can be used for all XtraGrid layouts.

Example

The example works with the GridTutorials application.

How to get the application

View example description

JavaScript

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

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

  // Locate the row that contains the "Raclette Courdavault" text in the "Product" column
  RowIndex = FindRowByCellText (Grid, null, "Product", "Raclette Courdavault");

  // Change the row data
  SelectCell (Grid, null, RowIndex, "Product");
  Grid.Keys ("Mozzarella di Giovanni" + "[Enter]");
  SelectCell (Grid, null, RowIndex, "Unit Price");
  Grid.Keys ("12.47" + "[Enter]");
  SelectCell (Grid, null, RowIndex, "Quantity");
  Grid.Keys ("40" + "[Enter]");
}

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

  // Set the focused view
  Grid.FocusedView = View;
  // Change the row and column focus
  View.FocusedRowHandle = RowIndex;
  View.FocusedColumn = GetColumn (Grid, View, ColumnId);
}

function FindRowByCellText (Grid, View, ColumnId, Text)
{
  var Column;

  if (strictEqual(View, null))
    View = Grid.MainView;

  Column = GetColumn (Grid, View, ColumnId);
  return View.LocateByDisplayText (0, Column, Text);
}

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

  // 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, RowIndex;

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

  // Locate the row that contains the "Raclette Courdavault" text in the "Product" column
  RowIndex = FindRowByCellText (Grid, null, "Product", "Raclette Courdavault");

  // Change the row data
  SelectCell (Grid, null, RowIndex, "Product");
  Grid.Keys ("Mozzarella di Giovanni" + "[Enter]");
  SelectCell (Grid, null, RowIndex, "Unit Price");
  Grid.Keys ("12.47" + "[Enter]");
  SelectCell (Grid, null, RowIndex, "Quantity");
  Grid.Keys ("40" + "[Enter]");
}

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

  // Set the focused view
  Grid.FocusedView = View;
  // Change the row and column focus
  View.FocusedRowHandle = RowIndex;
  View.FocusedColumn = GetColumn (Grid, View, ColumnId);
}

function FindRowByCellText (Grid, View, ColumnId, Text)
{
  var Column;

  if (View == null)
    View = Grid.MainView;

  Column = GetColumn (Grid, View, ColumnId);
  return View.LocateByDisplayText (0, Column, Text);
}

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 "Add New Row (Grouped Mode)" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Add New Row (Grouped Mode)"
  # Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  # Locate the row that contains the "Raclette Courdavault" text in the "Product" column
  RowIndex = FindRowByCellText (Grid, None, "Product", "Raclette Courdavault")

  # Change the row data
  SelectCell (Grid, None, RowIndex, "Product")
  Grid.Keys ("Mozzarella di Giovanni" + "[Enter]")
  SelectCell (Grid, None, RowIndex, "Unit Price")
  Grid.Keys ("12.47" + "[Enter]")
  SelectCell (Grid, None, RowIndex, "Quantity")
  Grid.Keys ("40" + "[Enter]")

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

  # Set the focused view
  Grid.FocusedView = View
  # Change the row and column focus
  View.FocusedRowHandle = RowIndex
  View.FocusedColumn = GetColumn (Grid, View, ColumnId)

def FindRowByCellText (Grid, View, ColumnId, Text):

  if (View == None):
    View = Grid.MainView

  Column = GetColumn (Grid, View, ColumnId)
  return View.LocateByDisplayText (0, Column, Text)

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, RowIndex

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

  ' Locate the row that contains the "Raclette Courdavault" text in the "Product" column
  RowIndex = FindRowByCellText (Grid, Nothing, "Product", "Raclette Courdavault")

  ' Change the row data
  Call SelectCell (Grid, Nothing, RowIndex, "Product")
  Call Grid.Keys ("Mozzarella di Giovanni" & "[Enter]")
  Call SelectCell (Grid, Nothing, RowIndex, "Unit Price")
  Call Grid.Keys ("^a[Del]" & "12.47" & "[Enter]")
  Call SelectCell (Grid, Nothing, RowIndex, "Quantity")
  Call Grid.Keys ("^a[Del]" & "40" & "[Enter]")
End Sub

Sub SelectCell (Grid, View, RowIndex, ColumnId)
  ' Get the grid view object, if it is not specified
  If View Is Nothing Then
    Set View = Grid.MainView
  End If

  ' Set the focused view
  Set Grid.FocusedView = View
  ' Change the row and column focus
  View.FocusedRowHandle = RowIndex
  Set View.FocusedColumn = GetColumn (Grid, View, ColumnId)
End Sub

Function FindRowByCellText (Grid, View, ColumnId, Text)
  Dim Column

  If View Is Nothing Then
    Set View = Grid.MainView
  End If

  Set Column = GetColumn (Grid, View, ColumnId)
  FindRowByCellText = View.LocateByDisplayText (0, Column, Text)
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

procedure SelectCell (Grid, View, RowIndex, ColumnId); forward;
function FindRowByCellText (Grid, View, ColumnId, Text); forward;
function GetColumn (Grid, View, ColumnId); forward;

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

  // Locate the row that contains the 'Raclette Courdavault' text in the 'Product' column
  RowIndex := FindRowByCellText (Grid, nil, 'Product', 'Raclette Courdavault');

  // Change the row data
  SelectCell (Grid, nil, RowIndex, 'Product');
  Grid.Keys ('Mozzarella di Giovanni' + '[Enter]');
  SelectCell (Grid, nil, RowIndex, 'Unit Price');
  Grid.Keys ('12.47' + '[Enter]');
  SelectCell (Grid, nil, RowIndex, 'Quantity');
  Grid.Keys ('40' + '[Enter]');
end;

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

  // Set the focused view
  Grid.FocusedView := View;
  // Change the row and column focus
  View.FocusedRowHandle := RowIndex;
  View.FocusedColumn := GetColumn (Grid, View, ColumnId);
end;

function FindRowByCellText (Grid, View, ColumnId, Text);
var Column : OleVariant;
begin
  if View = nil then
    View := Grid.MainView;

  Column := GetColumn (Grid, View, ColumnId);
  Result := View.LocateByDisplayText (0, Column, Text);
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, RowIndex;

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

  // Locate the row that contains the "Raclette Courdavault" text in the "Product" column
  RowIndex = FindRowByCellText (Grid, null, "Product", "Raclette Courdavault");

  // Change the row data
  SelectCell (Grid, null, RowIndex, "Product");
  Grid["Keys"]("Mozzarella di Giovanni" + "[Enter]");
  SelectCell (Grid, null, RowIndex, "Unit Price");
  Grid["Keys"]("12.47" + "[Enter]");
  SelectCell (Grid, null, RowIndex, "Quantity");
  Grid["Keys"]("40" + "[Enter]");
}

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

  // Set the focused view
  Grid["FocusedView"] = View;
  // Change the row and column focus
  View["FocusedRowHandle"] = RowIndex;
  View["FocusedColumn"] = GetColumn (Grid, View, ColumnId);
}

function FindRowByCellText (Grid, View, ColumnId, Text)
{
  var Column;

  if (View == null)
    View = Grid["MainView"];

  Column = GetColumn (Grid, View, ColumnId);
  return View["LocateByDisplayText"](0, Column, Text);
}

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);
}

Using the Incremental Search

The XtraGrid control supports the incremental search feature. It means that the user can select a grid cell by typing initial characters of the cell’s text. Incremental search may be useful when navigating through grids that contain a large amount of data. Note that this feature is available in all XtraGrid views except for the card view, and is enabled only if the view’s OptionsBehavior.AllowIncrementalSearch option is set to True.

To select a grid cell using the incremental search, you should focus the needed column and send the desired cell’s text to the grid control. The first cell in this column whose text matches the inputted one will become selected. Note that the search is performed downward starting from the initially selected cell.

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 "Incremental Search" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Incremental Search";
  // Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1");

  // Stop the active demo
  Grid.Keys ("[Esc]");

  // Select the cell using incremental search
  SelectCellByIncSearch (Grid, null, "Product", "Raclette Courdavault");
}

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

  View.FocusedColumn = GetColumn (Grid, View, ColumnId);
  Grid.Keys (Text + "[Enter]");
}

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 (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");

  // Select the "Incremental Search" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Incremental Search";
  // Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1");

  // Stop the active demo
  Grid.Keys ("[Esc]");

  // Select the cell using incremental search
  SelectCellByIncSearch (Grid, null, "Product", "Raclette Courdavault");
}

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

  View.FocusedColumn = GetColumn (Grid, View, ColumnId);
  Grid.Keys (Text + "[Enter]");
}

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 "Incremental Search" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Incremental Search"
  # Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  # Stop the active demo
  Grid.Keys ("[Esc]")

  # Select the cell using incremental search
  SelectCellByIncSearch (Grid, None, "Product", "Raclette Courdavault")

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

  View.FocusedColumn = GetColumn (Grid, View, ColumnId)
  Grid.Keys (Text + "[Enter]")

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")

  ' Select the "Incremental Search" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Incremental Search"
  ' Obtain the grid object
  Set Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  ' Stop the active demo
  Call Grid.Keys ("[Esc]")

  ' Select the cell using incremental search
  Call SelectCellByIncSearch (Grid, Nothing, "Product", "Raclette Courdavault")
End Sub

Sub SelectCellByIncSearch (Grid, View, ColumnId, Text)
  ' Get the grid view object, if it is not specified
  If View Is Nothing Then
    Set View = Grid.MainView
  End If

  Set View.FocusedColumn = GetColumn (Grid, View, ColumnId)
  Call Grid.Keys (Text & "[Enter]")
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

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;

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

  View.FocusedColumn := GetColumn (Grid, View, ColumnId);
  Grid.Keys (Text + 'Enter');
end;

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 'Incremental Search' demo
  frmMain.WinFormsObject('gcNavigations').WinFormsObject('listBoxControl1').SelectedItem := 'Incremental Search';
  // Obtain the grid object
  Grid := frmMain.WinFormsObject('pcMain').WinFormsObject('gcContainer').WinFormsObject('Form1').WinFormsObject('gridControl1');

  // Stop the active demo
  Grid.Keys ('[Esc]');

  // Select the cell using incremental search
  SelectCellByIncSearch (Grid, nil, 'Product', 'Raclette Courdavault');
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 "Incremental Search" demo
  frmMain["WinFormsObject"]("gcNavigations")["WinFormsObject"]("listBoxControl1")["SelectedItem"] = "Incremental Search";
  // Obtain the grid object
  Grid = frmMain["WinFormsObject"]("pcMain")["WinFormsObject"]("gcContainer")["WinFormsObject"]("Form1")["WinFormsObject"]("gridControl1");

  // Stop the active demo
  Grid["Keys"]("[Esc]");

  // Select the cell using incremental search
  SelectCellByIncSearch (Grid, null, "Product", "Raclette Courdavault");
}

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

  View["FocusedColumn"] = GetColumn (Grid, View, ColumnId);
  Grid["Keys"](Text + "[Enter]");
}

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
Obtaining and Setting Cell Values in Developer Express XtraGrid
Selecting Multiple Rows and Cards in Developer Express XtraGrid
ClickCell Action (Grid Controls)

Highlight search results