New to Telerik UI for WinForms? Download free 30-day trial

Expand Behavior

In this article we will show you how to change the initial expand/collapse behavior of RadPivotGrid rows and columns.

Initial state is the state of RadPivotGrid after refresh of the used data provider.

GroupsExpandBehavior

In order to control the expand/collapse state of rows and columns in RadPivotGrid you have to use an instance of GroupsExpandBehavior class from Telerik.Pivot.Core namespace. It has two properties that you can modify:

  • Expanded: Bool property which controls the state. When it is true the groups will be expanded. When the value is false the groups will be collapsed.

  • UpToLevel: Integer property which controls the levels for which to apply the behavior.

You can combine the two properties to achieve the desired result. The default value of Expanded property is true, so if you don't set it (or if you do not set ExpandBehavior), all groups will be expanded. If you do not set UpToLevel, the behavior will be applied to all groups (which is its default state). For example, if you set Expanded = false and UpToLevel = 2, all levels up to the set one (levels 0 and 1) will be collapsed, all groups with level greater than or equal to 2 will be expanded. So at initial state you will see all groups collapsed, but if you expand the first two levels, all other groups below them will be expanded.

Figure 1: Group Levels

WinForms RadPivotGrid Group Levels

Set Expand Behavior

RadPivotGrid has two properties to control the expand behavior - RowGroupsExpandBehavior and ColumnGroupsExpandBehavior. As you can guess, the first one controls the expand behavior of the rows and the second one - columns behavior. If you do not set these properties, all groups in rows and columns will be expanded.

Expanding Groups


RadPivotGrid pivot = new RadPivotGrid();
pivot.RowGroupsExpandBehavior = new GroupsExpandBehavior()
{
    Expanded = false,
    UpToLevel = 2
};
pivot.ColumnGroupsExpandBehavior = new GroupsExpandBehavior() { Expanded = false };

Dim pivot As New RadPivotGrid()
pivot.RowGroupsExpandBehavior = New GroupsExpandBehavior() With {.Expanded = False, .UpToLevel = 2}
pivot.ColumnGroupsExpandBehavior = New GroupsExpandBehavior() With {.Expanded = False}

Change Behavior at Run-time

If you want to collapse all groups in RadPivotGrid you can change the behavior at run-time and refresh the data provider to apply the change immediately. For example, you may add two buttons in your application and handle the Click event for each of them in order to expand/collapse the groups. Note that the new behavior will be applied each time when the data provider is refreshed.

Changing Expand Behavior


public void ExpandGroupsButton_Click(object sender, RoutedEventArgs e)
{
    (this.pivot.RowGroupsExpandBehavior as GroupsExpandBehavior).Expanded = true;
    (this.pivot.ColumnGroupsExpandBehavior as GroupsExpandBehavior).Expanded = true;
    this.pivot.DataProvider.Refresh();
}

private void CollapseGroupsButton_Click(object sender, RoutedEventArgs e)
{
    (this.pivot.RowGroupsExpandBehavior as GroupsExpandBehavior).Expanded = false;
    (this.pivot.ColumnGroupsExpandBehavior as GroupsExpandBehavior).Expanded = false;
    this.pivot.DataProvider.Refresh();
}

Public Sub ExpandGroupsButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    TryCast(Me.pivot.RowGroupsExpandBehavior, GroupsExpandBehavior).Expanded = True
    TryCast(Me.pivot.ColumnGroupsExpandBehavior, GroupsExpandBehavior).Expanded = True
    Me.pivot.DataProvider.Refresh()
End Sub
Private Sub CollapseGroupsButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    TryCast(Me.pivot.RowGroupsExpandBehavior, GroupsExpandBehavior).Expanded = False
    TryCast(Me.pivot.ColumnGroupsExpandBehavior, GroupsExpandBehavior).Expanded = False
    Me.pivot.DataProvider.Refresh()
End Sub

See Also

In this article