Expanding and Collapsing Rows in Infragistics UltraGrid

Applies to TestComplete 15.47, last modified on January 20, 2023

Grids that display hierarchical data or permit data grouping are able to expand data view rows. When a row is expanded it becomes a parent to child rows from a lower data view that display information regarding the parent record. The child row, in its turn, could become a parent to the rows of a lower hierarchy level and so forth. To reduce the detailed information from the other data views the parent row can be collapsed.

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.

Note: The UltraGrid control can contain expandable rows of two types:
  • Group rows. They appear when the grid contents are grouped by one or several columns.
  • Ordinary rows that have child data. The UltraGrid control can display data from several hierarchical levels. If a child level contains rows associated with the parent row, then a user can expand this parent row to view the child data.

You can verify the type of expandable row with the internal UltraGridRowObj.IsGroupByRow property that returns True when the current row is a group row. Nevertheless, you can expand or collapse the rows the same way, regardless of their type.

Using the Expand and Collapse Actions of InfragisticsUltraGridView and InfragisticsUltraGrid Objects

To expand or collapse rows of the UltraGrid control, you can use the Expand and Collapse actions of the InfragisticsUltraGrid or InfragisticsUltraGridView objects. The actions of the InfragisticsUltraGrid object apply to the rows of the main data view, whereas the InfragisticsUltraGridView actions affect the rows of the respective child data view. The wExpanded property of these objects indicates the current expanded state of a particular row.

The following example illustrates how you can expand and collapse rows using actions provided by TestComplete:

Note: This example works with the SamplesExplorer application that is shipped with the Infragistics NetAdvantage Suite.

JavaScript, JScript

function ExtendedExpandCollapse()
{
  var p1
  var w1
  var Grid
  
  // Obtain the application process and its main form
  p1 = Sys.Process("SamplesExplorer");
  w1 = p1.frmMain.WinFormsObject("tvwSamples");
  // Select the "Merged Cells Feature" demo
  w1.DblClickItem("|Feature Samples|V5.1 Merged Cells Feature");
  
  // Obtain the grid control
  Grid = p1.frmMergedCells.WinFormsObject("UltraGrid1");
  
  // Expand the first row in the main view
  Grid.Expand(0);
  // Expand the second row of a first row's data view
  Grid.wChildView(0).Expand(1);
  aqUtils.Delay(1000);
  // Collapse the second row of a first row's data view
  Grid.wChildView(0).Collapse(1);
  // Collapse the first row in the main view
  Grid.Expand(0, false);
}

Python

def ExtendedExpandCollapse():
  
  # Obtain the application process and its main form
  p1 = Sys.Process("SamplesExplorer")
  w1 = p1.frmMain.WinFormsObject("tvwSamples")
  # Select the "Merged Cells Feature" demo
  w1.DblClickItem("|Feature Samples|V5.1 Merged Cells Feature")
  
  # Obtain the grid control
  Grid = p1.frmMergedCells.WinFormsObject("UltraGrid1")
  
  # Expand the first row in the main view
  Grid.Expand(0) 
  # Expand the second row of a first row's data view 
  Grid.wChildView[0].Expand(1)
  aqUtils.Delay(1000)
  # Collapse the second row of a first row's data view 
  Grid.wChildView[0].Collapse(1)
  # Collapse the first row in the main view 
  Grid.Expand(0, False)

VBScript

Sub ExtendedExpandCollapse
  Dim p1
  Dim w1
  Dim Grid
  
  ' Obtain the application process and its main form
  Set p1 = Sys.Process("SamplesExplorer")
  Set w1 = p1.frmMain.WinFormsObject("tvwSamples")
  ' Select the "Merged Cells Feature" demo
  Call w1.DblClickItem("|Feature Samples|V5.1 Merged Cells Feature")
  
  ' Obtain the grid control
  Set Grid = p1.frmMergedCells.WinFormsObject("UltraGrid1")
  
  ' Expand the first row in the main view
  Call Grid.Expand(0)
  ' Expand the second row of a first row's data view
  Call Grid.wChildView(0).Expand(1)
  aqUtils.Delay(1000)
  ' Collapse the second row of a first row's data view
  Call Grid.wChildView(0).Collapse(1)
  ' Collapse the first row in the main view
  Call Grid.Expand(0, False)
