Searching for Records in Developer Express XtraGrid

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

In order to perform various actions over the XtraGrid control, you first need to locate the row (card) that contains the data that you are going to work with. This topic describes several approaches that you can use to search for the desired grid record.

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.

Searching in the Underlying Dataset

If the grid’s data is bound to an external dataset, you can search for the desired record within the dataset rather than within the grid’s view. Searching within the grid’s dataset is a universal approach that is free from the grid’s user interface limitations. However, in this way you can search for a record by its values rather than by their displayed representation in the grid. To search for records by the text displayed, you can use the methods of XtraGrid control view objects.

To obtain the XtraGrid control’s dataset, use the DataSource. In a similar way, you can use the DataSource property of a particular grid view to get the dataset displayed in this view. Since the XtraGrid control can be bound to various dataset types, the DataSource property can return objects of different types. If the grid is bound to a database table or an XML file, the DataSource can return a .NET DataSource, DataTable or a DataView object. If the grid or its view is bound to a custom collection of objects, the DataSource property will return this collection. You can then search for a dataset record using the corresponding methods of the returned object. To determine which methods you can use, either explore the object returned by the DataSource property in the Object Browser panel, or ask the tested application’s developers.

In the case of the database binding, the DataSource property of a particular grid view will return the DataView object representing the data displayed by the view. To search in the DataView object, you can use its Find or FindRows methods. The difference between these methods is that Find returns the index of the first found row that matches the search criteria, whereas FindRows returns the array of DataRowView objects corresponding to all matching rows. The parameter of both methods is the sought-for value.

Searching a DataView object implies the following steps:

  • Specify the name of the data table field in which the search is performed in the DataView.Sort property. You can determine a grid column’s data field name using its FieldName property.
  • To perform a case-sensitive search, set the DataView.Table.CaseSensitive property to True.
  • Call the Find or FindRows method and specify the sought-for value as a parameter. Note that to search for an object value, you need to create an instance of the corresponding .NET class. For more information on how you can do this, see Calling Functions From .NET Assemblies.

For more information on how to search within a DataView object, see the Finding Rows article in the MSDN library.

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 by the "Unit Price" value
  RowIndex = FindRowInDataset (Grid, null, "Unit Price", 36.8);
  if (RowIndex != -1)
    Grid.ClickCell (RowIndex, "Unit Price")
  else
    Log.Error ("Row was not found.");
}

