# Column Groups

Columns in RadGridView may be grouped in **column groups** visually represented by common column headers.

## Define Column Groups

To enable **column grouping** you need to add **GridViewColumnGroup** instances to RadGridView's **ColumnGroups** collection as illustrated in **Example 1**.

**Example 1: Define column groups**

```XAML
<telerik:RadGridView x:Name="RadGridView1" GridLinesVisibility="Vertical" Width="450" CanUserFreezeColumns="False" AutoGenerateColumns="False" IsFilteringAllowed="False" ShowGroupPanel="False" RowIndicatorVisibility="Collapsed">
    <telerik:RadGridView.ColumnGroups>
        <telerik:GridViewColumnGroup Name="VehicleInfo" Header="Vehicle info" />
        <telerik:GridViewColumnGroup Name="Dimensions" Header="Dimensions" />
    </telerik:RadGridView.ColumnGroups>
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Make}" ColumnGroupName="VehicleInfo" />
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Model}" ColumnGroupName="VehicleInfo" />
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Length}" ColumnGroupName="Dimensions" />
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Width}" ColumnGroupName="Dimensions" />
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Height}" ColumnGroupName="Dimensions" />
    </telerik:RadGridView.Columns>
</telerik:RadGridView>
```

> caption RadGridView with column groups

