Expanding and Collapsing Rows in Developer Express XtraGrid

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

The XtraGrid control supports hierarchical data representation. If the grid displays data of multiple nested data tables, then each data row can have child rows containing data associated with this row. To view the child data, the user needs to expand its parent row. The grid data can also be grouped by one or several columns, so the individual rows are organized into groups. This topic explains how you can expand and collapse rows and groups in the XtraGrid control from your test scripts:

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.

Using the Actions of DevExpressXtraGrid and DevExpressXtraGridView Objects

To expand or collapse rows in the XtraGrid control, you can use the Expand and Collapse actions of the DevExpressXtraGrid and DevExpressXtraGridView objects. The DevExpressXtraGrid object is applied to the rows of the main data view, whereas the DevExpressXtraGridView object affects the rows of the child data views. The wExpanded property lets you determine the current expanded state of a particular row. Similarly, to expand and collapse groups, use the ExpandGroup and CollapseGroup actions of the DevExpressXtraGrid or DevExpressXtraGridView objects, respectively. The wGroupExpanded property, in its turn, lets you check whether a particular group is expanded or collapsed.

The following example illustrates how you can expand and collapse rows and groups in the XtraGrid controls using the mentioned actions.

The example works with the GridMainDemo application.

How to get the application

JavaScript, JScript

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

  // Obtain the application process and its main form
  p = Sys.Process("GridMainDemo");
  frmMain = p.WinFormsObject("frmMain");
  frmMain.Maximize();
  // Select the "Master-Detail" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("navBarControl1").Groups.Item_2(1).SelectedLinkIndex = 0;
  // Obtain the grid control
  Grid = frmMain.WinFormsObject("panelControl1").WinFormsObject("gcContainer").WinFormsObject("MasterDetail").WinFormsObject("gridControl1");

  // Collapse and expand rows in the main view
  Grid.Collapse (0);
  Grid.Collapse (1);
  Grid.Expand (2);

  // Expand the first row in the "Products" view
  Grid.wChildView(0,"Products").Expand (0);

  // Expand the first group in the "Order Details" view
  Grid.wChildView(0,"Products").wChildView(0,"Order Details").ExpandGroup (0);
}

Python

def Main():

  # Obtain the application process and its main form
  p = Sys.Process("GridMainDemo")
  frmMain = p.WinFormsObject("frmMain")
  frmMain.Maximize()
  # Select the "Master-Detail" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("navBarControl1").Groups.Item_2[1].SelectedLinkIndex = 0
  # Obtain the grid control
  Grid = frmMain.WinFormsObject("panelControl1").WinFormsObject("gcContainer").WinFormsObject("MasterDetail").WinFormsObject("gridControl1")

  # Collapse and expand rows in the main view
  Grid.Collapse (0)
  Grid.Collapse (1)
  Grid.Expand (2)

  # Expand the first row in the "Products" view
  Grid.wChildView(0,"Products").Expand (0)

  # Expand the first group in the "Order Details" view
  Grid.wChildView(0,"Products").wChildView[0,"Order Details"].ExpandGroup (0)

VBScript

Sub Main
  Dim p, frmMain, Grid

  ' Obtain the application process and its main form
  Set p = Sys.Process("GridMainDemo")
  Set frmMain = p.WinFormsObject("frmMain")
  frmMain.Maximize
  ' Select the "Master-Detail" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("navBarControl1").Groups.Item_2(1).SelectedLinkIndex = 0
  ' Obtain the grid control
  Set Grid = frmMain.WinFormsObject("panelControl1").WinFormsObject("gcContainer").WinFormsObject("MasterDetail").WinFormsObject("gridControl1")

  ' Collapse and expand rows in the main view
  Call Grid.Collapse (0)
  Call Grid.Collapse (1)
  Call Grid.Expand (2)

  ' Expand the first row in the "Products" view
  Call Grid.wChildView(0,"Products").Expand (0)

  ' Expand the first group in the "Order Details" view
  Call Grid.wChildView(0,"Products").wChildView(0,"Order Details").ExpandGroup (0)
End Sub

DelphiScript

procedure Main;
var p, frmMain, Grid : OleVariant;
begin
  // Obtain the application process and its main form
  p := Sys.Process('GridMainDemo');
  frmMain := p.WinFormsObject('frmMain');
  frmMain.Maximize;
  // Select the "Master-Detail" demo
  frmMain.WinFormsObject('gcNavigations').WinFormsObject('navBarControl1').Groups.Item_2(1).SelectedLinkIndex := 0;
  // Obtain the grid control
  Grid := frmMain.WinFormsObject('panelControl1').WinFormsObject('gcContainer').WinFormsObject('MasterDetail').WinFormsObject('gridControl1');

  // Collapse and expand rows in the main view
  Grid.Collapse (0);
  Grid.Collapse (1);
  Grid.Expand (2);

  // Expand the first row in the "Products" view
  Grid.wChildView(0,'Products').Expand (0);

  // Expand the first group in the "Order Details" view
  Grid.wChildView(0,'Products').wChildView(0,'Order Details').ExpandGroup (0);
end;

