Edit this page

Grouping events

There are two events that are raised, when the data in RadGridView is grouped from the UI.

There are two events that are raised, when a user expands/collapses a group row in RadGridView.

The code snippets below show how to subscribe for those events:

XAML

<telerik:RadGridView Name="clubsGrid" 
            ItemsSource="{Binding Clubs}"
            Grouping="clubsGrid_Grouping"
            Grouped="clubsGrid_Grouped"
            GroupRowIsExpandedChanged="clubsGrid_GroupRowIsExpandedChanged"
            GroupRowIsExpandedChanging="clubsGrid_GroupRowIsExpandedChanging" />

C#

clubsGrid.Grouping += clubsGrid_Grouping;
clubsGrid.Grouped += clubsGrid_Grouped;
clubsGrid.GroupRowIsExpandedChanged += clubsGrid_GroupRowIsExpandedChanged;
clubsGrid.GroupRowIsExpandedChanging += clubsGrid_GroupRowIsExpandedChanging;

VB.NET

AddHandler clubsGrid.Grouping, AddressOf clubsGrid_Grouping
AddHandler clubsGrid.Grouped, AddressOf clubsGrid_Grouped
AddHandler clubsGrid.GroupRowIsExpandedChanged, AddressOf clubsGrid_GroupRowIsExpandedChanged
AddHandler clubsGrid.GroupRowIsExpandedChanging, AddressOf clubsGrid_GroupRowIsExpandedChanging

Grouping event

Via the Telerik.Windows.Controls.GridViewGroupingEventArgs of the Grouping event you can get the:

  • GroupDescriptor that defines the group to be added. It is of type IGroupDescriptor and you can cast it to ColumnGroupDescriptor if the grouping is done from the UI or to GroupDescriptor if you have added a GroupDescriptor to the GroupDescriptors collection of the gridview (this was the legacy way). From the ColumnGroupDescriptor you can access:

  • Column - the column the group is performed against

  • SortDirection - either ascending or descending

  • DisplayContent - the display content

  • Index of the descriptor in the GroupDescriptors collection

  • Action to be performed on the GroupDescriptor. The actions could be:

  • Place - the group descriptor is added to the GroupDescriptors collection (the user grouped by the column).

  • Move - the group descriptor is being moved to another index in the GroupDescriptors collection (the user rearranged the grouped columns).

  • Remove - the group descriptor is removed from the GroupDescriptors collection (the user ungrouped the column)

  • Sort - the user has sorted the grouped column by clicking on the rectangle in the group panel

  • Cancel - if you set it to True, the grouping is not performed.

For example, you can use the Grouping event to prevent the user from adding more than one group.

C#

private void clubsGrid_Grouping(object sender, GridViewGroupingEventArgs e)
{
    if (e.Action == GroupingEventAction.Place && e.Index > 0)
    {
        e.Cancel = true;
    }
}

VB.NET

Private Sub clubsGrid_Grouping(sender As Object, e As GridViewGroupingEventArgs)
    If e.Action = GroupingEventAction.Place AndAlso e.Index > 0 Then
        e.Cancel = True
    End If
End Sub

Grouped event

The Grouped event is raised when the data has already been grouped. All the additional actions performed on the group (for example sorting or removing it) will also raise the event with the relevant GridViewGroupedEventArgs.Action.

Via the GridViewGroupedEventArgs arguments you can get the:

  • GroupDescriptor that has been manipulated. It is of type IGroupDescriptor and you can cast it to ColumnGroupDescriptor if the grouping is done from the UI or to GroupDescriptor if you have added a GroupDescriptor to the GroupDescriptors collection of the gridview (this was the legacy way). From the ColumnGroupDescriptor you can access:

  • Column - the column the group is performed against

  • SortDirection - either ascending or descending

  • DisplayContent -the display content

  • Action performed on it. The actions could be:

  • Place - the group descriptor is added to the GroupDescriptors collection (the user grouped by the column).

  • Move - the group descriptor is being moved to another index in the GroupDescriptors collection (the user rearranged the grouped columns).

  • Remove - the group descriptor is removed from the GroupDescriptors collection (the user ungrouped the column)

  • Sort - the user has sorted the grouped column by clicking on the rectangle in the group panel

For example, you can use the Grouped event to get the column that is grouped by.

C#

private void clubsGrid_Grouped(object sender, GridViewGroupedEventArgs e)
{
    GridViewDataColumn column = ((Telerik.Windows.Controls.GridView.ColumnGroupDescriptor)(e.GroupDescriptor)).Column as GridViewDataColumn;
    MessageBox.Show("The GridView was grouped by column: " + column.Header.ToString());
}

VB.NET

Private Sub clubsGrid_Grouped(sender As Object, e As GridViewGroupedEventArgs)
    Dim column As GridViewDataColumn = TryCast(DirectCast(e.GroupDescriptor, Telerik.Windows.Controls.GridView.ColumnGroupDescriptor).Column, GridViewDataColumn)
    MessageBox.Show("The GridView was grouped by column: " + column.Header.ToString())
End Sub

GroupRowIsExpandedChanging event

The GroupRowIsExpandedChanging event is raised when the user expands a group row.

Via the GroupRowCancelEventArgs arguments you can Cancel the action of expanding the group row. You can also get the GridViewGroupRow (e.Row) you have just expanded.

For example, you can use the GroupRowIsExpandedChanging event like so.

C#

private void clubsGrid_GroupRowIsExpandedChanging(object sender, Telerik.Windows.Controls.GridView.GroupRowCancelEventArgs e)
{
    e.Cancel = true;
}

VB.NET

Private Sub clubsGrid_GroupRowIsExpandedChanging(sender As Object, e As Telerik.Windows.Controls.GridView.GroupRowCancelEventArgs)
    e.Cancel = True
End Sub

GroupRowIsExpandedChanged event

The GroupRowIsExpandedChanged event is raised when the user expands a group row.

Via the GroupRowEventArgs arguments you can get the GridViewGroupRow (e.Row) you have just expanded.

For example, you can use the GroupRowIsExpandedChanged event to get the expanded group row and set a different Background for it.

C#

private void clubsGrid_GroupRowIsExpandedChanged(object sender, Telerik.Windows.Controls.GridView.GroupRowEventArgs e)
{
    GridViewGroupRow expandedGroup = e.Row as GridViewGroupRow;
    if (expandedGroup.IsExpanded)
    {
        expandedGroup.Background = new SolidColorBrush(Colors.Red);
    }
    else
    {
        expandedGroup.Background = new SolidColorBrush(Colors.Green);
    }
}

VB.NET

Private Sub clubsGrid_GroupRowIsExpandedChanged(sender As Object, e As Telerik.Windows.Controls.GridView.GroupRowEventArgs)
    Dim expandedGroup As GridViewGroupRow = TryCast(e.Row, GridViewGroupRow)
    If expandedGroup.IsExpanded Then
        expandedGroup.Background = New SolidColorBrush(Colors.Red)
    Else
        expandedGroup.Background = New SolidColorBrush(Colors.Green)
    End If
End Sub

See Also