function FindRowInDataset (Grid, View, ColumnId, Value)
{
  var DataView, index;

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

  // Search for the specified value in the view's data source
  DataView = View.DataSource;
  DataView.Sort = GetColumn(Grid, View, ColumnId).FieldName;
  index = DataView.Find (Value);

  if (equal(index, -1))
    return index; // Row is not found
  else
    // Row is found; convert the dataset row index to a grid row index
    return View.GetRowHandle (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 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 by the "Unit Price" value
  RowIndex = FindRowInDataset (Grid, null, "Unit Price", 36.8);
  if (RowIndex != -1)
    Grid.ClickCell (RowIndex, "Unit Price")
  else
    Log.Error ("Row was not found.");
}

function FindRowInDataset (Grid, View, ColumnId, Value)
{
  var DataView, index;

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

  // Search for the specified value in the view's data source
  DataView = View.DataSource;
  DataView.Sort = GetColumn(Grid, View, ColumnId).FieldName;
  index = DataView.Find (Value);

  if (index == -1)
    return index; // Row is not found
  else
    // Row is found; convert the dataset row index to a grid row index
    return View.GetRowHandle (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 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 by the "Unit Price" value
  RowIndex = FindRowInDataset (Grid, None, "Unit Price", 36.8)
  if (RowIndex != -1):
    Grid.ClickCell (RowIndex, "Unit Price")
  else:
    Log.Error ("Row was not found.")

def FindRowInDataset (Grid, View, ColumnId, Value):

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

  # Search for the specified value in the views data source
  DataView = View.DataSource
  DataView.Sort = GetColumn(Grid, View, ColumnId).FieldName
  index = DataView.Find (Value)

  if (index == -1):
    return index # Row is not found
  else:
    # Row is found convert the dataset row index to a grid row index
    return View.GetRowHandle (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):
      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 by the "Unit Price" value
  RowIndex = FindRowInDataset (Grid, Nothing, "Unit Price", 36.8)
  If RowIndex <> -1 Then
    Call Grid.ClickCell (RowIndex, "Unit Price")
  Else
    Log.Error ("Row was not found.")
  End If
End Sub

Function FindRowInDataset (Grid, View, ColumnId, Value)
  Dim DataView, index

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

  ' Search for the specified value in the view's data source
  Set DataView = View.DataSource
  DataView.Sort = GetColumn(Grid, View, ColumnId).FieldName
  index = DataView.Find (Value)

  If index = -1 Then
    ' Row is not found
    FindRowInDataset = index
  Else
    ' Row is found; convert the dataset row index to a grid row index
    FindRowInDataset = View.GetRowHandle (index)
  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 FindRowInDataset (Grid, View, ColumnId, Value); 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 by the 'Unit Price' value
  RowIndex := FindRowInDataset (Grid, nil, 'Unit Price', 36.8);
  if RowIndex <> -1 then
    Grid.ClickCell (RowIndex, 'Unit Price')
  else
    Log.Error ('Row was not found.');
end;

function FindRowInDataset (Grid, View, ColumnId, Value);
var DataView, index : OleVariant;
begin
  // Get the grid view object, if it is not specified
  if View = nil then
    View := Grid.MainView;

  // Search for the specified value in the view's data source
  DataView := View.DataSource;
  DataView.Sort := GetColumn(Grid, View, ColumnId).FieldName;
  index := DataView.Find (Value);

  if index = -1 then
    // Row is not found
    Result := index
  else
    // Row is found; convert the dataset row index to a grid row index
    Result := View.GetRowHandle (index);
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 by the "Unit Price" value
  RowIndex = FindRowInDataset (Grid, null, "Unit Price", 36.8);
  if (RowIndex != -1)
    Grid["ClickCell"](RowIndex, "Unit Price")
  else
    Log["Error"]("Row was not found.");
}

function FindRowInDataset (Grid, View, ColumnId, Value)
{
  var View, DataView, index;

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

  // Search for the specified value in the view's data source
  DataView = View["DataSource"];
  DataView["Sort"] = GetColumn(Grid, View, ColumnId)["FieldName"];
  index = DataView["Find"](Value);

  if (index == -1)
    return index; // Row is not found
  else
    // Row is found; convert the dataset row index to a grid row index
    return View["GetRowHandle"](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);
}

Searching Using XtraGrid’s Internal Methods

The XtraGrid control has built-in methods that you can use to search for a desired grid record. To locate a record in a particular grid view, you can use the following methods:

  • view.LocateByValue (StartRowIndexColumnObjValue) - Returns the zero-based index of the row (card) containing the specified value in the specified column.
  • view.LocateByDisplayText (StartRowIndexColumnObjText) - Returns the zero-based index of the row (card) containing the specified text in the specified column. You can use this method, for example, if cell values are displayed with a specific formatting applied.

The StartRowIndex is the first parameter of these methods and is the index of the row (card) from which the search starts. ColumnObj is the object corresponding to the grid column (card field) in which the sought-for value or text is contained. The last parameter is the value or text that is sought-for.

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 row by cell text
  RowIndex = FindRowByCellText (Grid, null, "Unit Price", "$36.80");
  if (RowIndex != -1)
    Grid.ClickCell (RowIndex, "Unit Price")
  else
    Log.Error ("Row was not found.");

  // Locate row by cell value
  RowIndex = FindRowByCellValue (Grid, null, "Unit Price", CreateDecimal(34.8));
  if (RowIndex != -1)
    Grid.ClickCell (RowIndex, "Unit Price")
  else
    Log.Error ("Row was not found.");
}

function CreateDecimal (Value)
{
  return Sys.Process("GridTutorials").AppDomain("GridTutorials.exe").dotNET.System.Decimal.zctor(Value);
}

function FindRowByCellValue (Grid, View, ColumnId, Value)
{
  var Column, Index;
  // Get the grid view object, if it is not specified
  if (strictEqual(View, null))
    View = Grid.MainView;
  // Get the column object
  Column = GetColumn (Grid, View, ColumnId);
  // Locate a row by the cell value
  Index = View.LocateByValue (0, Column, Value);
  if (equal(Index, Grid.InvalidRowHandle))
    return -1; // Row is not found
  else
    return Index; // Row is found
}

function FindRowByCellText (Grid, View, ColumnId, Text)
{
  var Column, Index;
  // Get the grid view object, if it is not specified
  if (strictEqual(View, null))
    View = Grid.MainView;
  // Get the column object
  Column = GetColumn (Grid, View, ColumnId);
  // Locate a row by the cell text
  Index = View.LocateByDisplayText (0, Column, Text);
  if (equal(Index, Grid.InvalidRowHandle))
    return -1; // Row is not found
  else
    return Index; // Row is found
}

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 (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, 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 row by cell text
  RowIndex = FindRowByCellText (Grid, null, "Unit Price", "$36.80");
  if (RowIndex != -1)
    Grid.ClickCell (RowIndex, "Unit Price")
  else
    Log.Error ("Row was not found.");

  // Locate row by cell value
  RowIndex = FindRowByCellValue (Grid, null, "Unit Price", CreateDecimal(34.8));
  if (RowIndex != -1)
    Grid.ClickCell (RowIndex, "Unit Price")
  else
    Log.Error ("Row was not found.");
}

function CreateDecimal (Value)
{
  return Sys.Process("GridTutorials").AppDomain("GridTutorials.exe").dotNET.System.Decimal.zctor(Value);
}

function FindRowByCellValue (Grid, View, ColumnId, Value)
{
  var Column, Index;
  // Get the grid view object, if it is not specified
  if (View == null)
    View = Grid.MainView;
  // Get the column object
  Column = GetColumn (Grid, View, ColumnId);
  // Locate a row by the cell value
  Index = View.LocateByValue (0, Column, Value);
  if (Index == Grid.InvalidRowHandle)
    return -1; // Row is not found
  else
    return Index; // Row is found
}

function FindRowByCellText (Grid, View, ColumnId, Text)
{
  var Column, Index;
  // Get the grid view object, if it is not specified
  if (View == null)
    View = Grid.MainView;
  // Get the column object
  Column = GetColumn (Grid, View, ColumnId);
  // Locate a row by the cell text
  Index = View.LocateByDisplayText (0, Column, Text);
  if (Index == Grid.InvalidRowHandle)
    return -1; // Row is not found
  else
    return Index; // Row is found
}

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 row by cell text
  RowIndex = FindRowByCellText (Grid, None, "Unit Price", "$36.80")
  if (RowIndex != -1):
    Grid.ClickCell (RowIndex, "Unit Price")
  else:
    Log.Error ("Row was not found.")

  # Locate row by cell value
  RowIndex = FindRowByCellValue (Grid, None, "Unit Price", CreateDecimal(34.8))
  if (RowIndex != -1):
    Grid.ClickCell (RowIndex, "Unit Price")
  else:
    Log.Error ("Row was not found.")

def CreateDecimal (Value):
  return Sys.Process("GridTutorials").AppDomain("GridTutorials.exe").dotNET.System.Decimal.zctor(Value)

def FindRowByCellValue (Grid, View, 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)
  # Locate a row by the cell value
  Index = View.LocateByValue (0, Column, Value)
  if (Index == Grid.InvalidRowHandle):
    return -1 # Row is not found
  else:
    return Index # Row is found

def FindRowByCellText (Grid, View, ColumnId, Text):
  # 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)
  # Locate a row by the cell text
  Index = View.LocateByDisplayText (0, Column, Text)
  if (Index == Grid.InvalidRowHandle):
    return -1 # Row is not found
  else:
    return Index # Row is found

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 row by cell text
  RowIndex = FindRowByCellText (Grid, Nothing, "Unit Price", "$36.80")
  If RowIndex <> -1 Then
    Call Grid.ClickCell (RowIndex, "Unit Price")
  Else
    Log.Error ("Row was not found.")
  End If

  ' Locate row by cell value
  RowIndex = FindRowByCellValue (Grid, Nothing, "Unit Price", CreateDecimal(34.8))
  If RowIndex <> -1 Then
    Call Grid.ClickCell (RowIndex, "Unit Price")
  Else
    Log.Error ("Row was not found.")
  End If
End Sub 

Function CreateDecimal (Value)
  Set CreateDecimal = Sys.Process("GridTutorials").AppDomain("GridTutorials.exe").dotNET.System.Decimal.zctor(Value)
End Function

Function FindRowByCellValue (Grid, View, ColumnId, Value)
  Dim Column, Index
  ' 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)
  ' Locate a row by the cell value
  Index = View.LocateByValue (0, Column, Value)
  If Index = Grid.InvalidRowHandle Then
    FindRowByCellValue = -1 ' Row is not found
  Else
    FindRowByCellValue = Index ' Row is found
  End If
End Function

Function FindRowByCellText (Grid, View, ColumnId, Text)
  Dim Column, Index
  ' 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)
  ' Locate a row by the cell text
  Index = View.LocateByDisplayText (0, Column, Text)
  If Index = Grid.InvalidRowHandle Then
    FindRowByCellText = -1 ' Row is not found
  Else
    FindRowByCellText = Index ' Row is found
  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 FindRowByCellValue (Grid, View, ColumnId, Value); forward;
function FindRowByCellText (Grid, View, ColumnId, Text); forward;
function CreateDecimal (Value); 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 row by cell text
  RowIndex := FindRowByCellText (Grid, nil, 'Unit Price', '$36.80');
  if RowIndex <> -1 then
    Grid.ClickCell (RowIndex, 'Unit Price')
  else
    Log.Error ('Row was not found.');

  // Locate row by cell value
  RowIndex := FindRowByCellValue (Grid, nil, 'Unit Price', CreateDecimal(34.8));
  if RowIndex <> -1 then
    Grid.ClickCell (RowIndex, 'Unit Price')
  else
    Log.Error ('Row was not found.');
end;

function CreateDecimal (Value);
begin
  Result := Sys.Process('GridTutorials').AppDomain('GridTutorials.exe').dotNET.System.Decimal.zctor(Value);
end;

function FindRowByCellValue (Grid, View, ColumnId, Value);
var Column, Index : 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);
  // Locate a row by the cell value
  Index := View.LocateByValue (0, Column, Value);
  if Index = Grid.InvalidRowHandle then
    Result := -1 // Row is not found
  else
    Result := Index; // Row is found