C++Script, C#Script

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

  // Obtain the application process and its main form
  p = Sys["Process"]("GridMainDemo");
  frmMain = p["WinFormsObject"]("frmMain");
  frmMain["Maximize"]();
  // Select the "Master-Detail" demo
  frmMain["WinFormsObject"]("gcNavigations")["WinFormsObject"]("navBarControl1")["Groups"]["Item_2"](1)["SelectedLinkIndex"] = 0;
  // Obtain the grid control
  Grid = frmMain["WinFormsObject"]("panelControl1")["WinFormsObject"]("gcContainer")["WinFormsObject"]("MasterDetail")["WinFormsObject"]("gridControl1");

  // Collapse and expand rows in the main view
  Grid["Collapse"] (0);
  Grid["Collapse"] (1);
  Grid["Expand"] (2);

  // Expand the first row in the "Products" view
  Grid["wChildView"](0,"Products")["Expand"] (0);

  // Expand the first group in the "Order Details" view
  Grid["wChildView"](0,"Products")["wChildView"](0,"Order Details")["ExpandGroup"] (0);
}

Simulating Mouse and Keyboard Actions

It is possible to expand or collapse a row or group by double-clicking its indicator. You can simulate these clicks using the DblClickRowIndicator action of the DevExpressXtraGrid and DevExpressXtraGridView objects. The action affects both kinds of rows. The actual kind of a row is defined by the row index: data rows have positive or zero indexes, whereas group row indexes are negative.

The example works with the GridMainDemo application.

How to get the application

JavaScript, JScript

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

  // Obtain the application process and its main form
  p = Sys.Process("GridMainDemo");
  frmMain = p.WinFormsObject("frmMain");
  frmMain.Maximize();
  // Select the "Master-Detail" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("navBarControl1").Groups.Item_2(1).SelectedLinkIndex = 0;
  // Obtain the grid control
  Grid = frmMain.WinFormsObject("panelControl1").WinFormsObject("gcContainer").WinFormsObject("MasterDetail").WinFormsObject("gridControl1");

  // Collapse and expand rows in the main view
  Grid.DblClickRowIndicator (0);
  Grid.DblClickRowIndicator (1);
  Grid.DblClickRowIndicator (2);

  // Expand the first row in the "Products" view
  Grid.wChildView(0,"Products").DblClickRowIndicator (0);

  // Expand the first group in the "Order Details" view
  Grid.wChildView(0,"Products").wChildView(0,"Order Details").DblClickRowIndicator (-1);
}

Python

def Main ():

  # Obtain the application process and its main form
  p = Sys.Process("GridMainDemo")
  frmMain = p.WinFormsObject("frmMain")
  frmMain.Maximize()
  # Select the "Master-Detail" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("navBarControl1").Groups.Item_2[1].SelectedLinkIndex = 0
  # Obtain the grid control
  Grid = frmMain.WinFormsObject("panelControl1").WinFormsObject("gcContainer").WinFormsObject("MasterDetail").WinFormsObject("gridControl1")

  # Collapse and expand rows in the main view
  Grid.DblClickRowIndicator (0)
  Grid.DblClickRowIndicator (1)
  Grid.DblClickRowIndicator (2)

  # Expand the first row in the "Products" view
  Grid.wChildView(0,"Products").DblClickRowIndicator (0)

  # Expand the first group in the "Order Details" view
  Grid.wChildView(0,"Products").wChildView(0,"Order Details").DblClickRowIndicator (-1)

VBScript

Sub Main
  Dim p, frmMain, Grid

  ' Obtain the application process and its main form
  Set p = Sys.Process("GridMainDemo")
  Set frmMain = p.WinFormsObject("frmMain")
  frmMain.Maximize
  ' Select the "Master-Detail" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("navBarControl1").Groups.Item_2(1).SelectedLinkIndex = 0
  ' Obtain the grid control
  Set Grid = frmMain.WinFormsObject("panelControl1").WinFormsObject("gcContainer").WinFormsObject("MasterDetail").WinFormsObject("gridControl1")

  ' Collapse and expand rows in the main view
  Call Grid.DblClickRowIndicator (0)
  Call Grid.DblClickRowIndicator (1)
  Call Grid.DblClickRowIndicator (2)

  ' Expand the first row in the "Products" view
  Call Grid.wChildView(0,"Products").DblClickRowIndicator (0)

  ' Expand the first group in the "Order Details" view
  Call Grid.wChildView(0,"Products").wChildView(0,"Order Details").DblClickRowIndicator (-1)
End Sub

DelphiScript

procedure Main;
var p, frmMain, Grid : OleVariant;
begin
  // Obtain the application process and its main form
  p := Sys.Process('GridMainDemo');
  frmMain := p.WinFormsObject('frmMain');
  frmMain.Maximize;
  // Select the 'Master-Detail' demo
  frmMain.WinFormsObject('gcNavigations').WinFormsObject('navBarControl1').Groups.Item_2(1).SelectedLinkIndex := 0;
  // Obtain the grid control
  Grid := frmMain.WinFormsObject('panelControl1').WinFormsObject('gcContainer').WinFormsObject('MasterDetail').WinFormsObject('gridControl1');

  // Collapse and expand rows in the main view
  Grid.DblClickRowIndicator (0);
  Grid.DblClickRowIndicator (1);
  Grid.DblClickRowIndicator (2);

  // Expand the first row in the 'Products' view
  Grid.wChildView(0,'Products').DblClickRowIndicator (0);

  // Expand the first group in the 'Order Details' view
  Grid.wChildView(0,'Products').wChildView(0,'Order Details').DblClickRowIndicator (-1);
