# Group

## Content

Grouping refers to the process of organizing grid data into a hierarchical structure where rows with common column values are displayed as a group. The grouping feature also lets you collapse or expand the groups to support faster analysis of grid data. Also, the grouping feature allows users to dynamically configure a tree-view type structure. When in Group mode, a "grouping area" is added to the top of the grid, providing an intuitive interface for specifying column groups. In code, this collection is accessed through the GroupedColumns collection and consists of [C1DataColumn](/componentone/docs/win/online-truedbgrid/) objects that have been moved to the grouping area; it is similar to the [C1DataColumnCollection](/componentone/docs/win/online-truedbgrid/) class.

The grouping area is created when [DataView](/componentone/docs/win/online-truedbgrid/) is set to [DataViewEnum.GroupBy](/componentone/docs/win/online-truedbgrid/). When [AllowColMove](/componentone/docs/win/online-truedbgrid/) is set to **True**, the grid will support the ability to move one or more columns into this area. Users can do this by selecting a single column and dragging its header into the grouping area. This action can also be performed in code by invoking the [Add](/componentone/docs/win/online-truedbgrid/) method of the [GroupedColumnCollection](/componentone/docs/win/online-truedbgrid/). When a column is first added to the grouping area, nodes are added to the grid. Each node represents the unique value of the grouped column. Similarly when the last grouped column is removed from the area, the nodes are removed and the display will be similar to a normal grid.

When the expand icon ("+") is clicked the grid expands and the next set of grouping column data appears. If there is another grouped column, then this column has an expand icon next to it also. With the addition of each grouped column, another level of sorted information gets added to the tree view. When the expand icon on the final column in the **GroupedColumns** collection is clicked the data in the remaining columns is displayed in the grid's Normal style, as shown below:

