Searching for Records in Infragistics UltraGrid

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

One of the actions you will most likely perform with Infragistics UltraGrid is searching for a specific record within the grid. For example, you may need to find a certain record to make sure that data has been loaded into the grid correctly, or to determine the position of the desired record within the grid.

This topic describes the two available approaches for searching within the UltraGrid control:

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

When testing Infragistics UltraGrid controls, use specific methods and properties of the corresponding Infragistics UltraGrid object. You can call these methods and properties from your keyword tests, as well as from scripts. This topic describes how to work with an object’s properties and methods from your scripts. However, when testing a UltraGrid 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 Within the Grid’s Data Source

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 data source is a universal approach that is free from the grid’s user interface limitations. You can use this approach if you need to obtain and process values in the grid cells rather than their displayed representation.

The Infragistics UltraGrid control supports several kinds of data sources:

  • DataSets
  • DataTables
  • Any objects that implement IList interface, including ArrayList and Array and some other sources.

To obtain the UltraGrid’s dataset, use the DataSource property of the grid control. Since UltraGrid can be bound to various source types, the object returned by the DataSource property may have a different structure in different applications. You can explore the dataset object by finding the control’s DataSource property in the Object Browser panel and clicking the ellipsis button on the right of the property cell. You will see the properties and methods of the object returned by this property. Digging further, you will be able to determine the properties and methods that can be used to search within the dataset.

Let’s illustrate this approach with a couple of examples that use two different types of data sources.

The code below demonstrates how to use the methods of the data table view that is obtained via the DataSet object. It utilizes the “Fixed Rows” example from the “Samples Explorer” demo application that is provided with Infragistics UltraGrid. It locates a row in the underlying data table “Customers” that contains “Brooklyn” in the “City” column.

Example

View description

JavaScript, JScript

function Main ()
{
  var p, Grid, Table, RowId, ColumnName, Value;

  // Obtain the grid object and its dataset
  p = Sys.Process("SamplesExplorer");
  Grid = p.WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1");
  Table = Grid.DataSource.Tables.Item(0);

  // Locate row by cell value
  ColumnName = "City";
  Value = "Brooklyn";
  RowId = FindRow (Table, ColumnName, Value);
  if (RowId >= 0)
    Log.Message ("Index of the found data table row: " + RowId);
  else
    Log.Error ("The '" + ColumnName + "' column of the data table does not contain the value of '" + Value + "'.");
}

function FindRow (Table, ColumnName, Value)
{
  var View = Table.DefaultView;
  View.Sort = ColumnName;
  return View.Find (Value);
}

Python

def Main ():

  # Obtain the grid object and its dataset
  p = Sys.Process("SamplesExplorer")
  Grid = p.WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1")
  Table = Grid.DataSource.Tables.Item(0)

  # Locate row by cell value
  ColumnName = "City"
  Value = "Brooklyn"
  RowId = FindRow (Table, ColumnName, Value)
  if (RowId >= 0):
    Log.Message ("Index of the found data table row: " + RowId)
  else:
    Log.Error ("The '" + ColumnName + "' column of the data table does not contain the value of '" + Value + "'.")

def FindRow (Table, ColumnName, Value):
  View = Table.DefaultView
  View.Sort = ColumnName
  return View.Find (Value)

VBScript

Sub Main
  Dim p, Grid, Table, RowId, ColumnName, Value

  ' Obtain the grid object and its dataset
  Set p = Sys.Process("SamplesExplorer")
  Set Grid = p.WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1")
  Set Table = Grid.DataSource.Tables.Item(0)

  ' Locate row by cell value
  ColumnName = "City"
  Value = "Brooklyn"
  RowId = FindRow (Table, ColumnName, Value)
  If RowId >= 0 Then
    Call Log.Message ("Index of the found data table row: " & CStr(RowId))
  Else
    Call Log.Error ("The '" & ColumnName & "' column of the data table does not contain the value of '" & CStr(Value) & "'.")
  End If
End Sub

Function FindRow (Table, ColumnName, Value)
  Dim View
  Set View = Table.DefaultView
  View.Sort = ColumnName
  FindRow = View.Find (Value)
End Function

DelphiScript

function FindRow (Table, ColumnName, Value);
var View : OleVariant;
begin
  View := Table.DefaultView;
  View.Sort := ColumnName;
  Result := View.Find (Value);
end;