end;

C++Script, C#Script

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

  // Obtain the application process and its main form
  p = Sys["Process"]("GridMainDemo");
  frmMain = p["WinFormsObject"]("frmMain");
  frmMain["Maximize"]();
  // Select the "Master-Detail" demo
  frmMain["WinFormsObject"]("gcNavigations")["WinFormsObject"]("navBarControl1")["Groups"]["Item_2"](1)["SelectedLinkIndex"] = 0;
  // Obtain the grid control
  Grid = frmMain["WinFormsObject"]("panelControl1")["WinFormsObject"]("gcContainer")["WinFormsObject"]("MasterDetail")["WinFormsObject"]("gridControl1");

  // Collapse and expand rows in the main view
  Grid["DblClickRowIndicator"](0);
  Grid["DblClickRowIndicator"](1);
  Grid["DblClickRowIndicator"](2);

  // Expand the first row in the "Products" view
  Grid["wChildView"](0,"Products")["DblClickRowIndicator"](0);

  // Expand the first group in the "Order Details" view
  Grid["wChildView"](0,"Products")["wChildView"](0,"Order Details")["DblClickRowIndicator"](-1);
}

You can also expand and collapse XtraGrid rows and groups using the special keyboard shortcuts. For example, the Ctrl+Num Plus shortcut expands the currently selected row or group, and Ctrl+Num Minus collapses it. To send these keystrokes to the grid control, use the Keys action that is added to all onscreen objects by TestComplete.

The example below demonstrates how to expand and collapse XtraGrid rows by simulating keyboard shortcuts.

The example works with the GridMainDemo application.

How to get the application

JavaScript, JScript

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

  // Obtain the application process and its main form
  p = Sys.Process("GridMainDemo");
  frmMain = p.WinFormsObject("frmMain");
  frmMain.Maximize();
  // Select the "Master-Detail" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("navBarControl1").Groups.Item_2(1).SelectedLinkIndex = 0;
  // Obtain the grid control
  Grid = frmMain.WinFormsObject("panelControl1").WinFormsObject("gcContainer").WinFormsObject("MasterDetail").WinFormsObject("gridControl1");

  // Collapse and expand rows in the main view
  Grid.ClickRowIndicator (0);
  Grid.Keys ("^[NumMinus]");
  Grid.ClickRowIndicator (1);
  Grid.Keys ("^[NumMinus]");
  Grid.ClickRowIndicator (2);
  Grid.Keys ("^[NumPlus]");

  // Expand the first row in the "Products" view
  Grid.wChildView(0,"Products").ClickRowIndicator (0);
  Grid.Keys ("^[NumPlus]");

  // Expand the first group in the "Order Details" view
  Grid.wChildView(0,"Products").wChildView(0,"Order Details").ClickRowIndicator (-1);
  Grid.Keys ("^[NumPlus]");
}

Python

def Main ():

  # Obtain the application process and its main form
  p = Sys.Process("GridMainDemo")
  frmMain = p.WinFormsObject("frmMain")
  frmMain.Maximize()
  # Select the "Master-Detail" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("navBarControl1").Groups.Item_2[1].SelectedLinkIndex = 0
  # Obtain the grid control
  Grid = frmMain.WinFormsObject("panelControl1").WinFormsObject("gcContainer").WinFormsObject("MasterDetail").WinFormsObject("gridControl1")

  # Collapse and expand rows in the main view
  Grid.ClickRowIndicator (0)
  Grid.Keys ("^[NumMinus]")
  Grid.ClickRowIndicator (1)
  Grid.Keys ("^[NumMinus]")
  Grid.ClickRowIndicator (2)
  Grid.Keys ("^[NumPlus]")

  # Expand the first row in the "Products" view
  Grid.wChildView[0,"Products"].ClickRowIndicator (0)
  Grid.Keys ("^[NumPlus]")

  # Expand the first group in the "Order Details" view
  Grid.wChildView[0,"Products"].wChildView(0,"Order Details").ClickRowIndicator (-1)
  Grid.Keys ("^[NumPlus]")

VBScript