End Sub

DelphiScript

procedure ExtendedExpandCollapse;
  var p1: OleVariant;
  var w1: OleVariant;
  var Grid: OleVariant;
begin
  // Obtain the application process and its main form
  p1 := Sys.Process('SamplesExplorer');
  w1 := p1.frmMain.WinFormsObject('tvwSamples');
  // Select the "Merged Cells Feature" demo
  w1.DblClickItem('|Feature Samples|V5.1 Merged Cells Feature');
  
  // Obtain the grid control
  Grid := p1.frmMergedCells.WinFormsObject('UltraGrid1');
  
  // Expand the first row in the main view
  Grid.Expand(0);
  // Expand the second row of a first row's data view
  Grid.wChildView[0].Expand(1);
  aqUtils.Delay(1000);
  // Collapse the second row of a first row's data view
  Grid.wChildView[0].Collapse(1);
  // Collapse the first row in the main view
  Grid.Expand(0, False);
end;

C++Script, C#Script

function ExtendedExpandCollapse()
{
  var p1
  var w1
  var Grid
  
  // Obtain the application process and its main form
  p1 = Sys["Process"]("SamplesExplorer");
  w1 = p1["frmMain"]["WinFormsObject"]("tvwSamples");
  // Select the "Merged Cells Feature" demo
  w1["DblClickItem"]("|Feature Samples|V5.1 Merged Cells Feature");
  
  // Obtain the grid control
  Grid = p1["frmMergedCells"]["WinFormsObject"]("UltraGrid1");
  
  // Expand the first row in the main view
  Grid["Expand"](0);
  // Expand the second row of a first row's data view
  Grid["wChildView"](0)["Expand"](1);
  aqUtils["Delay"](1000);
  // Collapse the second row of a first row's data view
  Grid["wChildView"](0)["Collapse"](1);
  // Collapse the first row in the main view
  Grid["Expand"](0, false);
}

Using Internal Methods of the UltraGrid Control

This approach utilizes the internal methods and properties that are exposed by the .NET Application Support plugin.

The following table lists methods and properties that can be used to expand and collapse data rows and groups in the UltraGrid control. For more information on these methods, please refer to the Infragistics NetAdvantage documentation.

Method, Property Description
UltraGridRowObj.AllAncestorsExpanded Read-only property. Indicates if the row’s parent row and all its ancestors are expanded.
UltraGridRowObj.Expanded Read-write property. Specifies whether the row is expanded or not.
UltraGridRowObj.IsExpandable Read-only property. Indicates whether the retrieved row can be expanded.
UltraGridRowObj.IsExpanded Read-only property. Returns true if the row is expanded in the display.
UltraGridRowObj.ExpandAll Method. Expands all of the child rows of the current row.
UltraGridRowObj.CollapseAll Method. Collapses all of the child rows of the current row.

You can obtain the UltraGridRow object via the GetRow routine. It returns an object that corresponds to the row with a specified index. You can find the code for it in the Accessing Grid Elements in Infragistics UltraGrid topic.

Example

View description

JavaScript

function ExpandRow(Grid, View, RowIdx)
{
  // Obtain the row object
  Row = GetRow(Grid, View, RowIdx)
  
  if (!strictEqual(Row, null))
    {
    // Check whether a row can be expanded
    if (Row.IsExpandable)
      {
      Row.Expanded = true
      return true
      }
    }
   return false 
}

function CollapseRow(Grid, View, RowIdx)
{
  // Obtain the row object
  Row = GetRow(Grid, View, RowIdx)
  
  if (!strictEqual(Row, null))
    {
    // Check whether a row can be collapsed
    if (Row.IsExpandable)
      {
      Row.Expanded = false
      return true
      }
    }
   return false 
}