![Telerik UI for WPF RadGridView column groups with shared Vehicle info and Dimensions headers](https://www.telerik.com/products/wpf/documentation/assets/a1001f7a7853f9c9ddb57a239189ae20/radgridview_columnheaders_7.png)

Please observe that to add a specific column to a certain column group, you need to set the **ColumnGroupName** property of the column to match the value of the **Name** property of the relevant **GridViewColumnGroup**.

Note also that the **GridViewColumnGroup** class has a **Header** property which defines the content to be shown inside the common header. It may be a simple string or a visual element.

> tip In case that no header is specified the **Name** of the **GridViewColumnGroup** will be displayed.

## Nested Column Groups

Column groups can also be **nested**. **Example 2** shows how this is achieved.

**Example 2: Nested column groups**

```XAML
<telerik:RadGridView.ColumnGroups>
    <telerik:GridViewColumnGroup Name="Data" Header="Data">
        <telerik:GridViewColumnGroup Name="VehicleInfo" Header="Vehicle info" />
        <telerik:GridViewColumnGroup Name="Dimensions" Header="Dimensions" />
    </telerik:GridViewColumnGroup>
</telerik:RadGridView.ColumnGroups>
```

> caption Nested column groups

![Telerik UI for WPF RadGridView nested column groups under a shared Data header](https://www.telerik.com/products/wpf/documentation/assets/efc635cb8cb3f608651c7a8bfeaeaa58/radgridview_columnheaders_8.png)

## Define Custom Header

You can define a **custom header** for the **GridViewColumnGroup** through its **HeaderTemplate**.

**Example 3: Define GridViewColumnGroup.HeaderTemplate**

```XAML
<telerik:GridViewColumnGroup Name="Data">
    <telerik:GridViewColumnGroup.HeaderTemplate>
        <DataTemplate>
            <TextBlock Text="Data"/>
        </DataTemplate>
    </telerik:GridViewColumnGroup.HeaderTemplate>
</telerik:GridViewColumnGroup>
```

In addition, you can style the headers by defining a style targeting the **CommonColumnHeader** element and setting it as the **HeaderStyle** for the **GridViewColumnGroup**. For information on how to apply an implicit style, have a look at the [Styling Column Groups](https://www.telerik.com/products/wpf/documentation/controls/radgridview/styles-and-templates/styling-merged-column-headers) article.

## Virtualization Modes

Column groups also support **UI virtualization** meaning that only the visual elements that are loaded in the viewable area are processed. This way, the performance of the control can be significantly enhanced.

This functionality can be configured through the **EnableColumnGroupsVirtualization** boolean property. Its default value is **True** in which case UI virtualization is enabled.

This mode is especially useful in more complicated scenarios where additional features such as [frozen columns](https://www.telerik.com/products/wpf/documentation/controls/radgridview/columns/frozen-columns), [reordering of columns](https://www.telerik.com/products/wpf/documentation/controls/radgridview/columns/reordering-columns), etc. are used.

> important When the column group virtualization feature is enabled, **column group names should be unique**.

## Add or Remove Multiple Column Groups at Once

As of **R3 2020 SP** the ColumnGroups property is of type [RadObservableCollection](https://www.telerik.com/products/wpf/documentation/common-information/consuming-data/radobservablecollection) which exposes the **AddRange** and **RemoveRange** methods that can be used when adding or removing multiple groups at once in order to improve performance.

**Example 4: Add or remove multiple column groups at once**

```C#
this.GridView.ColumnGroups.AddRange(groupsToAdd);

this.GridView.ColumnGroups.RemoveRange(groupsToRemove);
```

```VB.NET
Me.GridView.ColumnGroups.AddRange(groupsToAdd)

Me.GridView.ColumnGroups.RemoveRange(groupsToRemove)
```

An alternative is to use the **SuspendNotifications** and **ResumeNotifications** methods prior and after adding or removing the groups.

**Example 5: Suspend and resume notifications**

```C#
this.GridView.ColumnGroups.SuspendNotifications();
foreach (var group in groupsToAdd)
{
	this.GridView.ColumnGroups.Add(group);
}

foreach (var group in groupsToRemove)
{

    this.GridView.ColumnGroups.Remove(group);
    }


this.GridView.ColumnGroups.ResumeNotifications();
```

```VB.NET
Me.GridView.ColumnGroups.SuspendNotifications()
For Each group In groupsToAdd
	Me.GridView.ColumnGroups.Add(group)
Next group

For Each group In groupsToRemove
	Me.GridView.ColumnGroups.Remove(group)
Next group

Me.GridView.ColumnGroups.ResumeNotifications()
```

## Specific Scenarios

This section will cover some specific scenarios when using column groups.

For the purpose of this demonstration we will define the following groups.

**Example 6: Sample column groups setup**

```XAML
<telerik:RadGridView.ColumnGroups>			
	<telerik:GridViewColumnGroup Name="Level2group1">
		<telerik:GridViewColumnGroup Name="Level1group11"/>
		<telerik:GridViewColumnGroup Name="Level1group12"/>
	</telerik:GridViewColumnGroup>
	<telerik:GridViewColumnGroup Name="Level2group2">
		<telerik:GridViewColumnGroup Name="Level1group21"/>
		<telerik:GridViewColumnGroup Name="Level1group22"/>
	</telerik:GridViewColumnGroup>
</telerik:RadGridView.ColumnGroups>
```

In case the user moves the frozen column splitter in the middle of an existing column group, then it will be **divided into two separate groups**. **Figures 3 to 5** demonstrate this scenario.

> caption Initial setup with FrozenColumnSplitter at default position

![Telerik UI for WPF RadGridView column groups before moving the frozen column splitter](https://www.telerik.com/products/wpf/documentation/assets/bfb7a84b86d3abef3d360279fbe2ec2f/columngroups_frozencolumn_1.png)

> caption A duplicate column group is created when FrozenColumnSplitter moved after the first column

![Telerik UI for WPF RadGridView column groups split after moving the frozen column splitter past the first column](https://www.telerik.com/products/wpf/documentation/assets/6622095c9fe97fce1aca93b0e205d18a/columngroups_frozencolumn_2.png)

> caption ColumnGroup's header is still visible after scrolling to the right

![Telerik UI for WPF RadGridView column group header remaining visible during horizontal scrolling](https://www.telerik.com/products/wpf/documentation/assets/cedf2a481b8f5ea554854417cd92c3c5/columngroups_frozencolumn_3.png)

When scrolling horizontally, the column group header remains visible until there are still visible sub-columns under the specific group.

Defined column groups are now **drawn only if they are specified for at least one visible column**.

When some GridViewColumns do not have a **ColumnGroupName** specified, they are **placed under an empty ColumnGroup**. This is illustrated in **Figure 6**.

> caption Various setups illustrating the default space fill

![Telerik UI for WPF RadGridView column groups showing default empty group space layout example one](https://www.telerik.com/products/wpf/documentation/assets/5704909cef2af43c18a26f16dd12103f/columngroups_defaultgroups_1.png)

![Telerik UI for WPF RadGridView column groups showing default empty group space layout example two](https://www.telerik.com/products/wpf/documentation/assets/1b74793ce91e76375c290a5e946cde43/columngroups_defaultgroups_2.png)

![Telerik UI for WPF RadGridView column groups showing default empty group space layout example three](https://www.telerik.com/products/wpf/documentation/assets/d285890db1c372f820f25cfe29e42a4a/columngroups_defaultgroups_3.png)

## See Also

* [Styling Column Groups](https://www.telerik.com/products/wpf/documentation/controls/radgridview/styles-and-templates/styling-merged-column-headers)
* [Column Headers](https://www.telerik.com/products/wpf/documentation/controls/radgridview/columns/column-headers)
* [Reordering Columns](https://www.telerik.com/products/wpf/documentation/controls/radgridview/columns/reordering-columns)
* [Frozen Columns](https://www.telerik.com/products/wpf/documentation/controls/radgridview/columns/frozen-columns)