Sub Main
  Dim p, frmMain, Grid

  ' Obtain the application process and its main form
  Set p = Sys.Process("GridMainDemo")
  Set frmMain = p.WinFormsObject("frmMain")
  frmMain.Maximize
  ' Select the "Master-Detail" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("navBarControl1").Groups.Item_2(1).SelectedLinkIndex = 0
  ' Obtain the grid control
  Set Grid = frmMain.WinFormsObject("panelControl1").WinFormsObject("gcContainer").WinFormsObject("MasterDetail").WinFormsObject("gridControl1")

  ' Collapse and expand rows in the main view
  Call Grid.ClickRowIndicator (0)
  Grid.Keys ("^[NumMinus]")
  Call Grid.ClickRowIndicator (1)
  Grid.Keys ("^[NumMinus]")
  Call Grid.ClickRowIndicator (2)
  Grid.Keys ("^[NumPlus]")

  ' Expand the first row in the "Products" view
  Call Grid.wChildView(0,"Products").ClickRowIndicator (0)
  Grid.Keys ("^[NumPlus]")

  ' Expand the first group in the "Order Details" view
  Call Grid.wChildView(0,"Products").wChildView(0,"Order Details").ClickRowIndicator (-1)
  Grid.Keys ("^[NumPlus]")
End Sub

DelphiScript

procedure Main;
var p, frmMain, Grid : OleVariant;
begin
  // Obtain the application process and its main form
  p := Sys.Process('GridMainDemo');
  frmMain := p.WinFormsObject('frmMain');
  frmMain.Maximize;
  // Select the 'Master-Detail' demo
  frmMain.WinFormsObject('gcNavigations').WinFormsObject('navBarControl1').Groups.Item_2(1).SelectedLinkIndex := 0;
  // Obtain the grid control
  Grid := frmMain.WinFormsObject('panelControl1').WinFormsObject('gcContainer').WinFormsObject('MasterDetail').WinFormsObject('gridControl1');

  // Collapse and expand rows in the main view
  Grid.ClickRowIndicator (0);
  Grid.Keys ('^[NumMinus]');
  Grid.ClickRowIndicator (1);
  Grid.Keys ('^[NumMinus]');
  Grid.ClickRowIndicator (2);
  Grid.Keys ('^[NumPlus]');

  // Expand the first row in the 'Products' view
  Grid.wChildView(0,'Products').ClickRowIndicator (0);
  Grid.Keys ('^[NumPlus]');

  // Expand the first group in the 'Order Details' view
  Grid.wChildView(0,'Products').wChildView(0,'Order Details').ClickRowIndicator (-1);
  Grid.Keys ('^[NumPlus]');
end;

C++Script, C#Script

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

  // Obtain the application process and its main form
  p = Sys["Process"]("GridMainDemo");
  frmMain = p["WinFormsObject"]("frmMain");
  frmMain["Maximize"]();
  // Select the "Master-Detail" demo
  frmMain["WinFormsObject"]("gcNavigations")["WinFormsObject"]("navBarControl1")["Groups"]["Item_2"](1)["SelectedLinkIndex"] = 0;
  // Obtain the grid control
  Grid = frmMain["WinFormsObject"]("panelControl1")["WinFormsObject"]("gcContainer")["WinFormsObject"]("MasterDetail")["WinFormsObject"]("gridControl1");

  // Collapse and expand rows in the main view
  Grid["ClickRowIndicator"](0);
  Grid["Keys"]("^[NumMinus]");
  Grid["ClickRowIndicator"](1);
  Grid["Keys"]("^[NumMinus]");
  Grid["ClickRowIndicator"](2);
  Grid["Keys"]("^[NumPlus]");

  // Expand the first row in the "Products" view
  Grid["wChildView"](0,"Products")["ClickRowIndicator"](0);
  Grid["Keys"]("^[NumPlus]");

  // Expand the first group in the "Order Details" view
  Grid["wChildView"](0,"Products")["wChildView"](0,"Order Details")["ClickRowIndicator"](-1);
  Grid["Keys"]("^[NumPlus]");
}

Using Internal Methods of the XtraGrid Control

Another way to expand and collapse XtraGrid rows is to use the internal methods of the XtraGrid control. The following table lists methods that can be used to expand and collapse data rows and groups in the XtraGrid control. For more information on these methods, please refer to the XtraGrid Suite documentation.

Method Description
viewObj.ExpandMasterRow (RowIndex) Expands a data row specified by its index (zero-based).
viewObj.CollapseMasterRow (RowIndex) Collapses a data row specified by its index (zero-based).
viewObj.GetMasterRowExpanded (RowIndex) Return True if the specified data row is expanded; otherwise False.
viewObj.CollapseAllDetails() Collapses all data rows in the given view.
viewObj.ExpandGroupRow (GroupIndex) Expands a group specified by its internal index (-1-based).
viewObj.CollapseGroupRow (GroupIndex) Collapses a group specified by its internal index (-1-based).
viewObj.GetRowExpanded (GroupIndex) Returns True if the specified group is expanded, and False otherwise.
viewObj.ExpandAllGroups() Expands all groups in the given view.
viewObj.CollapseAllGroups() Collapses all groups in the given view.

As you can see, all of these methods apply to the view object that contains the desired row or group. To learn how you can obtain grid views, see Accessing Views in Developer Express XtraGrid. Also note that these methods take internal row indexes as parameters. Indexes of data rows are zero-based, meaning that they start with 0 and continue down the grid, for example, 0,1,2,3... Groups have negative indexes, starting with -1 and decreasing downwards. So, the first group’s index is -1, the index of the second one is -2, and so on.

Example

The example works with the GridMainDemo application.

How to get the application

View example description