function TestExpandCollapse()
{
var Grid;
  RowIdx=0

  // Obtain the grid object
  Grid = Sys.Process("SamplesExplorer").WinFormsObject("frmEmptyRows").WinFormsObject("UltraGrid1");
  
  if (ExpandRow(Grid, null, RowIdx))
         Log.Message("The row "+RowIdx+" was expanded.")
    else Log.Message("The row "+RowIdx+" cannot be expanded.")
  
  if (CollapseRow(Grid, null, RowIdx))
         Log.Message("The row "+RowIdx+" was collapsed.")
    else Log.Message("The row "+RowIdx+" cannot be collapsed.")
}

JScript

function ExpandRow(Grid, View, RowIdx)
{
  // Obtain the row object
  Row = GetRow(Grid, View, RowIdx)
  
  if (Row != null)
    {
    // Check whether a row can be expanded
    if (Row.IsExpandable)
      {
      Row.Expanded = true
      return true
      }
    }
   return false 
}

function CollapseRow(Grid, View, RowIdx)
{
  // Obtain the row object
  Row = GetRow(Grid, View, RowIdx)
  
  if (Row != null)
    {
    // Check whether a row can be collapsed
    if (Row.IsExpandable)
      {
      Row.Expanded = false
      return true
      }
    }
   return false 
}

function TestExpandCollapse()
{
var Grid;
  RowIdx=0

  // Obtain the grid object
  Grid = Sys.Process("SamplesExplorer").WinFormsObject("frmEmptyRows").WinFormsObject("UltraGrid1");
  
  if (ExpandRow(Grid, null, RowIdx))
         Log.Message("The row "+RowIdx+" was expanded.")
    else Log.Message("The row "+RowIdx+" cannot be expanded.")
  
  if (CollapseRow(Grid, null, RowIdx))
         Log.Message("The row "+RowIdx+" was collapsed.")
    else Log.Message("The row "+RowIdx+" cannot be collapsed.")
}

Python

def ExpandRow(Grid, View, RowIdx):
  # Obtain the row object
  Row = GetRow(Grid, View, RowIdx)
  
  if (Row != None):
    # Check whether a row can be expanded
    if (Row.IsExpandable): 
      Row.Expanded = True
      return True
   return False 

def CollapseRow(Grid, View, RowIdx):
  # Obtain the row object
  Row = GetRow(Grid, View, RowIdx)
  
  if (Row != None):
    # Check whether a row can be collapsed
    if (Row.IsExpandable): 
      Row.Expanded = False
      return True
   return False 

def TestExpandCollapse():
  RowIdx=0

  # Obtain the grid object
  Grid = Sys.Process("SamplesExplorer").WinFormsObject("frmEmptyRows").WinFormsObject("UltraGrid1")
  
  if (ExpandRow(Grid, None, RowIdx)) :
    Log.Message("The row "+RowIdx+" was expanded.")
  else:
    Log.Message("The row "+RowIdx+" cannot be expanded.") 
  
  if (CollapseRow(Grid, None, RowIdx)):
    Log.Message("The row "+RowIdx+" was collapsed.")
  else :
    Log.Message("The row "+RowIdx+" cannot be collapsed.")

VBScript

Function ExpandRow(Grid, View, RowIdx)
  ' Set initial value for function
  ExpandRow = False
  ' Obtain the row object
  Set Row = GetRow(Grid, View, RowIdx)
  If Not Row Is Nothing Then
    ' Check whether a row can be expanded
    If Row.IsExpandable Then
      Row.Expanded = True
      ExpandRow = True
    End If
  End If
End Function

Function CollapseRow(Grid, View, RowIdx)
  ' Set initial value for function
  CollapseRow = False
  ' Obtain the row object
  Set Row = GetRow(Grid, View, RowIdx)
  If Not Row Is Nothing Then
    ' Check whether a row can be collapsed
    If Row.IsExpandable Then
      Row.Expanded = False
      CollapseRow = True
    End If
  End If
End Function