end;

function FindRowByCellText (Grid, View, ColumnId, Text);
var Column, Index : 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);
  // Locate a row by the cell text
  Index := View.LocateByDisplayText (0, Column, Text);
  if Index = Grid.InvalidRowHandle then
    Result := -1 // Row is not found
  else
    Result := Index; // Row is found
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 row by cell text
  RowIndex = FindRowByCellText (Grid, null, "Unit Price", "$36.80");
  if (RowIndex != -1)
    Grid["ClickCell"](RowIndex, "Unit Price")
  else
    Log["Error"]("Row was not found.");

  // Locate row by cell value
  RowIndex = FindRowByCellValue (Grid, null, "Unit Price", CreateDecimal(34.8));
  if (RowIndex != -1)
    Grid["ClickCell"](RowIndex, "Unit Price")
  else
    Log["Error"]("Row was not found.");
}

function CreateDecimal (Value)
{
  return Sys["Process"]("GridTutorials")["AppDomain"]("GridTutorials.exe")["dotNET"]["System"]["Decimal"]["zctor"](Value);
}

function FindRowByCellValue (Grid, View, ColumnId, Value)
{
  var Column, Index;
  // Get the grid view object, if it is not specified
  if (View == null)
    View = Grid["MainView"];
  // Get the column object
  Column = GetColumn (Grid, View, ColumnId);
  // Locate a row by the cell value
  Index = View["LocateByValue"](0, Column, Value);
  if (Index == Grid["InvalidRowHandle"])
    return -1; // Row is not found
  else
    return Index; // Row is found
}