JavaScript

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

  // Obtain the application process and its main form
  p = Sys.Process("GridMainDemo");
  frmMain = p.WinFormsObject("frmMain");
  frmMain.Maximize();
  // Select the "Master-Detail" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("navBarControl1").Groups.Item_2(1).SelectedLinkIndex = 0;
  // Obtain the grid control
  Grid = frmMain.WinFormsObject("panelControl1").WinFormsObject("gcContainer").WinFormsObject("MasterDetail").WinFormsObject("gridControl1");

  // Collapse and expand rows in the main view
  Collapse (Grid, null, 0);
  Collapse (Grid, null, 1);
  Expand (Grid, null, 2);

  // Expand the first row in the "Products" view
  ChildView = GetChildView (Grid, null, 2);
  Expand (Grid, ChildView, 0);
}

function Expand (Grid, View, RowIndex)
{
  SetExpanded (Grid, View, RowIndex, true);
}

function Collapse (Grid, View, RowIndex)
{
  SetExpanded (Grid, View, RowIndex, false);
}

// Expands or collapses the specified row
function SetExpanded (Grid, View, RowIndex, Expanded)
{
  // Get the grid view object, if it is not specified
  if (strictEqual(View, null))
    View = Grid.MainView;

  // Check if the row is already expanded/collapsed
  if (equal(View.GetMasterRowExpanded (RowIndex), Expanded))
  {
    if (Expanded)
      Log.Message ("Row " + RowIndex + " is already expanded.");
    else
      Log.Message ("Row " + RowIndex + " is already collapsed.");
  }
  else
    // Expand/Collapse the row
    View.SetMasterRowExpanded (RowIndex, Expanded);
}

// Returns the child view for the specified row
function GetChildView (Grid, ParentView, RowIndex)
{
  if (strictEqual(ParentView, null))
    return Grid.MainView.GetVisibleDetailView(RowIndex)
  else
    return ParentView.GetVisibleDetailView(RowIndex);
}

JScript

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

  // Obtain the application process and its main form
  p = Sys.Process("GridMainDemo");
  frmMain = p.WinFormsObject("frmMain");
  frmMain.Maximize();
  // Select the "Master-Detail" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("navBarControl1").Groups.Item_2(1).SelectedLinkIndex = 0;
  // Obtain the grid control
  Grid = frmMain.WinFormsObject("panelControl1").WinFormsObject("gcContainer").WinFormsObject("MasterDetail").WinFormsObject("gridControl1");

  // Collapse and expand rows in the main view
  Collapse (Grid, null, 0);
  Collapse (Grid, null, 1);
  Expand (Grid, null, 2);

  // Expand the first row in the "Products" view
  ChildView = GetChildView (Grid, null, 2);
  Expand (Grid, ChildView, 0);
}

function Expand (Grid, View, RowIndex)
{
  SetExpanded (Grid, View, RowIndex, true);
}

function Collapse (Grid, View, RowIndex)
{
  SetExpanded (Grid, View, RowIndex, false);
}

// Expands or collapses the specified row
function SetExpanded (Grid, View, RowIndex, Expanded)
{
  // Get the grid view object, if it is not specified
  if (View == null)
    View = Grid.MainView;

  // Check if the row is already expanded/collapsed
  if (View.GetMasterRowExpanded (RowIndex) == Expanded)
  {
    if (Expanded)
      Log.Message ("Row " + RowIndex + " is already expanded.");
    else
      Log.Message ("Row " + RowIndex + " is already collapsed.");
  }
  else
    // Expand/Collapse the row
    View.SetMasterRowExpanded (RowIndex, Expanded);
}

// Returns the child view for the specified row
function GetChildView (Grid, ParentView, RowIndex)
{
  if (ParentView == null)
    return Grid.MainView.GetVisibleDetailView(RowIndex)
  else
    return ParentView.GetVisibleDetailView(RowIndex);
}

Python

def Main ():

  # Obtain the application process and its main form
  p = Sys.Process("GridMainDemo")
  frmMain = p.WinFormsObject("frmMain")
  frmMain.Maximize()
  # Select the "Master-Detail" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("navBarControl1").Groups.Item_2[1].SelectedLinkIndex = 0
  # Obtain the grid control
  Grid = frmMain.WinFormsObject("panelControl1").WinFormsObject("gcContainer").WinFormsObject("MasterDetail").WinFormsObject("gridControl1")

  # Collapse and expand rows in the main view
  Collapse (Grid, None, 0)
  Collapse (Grid, None, 1)
  Expand (Grid, None, 2)

  # Expand the first row in the "Products" view
  ChildView = GetChildView (Grid, None, 2)
  Expand (Grid, ChildView, 0)

def Expand (Grid, View, RowIndex):
  SetExpanded (Grid, View, RowIndex, True)

def Collapse (Grid, View, RowIndex):
  SetExpanded (Grid, View, RowIndex, False)