![](https://cdn.mescius.io/document-site-files/images/a1b176e5-6638-4b1f-a786-e41a817558bb/images/grouping-grid.png)

To manipulate the grouping area in code, use the **GroupedColumn** identifiers to access the collection of grouped columns. Like the **Columns** property, the [GroupedColumns](/componentone/docs/win/online-truedbgrid/) supports [Add](/componentone/docs/win/online-truedbgrid/), and [RemoveAt](/componentone/docs/win/online-truedbgrid/) methods. However, since the [GroupedColumns](/componentone/docs/win/online-truedbgrid/) serves as a placeholder for existing grid columns, the semantics of its [Add](/componentone/docs/win/online-truedbgrid/) and [RemoveAt](/componentone/docs/win/online-truedbgrid/) methods are different.

The Add method moves an existing column to the grouping area; it does not create a new column in the grid. Similarly, the RemoveAt method removes a column from the grouping area and returns it to its original position within the grid; it does not delete the column altogether.

Use the [GroupByCaption](/componentone/docs/win/online-truedbgrid/) property to add descriptive or directional text to the grouping area, which will be displayed when no columns are present there. Additionally, you can also set the position of the grouped row or column using [Position](/componentone/docs/win/online-truedbgrid/) property of the [GroupInfo](/componentone/docs/win/online-truedbgrid/) class which accepts its values from the [GroupPositionEnum](/componentone/docs/win/online-truedbgrid/) enumeration.

## Column Grouping

True DBGrid provides the [GroupIntervalEnum](/componentone/docs/win/online-truedbgrid/) enumeration that allows you to group data rows according to date, month, year, alphabet, date values (Outlook-style grouping), or you can customize how you would like to sort your data. The following topics explain how to group using a few of these settings.

>type=note
> **Note**: The default setting is [GroupIntervalEnum.Default](/componentone/docs/win/online-truedbgrid/), which groups rows by their values.

### Group Rows by Year

This topic demonstrates how to use the [GroupIntervalEnum.Year](/componentone/docs/win/online-truedbgrid/) member in [C1TrueDBGrid](/componentone/docs/win/online-truedbgrid/).

Complete the following steps:

1. Set the [DataView](/componentone/docs/win/online-truedbgrid/) property to [DataViewEnum.GroupBy](/componentone/docs/win/online-truedbgrid/).
2. **In the Designer**
<br>
    In the **C1TrueDBGrid Tasks** menu, select **GroupBy** from the **Data Layout** drop-down.
<br>
    **In Code**
<br>
    Add the following code to the **Form\_Load** event:

    ```vbnet
    Me.C1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.GroupBy
    ```

    ```csharp
    this.c1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.GroupBy;
    ```
3. Open the **C1TrueDBGrid Designer** by selecting **Designer** from the **C1TrueDBGrid Tasks** menu.
4. Select the *OrderDate* column by clicking on it in the right pane.
    The column can also be selected by choosing *OrderDate* from the drop-down list in the toolbar.
5. Set the [Interval](/componentone/docs/win/online-truedbgrid/) property to [GroupIntervalEnum.Year](/componentone/docs/win/online-truedbgrid/).
<br>
    **In the Designer**
<br>
    Locate the Interval property in the left pane of the **C1TrueDBGrid Designer** and set it to **Year**.
<br>
    **In Code**
<br>
    Add the following code to the **Form\_Load** event

    ```vbnet
    ' Set the GroupInfo.Interval of the HireDate column to Year.
    Me.C1TrueDBGrid1.Columns("HireDate").GroupInfo.Interval = C1.Win.C1TrueDBGrid.GroupIntervalEnum.Year
    ```

    ```csharp
    // Set the GroupInfo.Interval of the HireDate column to Year.
    this.c1TrueDBGrid1.Columns["OrderDate"].GroupInfo.Interval = C1.Win.C1TrueDBGrid.GroupIntervalEnum.Year;
    ```


6\. Finally\, to keep the *OrderDate* column visible after grouping by it, set the [ColumnVisible](/componentone/docs/win/online-truedbgrid/) property to **True**.

**In the Designer**

Locate the ColumnVisible property in the left pane of the **C1TrueDBGrid Designer** and set it to **True**.

**In Code**

Add the following code to the **Form\_Load** event:

```vbnet
' Keep the HireDate column visible while grouping.
Me.C1TrueDBGrid1.Columns("HireDate").GroupInfo.ColumnVisible = True
```

```csharp
// Keep the HireDate column visible while grouping.
this.c1TrueDBGrid1.Columns["OrderDate"].GroupInfo.ColumnVisible = true;
```

In this example, the OrderDate column is grouped by year.

### Group Rows by the First Character of the Value

This topic demonstrates how to use the [GroupIntervalEnum.Alphabetical](/componentone/docs/win/online-truedbgrid/) member in **C1TrueDBGrid**.

![](https://cdn.mescius.io/document-site-files/images/a1b176e5-6638-4b1f-a786-e41a817558bb/images/order-date-group.png)

Complete the following steps:

1. Set the [DataView](/componentone/docs/win/online-truedbgrid/) property to [DataViewEnum.GroupBy](/componentone/docs/win/online-truedbgrid/).
<br>
    **In the Designer**
<br>
    In the **C1TrueDBGrid Tasks** menu, select **GroupBy** from the **Data Layout** drop-down.
<br>
    **In Code**
<br>
    Add the following code to the **Form\_Load** event:

    ```vbnet
    Me.C1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.GroupBy
    ```

    ```csharp
    this.c1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.GroupBy;
    ```
2. Open the **C1TrueDBGrid Designer** by selecting **Designer** from the **C1TrueDBGrid Tasks** menu.
3. Select the *CustomerID* column by clicking on it in the right pane.
    The column can also be selected by choosing *CustomerID* from the drop-down list in the toolbar.
4. Set the [Interval](/componentone/docs/win/online-truedbgrid/) property to **Alphabetical**.
<br>
    **In the Designer**
<br>
    Locate the **Interval** property in the left pane of the **C1TrueDBGrid Designer** and set it to **Alphabetical**.
<br>
    **In Code**
<br>
    Add the following code to the **Form\_Load** event:

    ```vbnet
    ' Set the GroupInfo.Interval of the ProductName column to Alphabetical.
    Me.C1TrueDBGrid1.Columns("ProductName").GroupInfo.Interval = C1.Win.C1TrueDBGrid.GroupIntervalEnum.Alphabetical
    ```

    ```csharp
    // Set the GroupInfo.Interval of the ProductName column to Alphabetical.
    this.c1TrueDBGrid1.Columns["CustomerID"].GroupInfo.Interval = C1.Win.C1TrueDBGrid.GroupIntervalEnum.Alphabetical;
    ```
5. Finally, to keep the CustomerID column visible after grouping by it, set the [ColumnVisible](/componentone/docs/win/online-truedbgrid/) property to **True**.
<br>
    **In the Designer**
<br>
    Locate the ColumnVisible property in the left pane of the **C1TrueDBGrid Designer** and set it to **True**.
<br>
    **In Code**
<br>
    Add the following code to the **Form\_Load** event

    ```vbnet
    ' Keep the ProductName column visible while grouping.
    Me.C1TrueDBGrid1.Columns("ProductName").GroupInfo.ColumnVisible = True
    ```

    ```csharp
    // Keep the ProductName column visible while grouping.
    this.c1TrueDBGrid1.Columns["CustomerID"].GroupInfo.ColumnVisible = true;
    ```

In this example, the CustomerID column is grouped by the first letter of the product name.