procedure Main;
var Grid, Table, RowId, ColumnName, Value : OleVariant;
begin
  // Obtain the grid object and its dataset
  p := Sys.Process('SamplesExplorer');
  Grid := p.WinFormsObject('frmFixedHeaders').WinFormsObject('UltraGrid1');
  Table := Grid.DataSource.Tables.Item[0];

  // Locate row by cell value
  ColumnName := 'City';
  Value := 'Brooklyn';
  RowId := FindRow (Table, ColumnName, Value);
  if RowId >= 0 then
    Log.Message ('Index of the found data table row: ' + aqConvert.VarToStr(RowId))
  else
    Log.Error ('The "' + ColumnName + '" column of the data table does not contain the value of "' + aqConvert.VarToStr(Value) + '".');
end;

C++Script, C#Script

function Main ()
{
  var p, Grid, Table, RowId, ColumnName, Value;

  // Obtain the grid object and its dataset
  p = Sys["Process"]("SamplesExplorer");
  Grid = p["WinFormsObject"]("frmFixedHeaders")["WinFormsObject"]("UltraGrid1");
  Table = Grid["DataSource"]["Tables"]["Item"](0);

  // Locate row by cell value
  ColumnName = "City";
  Value = "Brooklyn";
  RowId = FindRow (Table, ColumnName, Value);
  if (RowId >= 0)
    Log["Message"]("Index of the found data table row: " + RowId);
  else
    Log["Error"]("The '" + ColumnName + "' column of the data table does not contain the value of '" + Value + "'.");
}

function FindRow (Table, ColumnName, Value)
{
  var View = Table["DefaultView"];
  View["Sort"] = ColumnName;
  return View["Find"](Value);
}

The following code demonstrates how to use the methods of the BindingSource data source that is inherited from the IList interface. The example uses a sample application that displays the information from an Access database via the UltraGrid control.

Example

View description

JavaScript, JScript

function Main ()
{
  var p, Grid, MatchRowIndex, ColumnName, Value;
  
  // Obtain the grid object
  p = Sys.Process("UltraWinGridder");
  Grid = p.WinFormsObject("Form1").WinFormsObject("ultraGrid1");

  // Call the overloaded version of BindingSource.Find method
  ColumnName = "Title";
  Value = "Sherlock Holmes";
  MatchRowIndex = Grid.DataSource.Find_2(ColumnName, Value);
  if (MatchRowIndex >= 0)
    Log.Message ("Index of the found data table row: " + MatchRowIndex);
  else
    // No matches were found
    Log.Error ("The '" + ColumnName + "' column of the data table does not contain the value of '" + Value + "'.");
}

Python

def Main ():
  
  # Obtain the grid object
  p = Sys.Process("UltraWinGridder") 
  Grid = p.WinFormsObject("Form1").WinFormsObject("ultraGrid1")

  # Call the overloaded version of BindingSource.Find method
  ColumnName = "Title"
  Value = "Sherlock Holmes"
  MatchRowIndex = Grid.DataSource.Find_2(ColumnName, Value)
  if (MatchRowIndex >= 0): 
    Log.Message ("Index of the found data table row: " + MatchRowIndex)
  else:
    # No matches were found
    Log.Error ("The '" + ColumnName + "' column of the data table does not contain the value of '" + Value + "'.")

VBScript

Sub Main
  Dim p, Grid, MatchRowIndex, ColumnName, Value

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

  ' Call the overloaded version of BindingSource.Find method
  ColumnName = "Title"
  Value = "Sherlock Holmes"
  MatchRowIndex = Grid.DataSource.Find_2 (ColumnName, Value)
  If MatchRowIndex >= 0 Then
    Call Log.Message ("Index of the found data table row: " & CStr(MatchRowIndex))
  Else
    ' No matches were found
    Call Log.Error ("The '" & ColumnName & "' column of the data table does not contain the value of '" & Value & "'.")
  End If
End Sub

DelphiScript

procedure Main;
var p, Grid, MatchRowIndex, ColumnName, Value: OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('UltraWinGridder');
  Grid := p.WinFormsObject('Form1').WinFormsObject('ultraGrid1');

  // Call the overloaded version of BindingSource.Find method
  ColumnName := 'Title';
  Value := 'Sherlock Holmes';
  MatchRowIndex := Grid.DataSource.Find_2(ColumnName, Value);
  if MatchRowIndex >= 0 then
    Log.Message ('Index of the found data table row: ' + aqConvert.IntToStr(MatchRowIndex))
  else
    Log.Error ('The "' + ColumnName + '" column of the data table does not contain the value of "' + Value + '".');