# Expands or collapses the specified row
def SetExpanded (Grid, View, RowIndex, Expanded):
  # Get the grid view object, if it is not specified
  if (View == None):
    View = Grid.MainView 

  # Check if the row is already expanded/collapsed
  if (View.GetMasterRowExpanded (RowIndex) == Expanded):
    if Expanded:
      Log.Message ("Row " + RowIndex + " is already expanded.")
    else:
      Log.Message ("Row " + RowIndex + " is already collapsed.")
  else:
    # Expand/Collapse the row
    View.SetMasterRowExpanded (RowIndex, Expanded)

# Returns the child view for the specified row
def GetChildView (Grid, ParentView, RowIndex):
  if (ParentView == None):
    return Grid.MainView.GetVisibleDetailView(RowIndex)
  else:
    return ParentView.GetVisibleDetailView(RowIndex)

VBScript

Sub Main
  Dim p, frmMain, Grid, ChildView

  ' Obtain the application process and its main form
  Set p = Sys.Process("GridMainDemo")
  Set frmMain = p.WinFormsObject("frmMain")
  frmMain.Maximize
  ' Select the "Master-Detail" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("navBarControl1").Groups.Item_2(1).SelectedLinkIndex = 0
  ' Obtain the grid control
  Set Grid = frmMain.WinFormsObject("panelControl1").WinFormsObject("gcContainer").WinFormsObject("MasterDetail").WinFormsObject("gridControl1")

  ' Collapse and expand rows in the main view
  Call Collapse (Grid, Nothing, 0)
  Call Collapse (Grid, Nothing, 1)
  Call Expand (Grid, Nothing, 2)

  ' Expand the first row in the "Products" view
  Set ChildView = GetChildView (Grid, Nothing, 2)
  Call Expand (Grid, ChildView, 0)
End Sub

Sub Expand (Grid, View, RowIndex)
  Call SetExpanded (Grid, View, RowIndex, True)
End Sub

Sub Collapse (Grid, View, RowIndex)
  Call SetExpanded (Grid, View, RowIndex, False)
End Sub

' Expands or collapses the specified row
Sub SetExpanded (Grid, View, RowIndex, Expanded)
  ' Get the grid view object, if it is not specified
  If View Is Nothing Then
    Set View = Grid.MainView
  End If 

  ' Check if the row is already expanded/collapsed
  If View.GetMasterRowExpanded (RowIndex) = Expanded Then
    If Expanded Then
      Log.Message ("Row " & RowIndex & " is already expanded.")
    Else
      Log.Message ("Row " & RowIndex & " is already collapsed.")
    End If
  Else
    ' Expand/Collapse the row
    Call View.SetMasterRowExpanded (RowIndex, Expanded)
  End If
End Sub

' Returns the child view for the specified row
Function GetChildView (Grid, ParentView, RowIndex)
  If ParentView Is Nothing Then
    Set GetChildView = Grid.MainView.GetVisibleDetailView(RowIndex)
  Else
    Set GetChildView = ParentView.GetVisibleDetailView(RowIndex)
  End If
End Function

DelphiScript

procedure Expand (Grid, View, RowIndex); forward;
procedure Collapse (Grid, View, RowIndex); forward;
procedure SetExpanded (Grid, View, RowIndex, Expanded); forward;
function GetChildView (Grid, ParentView, RowIndex); forward;

procedure Main;
var p, frmMain, Grid, ChildView : OleVariant;
begin
  // Obtain the application process and its main form
  p := Sys.Process('GridMainDemo');
  frmMain := p.WinFormsObject('frmMain');
  frmMain.Maximize;
  // Select the 'Master-Detail' demo
  frmMain.WinFormsObject('gcNavigations').WinFormsObject('navBarControl1').Groups.Item_2(1).SelectedLinkIndex := 0;
  // Obtain the grid control
  Grid := frmMain.WinFormsObject('panelControl1').WinFormsObject('gcContainer').WinFormsObject('MasterDetail').WinFormsObject('gridControl1');

  // Collapse and expand rows in the main view
  Collapse (Grid, nil, 0);
  Collapse (Grid, nil, 1);
  Expand (Grid, nil, 2);

  // Expand the first row in the 'Products' view
  ChildView := GetChildView (Grid, nil, 2);
  Expand (Grid, ChildView, 0);
end;

procedure Expand (Grid, View, RowIndex);
begin
  SetExpanded (Grid, View, RowIndex, true);
end;

procedure Collapse (Grid, View, RowIndex);
begin
  SetExpanded (Grid, View, RowIndex, false);
end;

// Expands or collapses the specified row
procedure SetExpanded (Grid, View, RowIndex, Expanded);
var View : OleVariant;
begin
  // Get the grid view object, if it is not specified
  if View = nil then
    View := Grid.MainView;

  // Check if the row is already expanded/collapsed
  if View.GetMasterRowExpanded (RowIndex) = Expanded then
  begin
    if Expanded then
      Log.Message ('Row ' + aqConvert.VarToStr(RowIndex) + ' is already expanded.')
    else
      Log.Message ('Row ' + aqConvert.VarToStr(RowIndex) + ' is already collapsed.');
  end
  else
    // Expand/Collapse the row
    View.SetMasterRowExpanded (RowIndex, Expanded);
end;

