RadControls for WPF

There are two events that are raised, when the data in the RadGridView is grouped from the UI. The first one is the Grouping event and is raised before the data is grouped. The second one is the Grouped event and is raised when the data has been already grouped.

CopyXAML
<telerik:RadGridView Name="clubsGrid" 
                    ItemsSource="{Binding Clubs}"
                    Grouping="clubsGrid_Grouping"
                    Grouped="clubsGrid_Grouped" >
CopyC#
clubsGrid.Grouping += new EventHandler<GridViewGroupingEventArgs>(clubsGrid_Grouping);
clubsGrid.Grouped += new EventHandler<GridViewGroupedEventArgs>(clubsGrid_Grouped);
CopyVB.NET
clubsGrid.Grouping += New EventHandler(Of GridViewGroupingEventArgs)(clubsGrid_Grouping)
clubsGrid.Grouped += New EventHandler(Of GridViewGroupedEventArgs)(clubsGrid_Grouped)

Caution

As of Q3 2010, when the user groups from the UI - a ColumnGroupDescriptor (instead of GroupDescriptor) is added to the GroupDescriptors collection of RadGridView

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.

CopyC#
private void clubsGrid_Grouping(object sender, GridViewGroupingEventArgs e)
{
 if (e.Action == GroupingEventAction.Place && e.Index > 0)
 {
  e.Cancel = true;
 }
}
CopyVB.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 been already grouped. 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.

CopyC#
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());
}
CopyVB.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

See Also