end;

C++Script, C#Script

function Main ()
{
  var p, Grid, MatchRowIndex, ColumnName, Value;
  
  // Obtain the grid object
  p = Sys["Process"]("UltraWinGridder");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("ultraGrid1");

  // Call the overloaded version of BindingSource.Find method
  ColumnName = "Title";
  Value = "Sherlock Holmes";
  MatchRowIndex = Grid["DataSource"]["Find_2"](ColumnName, Value);
  if (MatchRowIndex >= 0)
    Log["Message"]("Index of the found data table row: " + MatchRowIndex);
  else
    // No matches were found
    Log["Error"]("The '" + ColumnName + "' column of the data table does not contain the value of '" + Value + "'.");
}

Searching Within the Grid’s View

The UltraGrid control lets you apply custom formatting to values displayed in the grid. If you need to check whether the record values are displayed with the appropriate formatting applied, or find a record by its formatted display string, you can search for the desired record directly within the grid. Information from a various datasets is displayed by a grid in a similar way, although several data representations can be applied. However it has a considerable drawback: as the grid represents formatted data and often uses inplace editors, the actual value can be significantly different from the value displayed by a grid.

The UltraGrid control does not have built-in methods for searching within the grid, so you will need to implement the search algorithm yourself. A general search approach implies that you iterate through the grid data views rows and on each iteration check whether the row meets the specified condition.

Example

View description

JavaScript

function Main ()
{
  var p, Grid, RowId, LookedUpColumn, Value;
  
  // Obtain the grid object
  p = Sys.Process("SamplesExplorer");
  Grid = p.WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1");

  // Locate the row
  LookedUpColumn = "City";
  Value = "Brooklyn";
  RowId = FindRowByCellValue (Grid, null, LookedUpColumn, Value);

  
  if (RowId >= 0)
    Log.Message ("Index of the found grid row: " + RowId);
  else
    Log.Error ("The '" + LookedUpColumn + "' column does not contain the '" + aqConvert.VarToStr(Value) + "' value.");
}

function FindRowByCellValue (Grid, ChildView, ColName, SoughtValue)
{
  var Val;

  if (strictEqual(ChildView, null))
    ChildView = Grid;

  Val = aqConvert.VarToStr (SoughtValue);

  // Iterate through grid rows
  for (let i = 0; i < ChildView.wRowCount; i++)
    // Compare the cell value with the specified value
    if (equal(aqConvert.VarToStr(ChildView.wValue(i,ColName)), Val))
      return i; // Row is found

   return -1; // Row is not found
}

JScript

function Main ()
{
  var p, Grid, RowId, LookedUpColumn, Value;
  
  // Obtain the grid object
  p = Sys.Process("SamplesExplorer");
  Grid = p.WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1");

  // Locate the row
  LookedUpColumn = "City";
  Value = "Brooklyn";
  RowId = FindRowByCellValue (Grid, null, LookedUpColumn, Value);

  
  if (RowId >= 0)
    Log.Message ("Index of the found grid row: " + RowId);
  else
    Log.Error ("The '" + LookedUpColumn + "' column does not contain the '" + aqConvert.VarToStr(Value) + "' value.");
}

function FindRowByCellValue (Grid, ChildView, ColName, SoughtValue)
{
  var Val, i;

  if (ChildView == null)
    ChildView = Grid;

  Val = aqConvert.VarToStr (SoughtValue);

  // Iterate through grid rows
  for (i = 0; i < ChildView.wRowCount; i++)
    // Compare the cell value with the specified value
    if (aqConvert.VarToStr(ChildView.wValue(i,ColName)) == Val)
      return i; // Row is found

   return -1; // Row is not found
}

Python

def Main ():
  
  # Obtain the grid object
  p = Sys.Process("SamplesExplorer")
  Grid = p.WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1")

  # Locate the row
  LookedUpColumn = "City"
  Value = "Brooklyn"
  RowId = FindRowByCellValue (Grid, None, LookedUpColumn, Value)

  
  if (RowId >= 0):
    Log.Message ("Index of the found grid row: " + RowId)
  else:
    Log.Error ("The '" + LookedUpColumn + "' column does not contain the '" + aqConvert.VarToStr(Value) + "' value.")