// Returns the child view for the specified row
function GetChildView (Grid, ParentView, RowIndex);
begin
  if ParentView = nil then
    Result := Grid.MainView.GetVisibleDetailView(RowIndex)
  else
    Result := ParentView.GetVisibleDetailView(RowIndex);
end;

C++Script, C#Script

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

  // Obtain the application process and its main form
  p = Sys["Process"]("GridMainDemo");
  frmMain = p["WinFormsObject"]("frmMain");
  frmMain["Maximize"]();
  // Select the "Master-Detail" demo
  frmMain["WinFormsObject"]("gcNavigations")["WinFormsObject"]("navBarControl1")["Groups"]["Item_2"](1)["SelectedLinkIndex"] = 0;
  // Obtain the grid control
  Grid = frmMain["WinFormsObject"]("panelControl1")["WinFormsObject"]("gcContainer")["WinFormsObject"]("MasterDetail")["WinFormsObject"]("gridControl1");

  // Collapse and expand rows in the main view
  Collapse (Grid, null, 0);
  Collapse (Grid, null, 1);
  Expand (Grid, null, 2);

  // Expand the first row in the "Products" view
  ChildView = GetChildView (Grid, null, 2);
  Expand (Grid, ChildView, 0);
}

function Expand (Grid, View, RowIndex)
{
  SetExpanded (Grid, View, RowIndex, true);
}

function Collapse (Grid, View, RowIndex)
{
  SetExpanded (Grid, View, RowIndex, false);
}

// Expands or collapses the specified row
function SetExpanded (Grid, View, RowIndex, Expanded)
{
  // Get the grid view object, if it is not specified
  if (View == null)
    View = Grid["MainView"];

  // Check if the row is already expanded/collapsed
  if (View["GetMasterRowExpanded"](RowIndex) == Expanded)
  {
    if (Expanded)
      Log["Message"]("Row " + RowIndex + " is already expanded.");
    else
      Log["Message"]("Row " + RowIndex + " is already collapsed.");
  }
  else
    // Expand/Collapse the row
    View["SetMasterRowExpanded"](RowIndex, Expanded);
}

// Returns the child view for the specified row
function GetChildView (Grid, ParentView, RowIndex)
{
  if (ParentView == null)
    return Grid["MainView"]["GetVisibleDetailView"](RowIndex)
  else
    return ParentView["GetVisibleDetailView"](RowIndex);
}

As already mentioned, you can collapse all data rows in a particular grid view using the view’s CollapseAllDetails method. However, the XtraGrid control does not have built-in methods that let you expand all data rows at once. If you need this functionality in your tests, you can write a sample routine that calls the grid view’s ExpandMasterRow method for all rows in a loop.

Example 2

The example works with the GridMainDemo application.

How to get the application

View example description

JavaScript

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

  // Obtain the application process and its main form
  p = Sys.Process("GridMainDemo");
  frmMain = p.WinFormsObject("frmMain");
  frmMain.Maximize();
  // Select the "Master-Detail" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("navBarControl1").Groups.Item_2(1).SelectedLinkIndex = 0;
  // Obtain the grid control
  Grid = frmMain.WinFormsObject("panelControl1").WinFormsObject("gcContainer").WinFormsObject("MasterDetail").WinFormsObject("gridControl1");

  ExpandAllDetails (Grid, null);

  CollapseAllDetails (Grid, null);
}

// Expands all data rows in the specified view
function ExpandAllDetails (Grid, View)
{
  var nRows;

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

  // Expand all data rows
  nRows = View.DataRowCount;
  for (let i = 0; i < nRows; i++)
    View.ExpandMasterRow (i);
}

// Collapses all data rows in the specified view
function CollapseAllDetails (Grid, View)
{
  // Get the grid view object, if it is not specified
  if (strictEqual(View, null))
    View = Grid.MainView;
  // Collapse all data rows
  View.CollapseAllDetails();
}

JScript

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

  // Obtain the application process and its main form
  p = Sys.Process("GridMainDemo");
  frmMain = p.WinFormsObject("frmMain");
  frmMain.Maximize();
  // Select the "Master-Detail" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("navBarControl1").Groups.Item_2(1).SelectedLinkIndex = 0;
  // Obtain the grid control
  Grid = frmMain.WinFormsObject("panelControl1").WinFormsObject("gcContainer").WinFormsObject("MasterDetail").WinFormsObject("gridControl1");

  ExpandAllDetails (Grid, null);

  CollapseAllDetails (Grid, null);
}

// Expands all data rows in the specified view
function ExpandAllDetails (Grid, View)
{
  var nRows, i;

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

  // Expand all data rows
  nRows = View.DataRowCount;
  for (i = 0; i < nRows; i++)
    View.ExpandMasterRow (i);
}

// Collapses all data rows in the specified view
function CollapseAllDetails (Grid, View)
{
  // Get the grid view object, if it is not specified
  if (View == null)
    View = Grid.MainView;
  // Collapse all data rows
  View.CollapseAllDetails();
}

Python