function FindRowByCellText (Grid, View, ColumnId, Text)
{
  var View, Column, Index;
  // Get the grid view object, if it is not specified
  if (View == null)
    View = Grid["MainView"];
  // Get the column object
  Column = GetColumn (Grid, View, ColumnId);
  // Locate a row by the cell text
  Index = View["LocateByDisplayText"](0, Column, Text);
  if (Index == Grid["InvalidRowHandle"])
    return -1; // Row is not found
  else
    return Index; // Row is found
}

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 Feature

The XtraGrid control supports the incremental search feature. When the user starts typing into the grid, the first cell matching the inputted text becomes selected in the grid. You can use the incremental search to quickly locate and select a record with the desired cell text in the grid. Note that this feature is enabled in a particular grid view only if the view’s OptionsBehavior.AllowIncrementalSearch property is True.

To perform an incremental search, you can simulate inputting the sought-for string into the grid using the Keys action, or use the grid views’s StartIncrementalSearch method with the sought-for string as a parameter. In the example below, we use the StartIncrementalSearch method to avoid inputting data into the grid.

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 and select the cell
  RowIndex = FindRowByIncSearch (Grid, null, "Product", "Ipoh Coffee");
  if (RowIndex != -1)
    Log.Message ("Row index: " + RowIndex)
  else
    Log.Error ("The row was not found.");
}

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

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

  // Enable incremental search
  IncSearchEnabled = View.OptionsBehavior.AllowIncrementalSearch;
  View.OptionsBehavior.AllowIncrementalSearch = true;
      
  // Select the topmost cell in the specified column
  View.FocusedRowHandle = 0;
  View.FocusedColumn = Column;

  // Use incremental search
  View.StartIncrementalSearch (Text);
  View.StopIncrementalSearch();
  // Restore the initial incremental search setting
  View.OptionsBehavior.AllowIncrementalSearch = IncSearchEnabled;

  // Check if the search was successful
  if (equal(View.GetRowCellDisplayText (View.FocusedRowHandle, Column).OleValue, Text))
    return View.FocusedRowHandle // Row was found and selected
  else
    return -1; // Row was not found
}

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 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 and select the cell
  RowIndex = FindRowByIncSearch (Grid, null, "Product", "Ipoh Coffee");
  if (RowIndex != -1)
    Log.Message ("Row index: " + RowIndex)
  else
    Log.Error ("The row was not found.");
}

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

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

  // Enable incremental search
  IncSearchEnabled = View.OptionsBehavior.AllowIncrementalSearch;
  View.OptionsBehavior.AllowIncrementalSearch = true;
      
  // Select the topmost cell in the specified column
  View.FocusedRowHandle = 0;
  View.FocusedColumn = Column;

  // Use incremental search
  View.StartIncrementalSearch (Text);
  View.StopIncrementalSearch();
  // Restore the initial incremental search setting
  View.OptionsBehavior.AllowIncrementalSearch = IncSearchEnabled;

  // Check if the search was successful
  if (View.GetRowCellDisplayText (View.FocusedRowHandle, Column).OleValue == Text)
    return View.FocusedRowHandle // Row was found and selected
  else
    return -1; // Row was not found
}

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 and select the cell
  RowIndex = FindRowByIncSearch (Grid, None, "Product", "Ipoh Coffee")
  if (RowIndex != -1):
    Log.Message ("Row index: " + RowIndex)
  else:
    Log.Error ("The row was not found.")

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

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

  # Enable incremental search
  IncSearchEnabled = View.OptionsBehavior.AllowIncrementalSearch
  View.OptionsBehavior.AllowIncrementalSearch = True
      
  # Select the topmost cell in the specified column
  View.FocusedRowHandle = 0
  View.FocusedColumn = Column

  # Use incremental search
  View.StartIncrementalSearch (Text)
  View.StopIncrementalSearch()
  # Restore the initial incremental search setting
  View.OptionsBehavior.AllowIncrementalSearch = IncSearchEnabled

  # Check if the search was successful
  if (View.GetRowCellDisplayText (View.FocusedRowHandle, Column).OleValue == Text):
    return View.FocusedRowHandle # Row was found and selected
  else:
    return -1 # Row was not found

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 and select the cell
  RowIndex = FindRowByIncSearch (Grid, Nothing, "Product", "Ipoh Coffee")
  If RowIndex <> -1 Then
    Log.Message ("Row index: " & RowIndex)
  Else
    Log.Error ("The row was not found.")
  End If