def FindRowByCellValue (Grid, ChildView, ColName, SoughtValue):

  if (ChildView == None):
    ChildView = Grid

  Val = aqConvert.VarToStr (SoughtValue)

  # Iterate through grid rows 
  for i in range(0, ChildView.wRowCount-1):
    # Compare the cell value with the specified value
    if (aqConvert.VarToStr(ChildView.wValue[i,ColName]) == Val):
      return i # Row is found

   return -1 # Row is not found

VBScript

Sub Main
  Dim p, Grid, LookedUpColumn, Value, RowId

  ' Obtain the grid object
  Set p = Sys.Process("SamplesExplorer")
  Set Grid = p.WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1")

  ' Locate the rows
  LookedUpColumn = "City"
  Value = "Brooklyn"
  RowId = FindRowByCellValue (Grid, Nothing, LookedUpColumn, Value)
  If RowId >= 0 Then
    Log.Message ("Index of the found grid row: " & CStr(RowId))
  Else
    Log.Error ("The '" & LookedUpColumn & "' column does not contain the '" & CStr(Value) & "' value.")
  End If
End Sub

Function FindRowByCellValue (Grid, ChildView, ColName, SoughtValue)
  Dim Val, i

  If ChildView Is Nothing Then
    Set ChildView = Grid
  End If

  Val = aqConvert.VarToStr (SoughtValue)

  ' Iterate through grid rows
  For i = 0 To ChildView.wRowCount-1
    ' Compare the cell value with the specified value
    If aqConvert.VarToStr(ChildView.wValue(i,ColName)) = Val Then
      FindRowByCellValue = i ' Row is found
      Exit Function
    End If
  Next

  FindRowByCellValue = -1 ' Row is not found
End Function

DelphiScript

function FindRowByCellValue (Grid, ChildView, ColName, SoughtValue):integer; forward;

procedure Main;
var p, Grid, RowId, LookedUpColumn, Value : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('SamplesExplorer');
  Grid := p.WinFormsObject('frmFixedHeaders').WinFormsObject('UltraGrid1');

  // Locate the row
  LookedUpColumn := 'City';
  Value := 'Brooklyn';
  RowId := FindRowByCellValue (Grid, nil, LookedUpColumn, Value);
  if RowId >= 0 then
    Log.Message ('Index of the found data table row: ' + aqConvert.IntToStr(RowId))
  else
    Log.Error ('The "' + LookedUpColumn + '" column does not contain the "' + aqConvert.VarToStr(Value) + '" value.');
end;

function FindRowByCellValue (Grid, ChildView, ColName, SoughtValue):integer;
var i: integer;
    Val: OleVariant;
begin
  if ChildView = nil then
     ChildView := Grid;
  
  Val := aqConvert.VarToStr (SoughtValue);
  for i := 0 to ChildView.wRowCount-1 do
    if aqConvert.VarToStr(ChildView.wValue[i,ColName])=Val then
    begin
      Result := i; // Row is found
      Exit
    end;

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

C++Script, C#Script

function Main ()
{
  var p, Grid, RowId, LookedUpColumn, Value;
  
  // Obtain the grid object
  p = Sys["Process"]("SamplesExplorer");
  Grid = p["WinFormsObject"]("frmFixedHeaders")["WinFormsObject"]("UltraGrid1");

  // Locate the row
  LookedUpColumn = "City";
  Value = "Brooklyn";
  RowId = FindRowByCellValue (Grid, null, LookedUpColumn, Value);
  if (RowId >= 0)
    Log["Message"]("Index of the found grid row: " + RowId);
  else
    Log["Error"]("The '" + LookedUpColumn + "' column does not contain the '" + aqConvert["VarToStr"](Value) + "' value.");
}

function FindRowByCellValue (Grid, ChildView, ColName, SoughtValue)
{
  var Val, i;

  if (ChildView == null)
    ChildView = Grid;

  Val = aqConvert["VarToStr"] (SoughtValue);

  // Iterate through grid rows
  for (i = 0; i < ChildView["wRowCount"]; i++)
    // Compare the cell value with the specified value
    if (aqConvert["VarToStr"](ChildView["wValue"](i,ColName)) == Val)
      return i; // Row is found

   return -1; // Row is not found
}

See Also

Working With Infragistics UltraGrid
Accessing Grid Elements in Infragistics UltraGrid
Iterating Through Rows in Infragistics UltraGrid

Highlight search results