def Main ():

  # Obtain the application process and its main form
  p = Sys.Process("GridMainDemo")
  frmMain = p.WinFormsObject("frmMain")
  frmMain.Maximize()
  # Select the "Master-Detail" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("navBarControl1").Groups.Item_2[1].SelectedLinkIndex = 0
  # Obtain the grid control
  Grid = frmMain.WinFormsObject("panelControl1").WinFormsObject("gcContainer").WinFormsObject("MasterDetail").WinFormsObject("gridControl1")

  ExpandAllDetails (Grid, None)

  CollapseAllDetails (Grid, None)

# Expands all data rows in the specified view
def ExpandAllDetails (Grid, View):

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

  # Expand all data rows
  nRows = View.DataRowCount
  for i in range(0, nRows-1):
    View.ExpandMasterRow (i)

# Collapses all data rows in the specified view
def CollapseAllDetails (Grid, View):
  # Get the grid view object, if it is not specified
  if (View == None):
    View = Grid.MainView
  # Collapse all data rows
  View.CollapseAllDetails()

VBScript

Sub Main
  Dim p, frmMain, Grid

  ' Obtain the application process and its main form
  Set p = Sys.Process("GridMainDemo")
  Set frmMain = p.WinFormsObject("frmMain")
  frmMain.Maximize
  ' Select the "Master-Detail" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("navBarControl1").Groups.Item_2(1).SelectedLinkIndex = 0
  ' Obtain the grid control
  Set Grid = frmMain.WinFormsObject("panelControl1").WinFormsObject("gcContainer").WinFormsObject("MasterDetail").WinFormsObject("gridControl1")

  Call ExpandAllDetails (Grid, Nothing)

  Call CollapseAllDetails (Grid, Nothing)
End Sub

' Expands all data rows in the specified view
Sub ExpandAllDetails (Grid, View)
  Dim nRows, i

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

  ' Expand all data rows
  nRows = View.DataRowCount
  For i = 0 To nRows-1
    View.ExpandMasterRow (i)
  Next
End Sub

' Collapses all data rows in the specified view
Sub CollapseAllDetails (Grid, View)
  ' Get the grid view object, if it is not specified
  If View Is Nothing Then
    Set View = Grid.MainView
  End If

  ' Collapse all data rows
  View.CollapseAllDetails
End Sub

DelphiScript

procedure ExpandAllDetails (Grid, View); forward;
procedure CollapseAllDetails (Grid, View); forward;

procedure Main;
var p, frmMain, Grid : OleVariant;
begin
  // Obtain the application process and its main form
  p := Sys.Process('GridMainDemo');
  frmMain := p.WinFormsObject('frmMain');
  frmMain.Maximize;
  // Select the 'Master-Detail' demo
  frmMain.WinFormsObject('gcNavigations').WinFormsObject('navBarControl1').Groups.Item_2(1).SelectedLinkIndex := 0;
  // Obtain the grid control
  Grid := frmMain.WinFormsObject('panelControl1').WinFormsObject('gcContainer').WinFormsObject('MasterDetail').WinFormsObject('gridControl1');

  ExpandAllDetails (Grid, nil);

  CollapseAllDetails (Grid, nil);
end;

// Expands all data rows in the specified view
procedure ExpandAllDetails (Grid, View);
var nRows, i : OleVariant;
begin
  // Get the grid view object, if it is not specified
  if View = nil then
    View := Grid.MainView;

  // Expand all data rows
  nRows := View.DataRowCount;
  for i := 0 to nRows-1 do
    View.ExpandMasterRow (i);
end;

// Collapses all data rows in the specified view
procedure CollapseAllDetails (Grid, View);
begin
  // Get the grid view object, if it is not specified
  if View = nil then
    View := Grid.MainView;
  // Collapse all data rows
  View.CollapseAllDetails;
end;

C++Script, C#Script

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

  // Obtain the application process and its main form
  p = Sys["Process"]("GridMainDemo");
  frmMain = p["WinFormsObject"]("frmMain");
  frmMain["Maximize"];
  // Select the "Master-Detail" demo
  frmMain["WinFormsObject"]("gcNavigations")["WinFormsObject"]("navBarControl1")["Groups"]["Item_2"](1)["SelectedLinkIndex"] = 0;
  // Obtain the grid control
  Grid = frmMain["WinFormsObject"]("panelControl1")["WinFormsObject"]("gcContainer")["WinFormsObject"]("MasterDetail")["WinFormsObject"]("gridControl1");

  ExpandAllDetails (Grid, null);

  CollapseAllDetails (Grid, null);
}

// Expands all data rows in the specified view
function ExpandAllDetails (Grid, View)
{
  var nRows, i;

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

  // Expand all data rows
  nRows = View["DataRowCount"];
  for (i = 0; i < nRows; i++)
    View["ExpandMasterRow"](i);
}

// Collapses all data rows in the specified view
function CollapseAllDetails (Grid, View)
{
  // Get the grid view object, if it is not specified
  if (View == null)
    View = Grid["MainView"];
  // Collapse all data rows
  View["CollapseAllDetails"]();
}

See Also

Working With Developer Express XtraGrid
Accessing Views in Developer Express XtraGrid
Collapse Action (Grid Controls)
CollapseGroup Action (Grid Controls)
Expand Action (Grid Controls)
ExpandGroup Action (Grid Controls)

Highlight search results