End Sub

Function FindRowByIncSearch (Grid, View, ColumnId, Text)
  Dim Column, IncSearchEnabled

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

  ' Enable incremental search
  IncSearchEnabled = View.OptionsBehavior.AllowIncrementalSearch
  View.OptionsBehavior.AllowIncrementalSearch = True

  ' Select the topmost cell in the specified column
  View.FocusedRowHandle = 0
  Set View.FocusedColumn = Column

  ' Use incremental search
  View.StartIncrementalSearch (Text)
  View.StopIncrementalSearch
  ' Restore the initial incremental search setting
  View.OptionsBehavior.AllowIncrementalSearch = IncSearchEnabled

  ' Check if the search was successful
  If View.GetRowCellDisplayText(View.FocusedRowHandle, Column).OleValue = Text Then
    FindRowByIncSearch = View.FocusedRowHandle ' Row was found and selected
  Else
    FindRowByIncSearch = -1 ' Row was not found
  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 FindRowByIncSearch (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 and select the cell
  RowIndex := FindRowByIncSearch (Grid, nil, 'Product', 'Ipoh Coffee');
  if RowIndex <> -1 then
    Log.Message ('Row index: ' + aqConvert.VarToStr(RowIndex))
  else
    Log.Error ('The row was not found.');
end;

function FindRowByIncSearch (Grid, View, ColumnId, Text);
var Column, IncSearchEnabled : 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);

  // Enable incremental search
  IncSearchEnabled := View.OptionsBehavior.AllowIncrementalSearch;
  View.OptionsBehavior.AllowIncrementalSearch := true;
      
  // Select the topmost cell in the specified column
  View.FocusedRowHandle := 0;
  View.FocusedColumn := Column;

  // Use incremental search
  View.StartIncrementalSearch (Text);
  View.StopIncrementalSearch;
  // Restore the initial incremental search setting
  View.OptionsBehavior.AllowIncrementalSearch := IncSearchEnabled;

  // Check if the search was successful
  if View.GetRowCellDisplayText (View.FocusedRowHandle, Column).OleValue = Text then
    Result := View.FocusedRowHandle // Row was found and selected
  else
    Result := -1; // Row was not found
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 and select the cell
  RowIndex = FindRowByIncSearch (Grid, null, "Product", "Ipoh Coffee");
  if (RowIndex != -1)
    Log["Message"]("Row index: " + RowIndex)
  else
    Log["Error"]("The row was not found.");
}

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

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

  // Enable incremental search
  IncSearchEnabled = View["OptionsBehavior"]["AllowIncrementalSearch"];
  View["OptionsBehavior"]["AllowIncrementalSearch"] = true;
      
  // Select the topmost cell in the specified column
  View["FocusedRowHandle"] = 0;
  View["FocusedColumn"] = Column;

  // Use incremental search
  View["StartIncrementalSearch"](Text);
  View["StopIncrementalSearch"]();
  // Restore the initial incremental search setting
  View["OptionsBehavior"]["AllowIncrementalSearch"] = IncSearchEnabled;

  // Check if the search was successful
  if (View["GetRowCellDisplayText"](View["FocusedRowHandle"], Column)["OleValue"] == Text)
    return View["FocusedRowHandle"] // Row was found and selected
  else
    return -1; // Row was not found
}

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