Sub TestExpandCollapse
Dim Grid
  RowIdx=0

  ' Obtain the grid object
  Set Grid = Sys.Process("SamplesExplorer").WinFormsObject("frmEmptyRows").WinFormsObject("UltraGrid1")
  
  If ExpandRow(Grid, Nothing, RowIdx) Then 
         Log.Message("The row "+CStr(RowIdx)+" was expanded.")
    Else Log.Message("The row "+CStr(RowIdx)+" cannot be expanded.")
  End If
  
  If CollapseRow(Grid, Nothing, RowIdx) Then 
         Log.Message("The row "+CStr(RowIdx)+" was collapsed.")
    Else Log.Message("The row "+CStr(RowIdx)+" cannot be collapsed.")
  End If
End Sub

DelphiScript

function ExpandRow(Grid, View, RowIdx);
var
  Row: Variant;
begin
  // Set initial value for function
  Result := False;
  // Obtain the row object
  Row := GetRow(Grid, View, RowIdx);
  if Row<>nil then
    begin
      // Check whether a row can be expanded
      if Row.IsExpandable then
        begin
          Row.Expanded := True;
          Result:=True;
        end;
    end;
end;

function CollapseRow(Grid, View, RowIdx);
var
  Row: Variant;
begin
  // Set initial value for function
  Result := False;
  // Obtain the row object
  Row := GetRow(Grid, View, RowIdx);
  if Row<>nil then
    begin
      // Check whether a row can be collapsed
      if Row.IsExpandable then
        begin
          Row.Expanded := False;
          Result:=True;
        end;
    end;
end;


procedure TestExpandCollapse;
var Grid: Variant;
    RowIdx: integer;
begin
  RowIdx:=0;

  // Obtain the grid object
  Grid := Sys.Process('SamplesExplorer').WinFormsObject('frmEmptyRows').WinFormsObject('UltraGrid1');
  
  if ExpandRow(Grid, nil, RowIdx) then 
         Log.Message('The row '+aqConvert.IntToStr(RowIdx)+' was expanded.')
    else Log.Message('The row '+aqConvert.IntToStr(RowIdx)+' cannot be expanded.');
  
  if CollapseRow(Grid, nil, RowIdx) then 
         Log.Message('The row '+aqConvert.IntToStr(RowIdx)+' was collapsed.')
    else Log.Message('The row '+aqConvert.IntToStr(RowIdx)+' cannot be collapsed.');
end;

C++Script, C#Script

function ExpandRow(Grid, View, RowIdx)
{
  // Obtain the row object
  Row = GetRow(Grid, View, RowIdx)
  
  if (Row != null)
    {
    // Check whether a row can be expanded
    if (Row["IsExpandable"])
      {
      Row["Expanded"] = true
      return true
      }
    }
   return false 
}

function CollapseRow(Grid, View, RowIdx)
{
  // Obtain the row object
  Row = GetRow(Grid, View, RowIdx)
  
  if (Row != null)
    {
    // Check whether a row can be collapsed
    if (Row["IsExpandable"])
      {
      Row["Expanded"] = false
      return true
      }
    }
   return false 
}

function TestExpandCollapse()
{
var Grid;
  RowIdx=0

  // Obtain the grid object
  Grid = Sys["Process"]("SamplesExplorer")["WinFormsObject"]("frmEmptyRows")["WinFormsObject"]("UltraGrid1");
  
  if (ExpandRow(Grid, null, RowIdx))
         Log["Message"]("The row "+RowIdx+" was expanded.")
    else Log["Message"]("The row "+RowIdx+" cannot be expanded.")
  
  if (CollapseRow(Grid, null, RowIdx))
         Log["Message"]("The row "+RowIdx+" was collapsed.")
    else Log["Message"]("The row "+RowIdx+" cannot be collapsed.")
}

See Also

Working With Infragistics UltraGrid
Accessing Grid Elements in Infragistics UltraGrid
Selecting Multiple Rows in Infragistics UltraGrid
Obtaining Selected Elements in Infragistics UltraGrid
Expand Action (Grid Controls)
Collapse Action (Grid Controls)
wExpanded Property (Grid Controls)

Highlight search results