Searching by Iterating Through Grid Rows

Sometimes the approaches described in the previous sections may not suit your needs. For example, you may want to search for a record by values or text in multiple columns, search for a particular group row, and so on. In this case, you will need to implement the search procedure yourself. The general approach implies that you iterate through grid rows (cards) and on each iteration check whether the current row matches the search condition.

Example

The example works with the GridTutorials application.

How to get the application

View example description

JavaScript, 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 row by cell value
  RowIndex = FindRow (Grid, "Unit Price", CreateDecimal(34.8));
  if (RowIndex != -1)
    Grid.ClickCell (RowIndex, "Unit Price")
  else
    Log.Error ("Row was not found.");
}

function FindRow (ViewObj, ColumnId, Value)
{
  // Iterate through rows
  for (var i=0; i<ViewObj.wRowCount; i++)
    // Check the cell value in the specified column
    if (ViewObj.wValue(i, ColumnId).Equals(Value))
      return i; // Row is found

  return -1; // Row is not found
}

function CreateDecimal (Value)
{
  return Sys.Process("GridTutorials").AppDomain("GridTutorials.exe").dotNET.System.Decimal.zctor(Value);
}

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 row by cell value
  RowIndex = FindRow (Grid, "Unit Price", CreateDecimal(34.8))
  if (RowIndex != -1):
    Grid.ClickCell (RowIndex, "Unit Price")
  else:
    Log.Error ("Row was not found.")

def FindRow (ViewObj, ColumnId, Value):
  # Iterate through rows
  for i in range(0, ViewObj.wRowCount-1):
    # Check the cell value in the specified column
    if (ViewObj.wValue[i, ColumnId].Equals(Value)):
      return i # Row is found

  return -1 # Row is not found

def CreateDecimal (Value):
  return Sys.Process("GridTutorials").AppDomain("GridTutorials.exe").dotNET.System.Decimal.zctor(Value)

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 row by cell value
  RowIndex = FindRow (Grid, "Unit Price", CreateDecimal(34.8))
  If RowIndex <> -1 Then 
    Call Grid.ClickCell (RowIndex, "Unit Price")
  Else
    Log.Error ("Row was not found.")
  End If
End Sub

Function FindRow (ViewObj, ColumnId, Value)
  Dim i
  ' Iterate through rows
  For i = 0 To ViewObj.wRowCount-1
    ' Check the cell value in the specified column
    If ViewObj.wValue(i, ColumnId).Equals(Value) Then
      FindRow = i ' Row is found
      Exit Function
    End If
  Next

  FindRow = -1 ' Row is not found
End Function

Function CreateDecimal (Value)
  Set CreateDecimal = Sys.Process("GridTutorials").AppDomain("GridTutorials.exe").dotNET.System.Decimal.zctor(Value)
End Function

DelphiScript

function FindRow (ViewObj, ColumnId, Value);
var i : OleVariant;
begin
  // Iterate through rows
  for i := 0 to ViewObj.wRowCount-1 do
    // Check the cell value in the specified column
    if ViewObj.wValue[i, ColumnId].Equals(Value) then
    begin
      Result := i; // Row is found
      Exit
    end;

  Result := -1; // Row is not found
end;

function CreateDecimal (Value);
begin
  Result := Sys.Process('GridTutorials').AppDomain('GridTutorials.exe').dotNET.System.Decimal.zctor(Value);
end;

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 row by cell value
  RowIndex := FindRow (Grid, 'Unit Price', CreateDecimal(34.8));
  if RowIndex <> -1 then
    Grid.ClickCell (RowIndex, 'Unit Price')
  else
    Log.Error ('Row was not found.');
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 row by cell value
  RowIndex = FindRow (Grid, "Unit Price", CreateDecimal(34.8));
  if (RowIndex != -1)
    Grid["ClickCell"](RowIndex, "Unit Price")
  else
    Log["Error"]("Row was not found.");
}

function FindRow (ViewObj, ColumnId, Value)
{
  // Iterate through rows
  for (var i=0; i<ViewObj["wRowCount"]; i++)
    // Check the cell value in the specified column
    if (ViewObj["wValue"](i, ColumnId)["Equals"](Value))
      return i; // Row is found

  return -1; // Row is not found
}

function CreateDecimal (Value)
{
  return Sys["Process"]("GridTutorials")["AppDomain"]("GridTutorials.exe")["dotNET"]["System"]["Decimal"]["zctor"](Value);
}

Example 2

The following example is a bit more complex. It demonstrates how to find an XtraGrid row (card) by values held in multiple cells.

The example works with the GridTutorials application.

How to get the application

View example description

JavaScript, JScript

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

  // 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 a row by several cell values
  Columns = new Array ("Order ID", "Unit Price");
  Values = new Array (10673, CreateDecimal(46));
  RowIndex = FindRowByMultipleValues (Grid, Columns, Values);
  if (RowIndex != -1)
    Grid.ClickCell (RowIndex, "Unit Price")
  else
    Log.Error ("Row was not found.");
}

function FindRowByMultipleValues (ViewObj, Columns, Values)
{
  var match, i, j;

  // Iterate through rows
  for (i=0; i<ViewObj.wRowCount; i++)
  {
    match = true;

    // Check cell values in the specified columns
    for (j=0; j<Columns.length; j++)
      if (! ViewObj.wValue(i, Columns[j]).Equals(Values[j]))
      {
        match = false; // Cell value differs from the specified one
        break;
      }

    if (match)
      return i; // Row is found
  }

  return -1; // Row is not found
}

function CreateDecimal (Value)
{
  return Sys.Process("GridTutorials").AppDomain("GridTutorials.exe").dotNET.System.Decimal.zctor(Value);
}

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 a row by several cell values
  Columns = list("Order ID", "Unit Price")
  Values = list(10673, CreateDecimal(46))
  RowIndex = FindRowByMultipleValues (Grid, Columns, Values)
  if (RowIndex != -1):
    Grid.ClickCell (RowIndex, "Unit Price")
  else:
    Log.Error ("Row was not found.")

def FindRowByMultipleValues (ViewObj, Columns, Values):

  # Iterate through rows
  for i in range (0, ViewObj.wRowCount-1):
    match = True

    # Check cell values in the specified columns
    for j in range(0, Columns.length-1):
      if not ViewObj.wValue[i, Columns[j]].Equals(Values[j]):
        match = False # Cell value differs from the specified one
        break

    if (match):
      return i # Row is found

  return -1 # Row is not found

def CreateDecimal (Value):
  return Sys.Process("GridTutorials").AppDomain("GridTutorials.exe").dotNET.System.Decimal.zctor(Value)

VBScript

Sub Main
  Dim p, frmMain, Grid, RowIndex, Columns, Values

  ' 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 a row by multiple cell values
  Columns = Array ("Order ID", "Unit Price")
  Values = Array (CLng(10673), CreateDecimal(46))
  RowIndex = FindRowByMultipleValues (Grid, Columns, Values)
  If RowIndex <> -1 Then
    Call Grid.ClickCell (RowIndex, "Unit Price")
  Else
    Log.Error ("Row was not found.")
  End If
End Sub

Function FindRowByMultipleValues (ViewObj, Columns, Values)
  Dim match, i, j

  ' Iterate through rows
  For i = 0 To ViewObj.wRowCount-1
    match = True

    ' Check cell values in the specified columns
    For j = 0 To UBound(Columns)
      If Not ( ViewObj.wValue(i, Columns(j)).Equals(Values(j)) ) Then
        match = False ' Cell value differs from the specified one
        Exit For
      End If
    Next

    If match Then
      FindRowByMultipleValues = i ' Row is found
      Exit Function
    End If
  Next

  FindRowByMultipleValues = -1 ' Row is not found
End Function

Function CreateDecimal (Value)
  Set CreateDecimal = Sys.Process("GridTutorials").AppDomain("GridTutorials.exe").dotNET.System.Decimal.zctor(Value)
End Function

DelphiScript

function FindRowByMultipleValues (ViewObj, Columns, Values);
var match, i, j : OleVariant;
begin
  // Iterate through rows
  for i := 0 to ViewObj.wRowCount-1 do
  begin
    match := true;

    // Check cell values in the specified columns
    for j := 0 to VarArrayHighBound(Columns, 1) do
      if not ( ViewObj.wValue[i, Columns[j]].Equals(Values[j]) ) then
      begin
        match := false; // Cell value differs from the specified one
        Break;
      end;

    if match then
    begin
      Result := i; // Row is found
      Exit
    end
  end;

  Result := -1; // Row is not found
end;

function CreateDecimal (Value);
begin
  Result := Sys.Process('GridTutorials').AppDomain('GridTutorials.exe').dotNET.System.Decimal.zctor(Value);
end;

function Main;
var p, frmMain, Grid, RowIndex, Columns, Values : 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');

  Columns := CreateVariantArray (0, 1);
  Columns[0] := 'Order ID'; Columns[1] := 'Unit Price';
  Values := CreateVariantArray (0, 1);
  Values[0] := 10673; Values[1] := CreateDecimal(46);
  // Locate a row by several cell values
  RowIndex := FindRowByMultipleValues (Grid, Columns, Values);
  if RowIndex <> -1 then
    Grid.ClickCell (RowIndex, 'Unit Price')
  else
    Log.Error ('Row was not found.');
end;

C++Script, C#Script

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

  // 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 a row by several cell values
  Columns = new Array ("Order ID", "Unit Price");
  Values = new Array (10673, CreateDecimal(46));
  RowIndex = FindRowByMultipleValues (Grid, Columns, Values);
  if (RowIndex != -1)
    Grid["ClickCell"](RowIndex, "Unit Price")
  else
    Log["Error"]("Row was not found.");
}

function FindRowByMultipleValues (ViewObj, Columns, Values)
{
  var match, i, j;

  // Iterate through rows
  for (i=0; i<ViewObj["wRowCount"]; i++)
  {
    match = true;

    // Check cell values in the specified columns
    for (j=0; j<Columns["length"]; j++)
      if (! ViewObj["wValue"](i, Columns[j])["Equals"](Values[j]))
      {
        match = false; // Cell value differs from the specified one
        break;
      }

    if (match)
      return i; // Row is found
  }

  return -1; // Row is not found
}

function CreateDecimal (Value)
{
  return Sys["Process"]("GridTutorials")["AppDomain"]("GridTutorials.exe")["dotNET"]["System"]["Decimal"]["zctor"](Value);
}

See Also

Working With Developer Express XtraGrid
Accessing Views in Developer Express XtraGrid
Obtaining and Setting Cell Values in Developer Express XtraGrid
Iterating Through Rows and Cards in Developer Express XtraGrid

Highlight search results