# Summary Rows

You can use summary rows to show summary information about the displayed data, such as first item, last item, count, etc. The summary rows can be top and bottom pinned. You can use them in a flat **RadGridView**, or in groups and hierarchical views.

## Basic Example

To add summary rows to your application, start by initializing a new instance of **GridViewSummaryItem**.

The following example demonstrates the steps to create a top pinned summary row that shows the count of the **RadGridView** rows:

```C#
GridViewSummaryItem summaryItem = new GridViewSummaryItem();
summaryItem.Name = "ShipName";
summaryItem.Aggregate = GridAggregateFunction.Count;

GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem();
summaryRowItem.Add(summaryItem);

this.radGridView1.SummaryRowsTop.Add(summaryRowItem);
this.radGridView1.SummaryRowsBottom.Add(summaryRowItem);
```

```VB.NET
Dim summaryItem As New GridViewSummaryItem()
summaryItem.Name = "ShipName"
summaryItem.Aggregate = GridAggregateFunction.Count

Dim summaryRowItem As New GridViewSummaryRowItem()
summaryRowItem.Add(summaryItem)

Me.RadGridView1.SummaryRowsTop.Add(summaryRowItem)
Me.RadGridView1.SummaryRowsBottom.Add(summaryRowItem)
```

> caption Figure 1: Top pinned summary row

![Telerik UI for WinForms RadGridView showing a top pinned summary row with aggregate values](https://www.telerik.com/products/winforms/documentation/assets/48a6a773974d7e012e68253ac18bcd4f/gridview-rows-summary-rows001.png)

## SummaryItem properties

The following list represents the most used SummaryItem properties:

* **Name:** The name of the column for which you display summary information.

* **Aggregate:** The function that will be evaluated, for example, **Sum**, **Count**, **Max**, etc.

* **AggregateExpression:** The expression that will be evaluated. This property allows you to use advanced expressions instead of a simple **Aggregate** function.

* **FormatString:** The formatting string that is applied to the calculated value of the summary item.

## Predefined aggregates

You can use the following predefined aggregates for the **Aggregate** property:

* **None:** No expression is evaluated.

* **Sum:** Returns the sum of all values.

* **Min:** Returns the minimum value.

* **Max:** Returns the maximum value.

* **Last:** Returns the last value.

* **First:** Returns the first value.

* **Count:** Returns the count of all values.

* **Avg:** Returns the average of all values.

* **StDev:** Returns the standard deviation of all values.

* **Var:** Returns the sample variance of all values.

## Using the FormatString

The following example demonstrates how to add several summary items to a summary row and how to use the **FormatString**:

```C#
GridViewSummaryItem summaryItemShipName = new GridViewSummaryItem("ShipName", "{0}", GridAggregateFunction.Last);
GridViewSummaryItem summaryItemFreight = new GridViewSummaryItem("Freight", "Max Freight = {0}", GridAggregateFunction.Max);

GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem(
    new GridViewSummaryItem[] { summaryItemShipName, summaryItemFreight });

this.radGridView1.SummaryRowsTop.Add(summaryRowItem);
```

```VB.NET
Dim summaryItemShipName As New GridViewSummaryItem("ShipName", "{0}", GridAggregateFunction.Last)
Dim summaryItemFreight As New GridViewSummaryItem("Freight", "Max Freight = {0}", GridAggregateFunction.Max)

Dim summaryRowItem As New GridViewSummaryRowItem(New GridViewSummaryItem() {summaryItemShipName, summaryItemFreight})

Me.RadGridView1.SummaryRowsTop.Add(summaryRowItem)
```

> caption Figure 2: Format the Summary Item

![Telerik UI for WinForms RadGridView showing formatted summary items with custom display text](https://www.telerik.com/products/winforms/documentation/assets/e10176926fd93e4ff6e52e10ffc22fdd/gridview-rows-summary-rows002.png)

## Using an AggregateExpression

The next example demonstrates how to use **AggregateExpression** to calculate an average value by excluding the **Max** and **Min** values. Aggregate expressions can use the built-in aggregate functions listed in the [Functions Reference](https://www.telerik.com/products/winforms/documentation/controls/gridview/features/editing/expression-editor/functions-reference), including `STDEV` and `VAR`.

```C#
GridViewSummaryItem summaryItem = new GridViewSummaryItem();
summaryItem.Name = "Freight";
summaryItem.AggregateExpression = "(Sum(Freight) - Max(Freight) - Min(Freight)) / Count(Freight)";

GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem();
summaryRowItem.Add(summaryItem);
this.radGridView1.SummaryRowsTop.Add(summaryRowItem);
```

```VB.NET
Dim summaryItem As New GridViewSummaryItem()
summaryItem.Name = "Freight"
summaryItem.AggregateExpression = "(Sum(Freight) - Max(Freight) - Min(Freight)) / Count(Freight)"

Dim summaryRowItem As New GridViewSummaryRowItem()
summaryRowItem.Add(summaryItem)
Me.RadGridView1.SummaryRowsTop.Add(summaryRowItem)
```

> caption Figure 3: Using AggregateExpression

![Telerik UI for WinForms RadGridView showing a summary row calculated by using an AggregateExpression](https://www.telerik.com/products/winforms/documentation/assets/abb602404270791a123f846b300ed144/gridview-rows-summary-rows003.png)

## Summary rows in a grouped RadGridView

When you use grouping, SummaryItem calculates the aggregate values individually for each group and then displays the summary rows for each group. To show a total summary for all groups, you can use the **ShowTotals** template property. The totals are calculated for the child rows of all groups. You can control if the summary rows are top or bottom pinned even in grouped RadGridView.

As of the **R1 2020** version, the template in **RadGridView** offers the **ShowSubTotals** property. Its value indicates whether summary rows will be shown for each group. The property is only relevant when the grid is grouped.

The next example demonstrates how to use summary rows in grouping:

```C#
this.radGridView1.MasterTemplate.ShowTotals = true;

GridViewSummaryItem summaryItem = new GridViewSummaryItem("Freight", "{0}", GridAggregateFunction.Max);
GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem();
summaryRowItem.Add(summaryItem);

this.radGridView1.SummaryRowsTop.Add(summaryRowItem);
```

```VB.NET
Me.RadGridView1.MasterTemplate.ShowTotals = True

Dim summaryItem As New GridViewSummaryItem("Freight", "{0}", GridAggregateFunction.Max)
Dim summaryRowItem As New GridViewSummaryRowItem()
summaryRowItem.Add(summaryItem)

Me.RadGridView1.SummaryRowsTop.Add(summaryRowItem)
```

> caption Figure 4: Summary rows in a grouped RadGridView

![Telerik UI for WinForms RadGridView showing summary rows displayed for grouped data](https://www.telerik.com/products/winforms/documentation/assets/8821295329d0a79c0657d6cbc1271cd2/gridview-rows-summary-rows004.png)

As of the **2024 Q1** version, the template in **RadGridView** offers the **ShowCollapsedGroupSummaries** property. Its value indicates whether summary rows will be shown for each group when they are **collapsed**. The property is only relevant when the grid is grouped.

**Summary rows shown for collapsed groups**

![Telerik UI for WinForms RadGridView showing summary rows for collapsed grouped rows](https://www.telerik.com/products/winforms/documentation/assets/f9ce892535b37f786f3d82ea15fec846/gridview-rows-summary-rows010.png)

In a scenario in which we have nested grouping, the group summary row will appear for each nested group and its parent group when **ShowParentGroupSummaries** property is set to true. To show the group summary row on a specific group, we can set the same **ShowCollapsedGroupSummaries** property which was exposed on a group row level. It has higher priority than the **ShowCollapsedGroupSummaries** that comes from the template. The group row **ShowCollapsedGroupSummaries** property can be set in the CreateRowInfo event handler. However, an important moment here is that first the **ShowCollapsedGroupSummaries** property of the template needs to be set to true, then we can use the below example to remove the group summary row on a nested group.

```C#
private void RadGridView1_CreateRowInfo(object sender, GridViewCreateRowInfoEventArgs e)
{
    if (e.RowInfo is GridViewGroupRowInfo)
    {
        GridViewGroupRowInfo groupRowInfo = e.RowInfo as GridViewGroupRowInfo;
        groupRowInfo.ShowCollapsedGroupSummaries = groupRowInfo.GroupLevel == 0;
    }
}
```

```VB.NET
Private Sub RadGridView1_CreateRowInfo(ByVal sender As Object, ByVal e As GridViewCreateRowInfoEventArgs)
    If TypeOf e.RowInfo Is GridViewGroupRowInfo Then
        Dim groupRowInfo As GridViewGroupRowInfo = TryCast(e.RowInfo, GridViewGroupRowInfo)
        groupRowInfo.ShowCollapsedGroupSummaries = groupRowInfo.GroupLevel = 0
    End If
End Sub
```

**Nested grouped rows with selective collapsed group summaries**

![Telerik UI for WinForms RadGridView showing nested groups with selective collapsed group summaries](https://www.telerik.com/products/winforms/documentation/assets/6168d9491ffb9868dca3f46442844f1b/gridview-rows-summary-rows011.png)

## Summary rows in a hierarchical RadGridView

You can add summary rows in hierarchical views. They are calculated for the child rows in the current view.\
The following example demonstrates how to add a summary row to the first level of a **RadGridView** hierarchy:

```C#
GridViewSummaryItem summaryItem = new GridViewSummaryItem("Quantity", "Max quantity = {0}", GridAggregateFunction.Max);
GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem();
summaryRowItem.Add(summaryItem);

this.radGridView1.MasterTemplate.Templates[0].SummaryRowsTop.Add(summaryRowItem);
```

```VB.NET
Dim summaryItem As New GridViewSummaryItem("Quantity", "Max quantity = {0}", GridAggregateFunction.Max)
Dim summaryRowItem As New GridViewSummaryRowItem()
summaryRowItem.Add(summaryItem)

Me.RadGridView1.MasterTemplate.Templates(0).SummaryRowsTop.Add(summaryRowItem)
```

## Customizing the summary row

The **GroupSummaryEvaluate** event is fired after a summary item is calculated and before the summary row is displayed. You can use this event to apply custom formatting to the summary row. The same event is used to format the group header, so it is important to check the **Parent** in the event arguments, as shown in the example:

```C#


private void radGridView1_GroupSummaryEvaluate(object sender, GroupSummaryEvaluationEventArgs e)
{
    if (e.SummaryItem.Name == "ShipName")
    {
        e.FormatString = String.Format("There are {0} ships total.", e.Value);
    }
}
```

```VB.NET
Private Sub radGridView1_GroupSummaryEvaluate(ByVal sender As Object, ByVal e As GroupSummaryEvaluationEventArgs)
    If e.SummaryItem.Name = "ShipName" Then
        e.FormatString = String.Format("There are {0} ships total.", e.Value)
    End If
End Sub

```

> caption Figure 5: Customizing the summary row

![Telerik UI for WinForms RadGridView showing a summary row with custom formatting applied](https://www.telerik.com/products/winforms/documentation/assets/be3006076e4578e15bde448f420650ae/gridview-rows-summary-rows006.png)

## Extending the SummaryItem

You can write your own logic for summary items evaluation by inheriting the **GridViewSummaryItem** class and overriding the **Evaluate** method.

The following example demonstrates how to create a custom summary item that calculates the orders count with *Freight* less than *50*:

```C#
void CustomSummaryItemUsage()
{
    CustomSummaryItem summaryItem = new CustomSummaryItem("Freight", "The low freight orders are {0}.", GridAggregateFunction.Count);
    GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem();
    summaryRowItem.Add(summaryItem);
    this.radGridView1.SummaryRowsTop.Add(summaryRowItem);
}

public class CustomSummaryItem : GridViewSummaryItem
{
    public CustomSummaryItem(string name, string formatString, GridAggregateFunction aggregate)
        : base(name, formatString, aggregate)
    { }

    public override object Evaluate(IHierarchicalRow row)
    {
        int lowFreightsCount = 0;
        foreach (GridViewRowInfo childRow in row.ChildRows)
        {
            if ((decimal)childRow.Cells["Freight"].Value < 50m)
            {
                lowFreightsCount++;
            }
        }
        return lowFreightsCount;
    }
}
```

```VB.NET
Private Sub CustomSummaryItemUsage()

    Dim summaryItem As New CustomSummaryItem("Freight", "The low freight orders are {0}.", GridAggregateFunction.Count)
    Dim summaryRowItem As New GridViewSummaryRowItem()
    summaryRowItem.Add(summaryItem)
    Me.RadGridView1.SummaryRowsTop.Add(summaryRowItem)
End Sub

Public Class CustomSummaryItem
    Inherits GridViewSummaryItem
    Public Sub New(ByVal name As String, ByVal formatString As String, ByVal aggregate As GridAggregateFunction)
        MyBase.New(name, formatString, aggregate)
    End Sub

    Public Overrides Function Evaluate(ByVal row As IHierarchicalRow) As Object
        Dim lowFreightsCount As Integer = 0
        For Each childRow As GridViewRowInfo In row.ChildRows
            If CDec(childRow.Cells("Freight").Value) < 50D Then
                lowFreightsCount += 1
            End If
        Next childRow
        Return lowFreightsCount
    End Function
End Class
```

![Telerik UI for WinForms RadGridView showing a custom summary item that counts orders with Freight values below 50](https://www.telerik.com/products/winforms/documentation/assets/0cd1ac6efff6c3f0d9bb6d7fa46cb8da/gridview-rows-summary-rows007.png)

## BottomPinnedRowsMode

You can control the location of the bottom summary rows through the **BottomPinnedRowsMode** property. The available options are:

* *Float*: The bottom pinned rows will stay below the scrollable rows. If there are not enough rows to fill the viewport, there will be no gap between the scrollable and pinned rows.
* *Fixed*: The bottom pinned rows will remain at the bottom of the viewport no matter how many scrollable rows there are in the viewport.

**Compare the fixed and floating BottomPinnedRowsMode layouts**

| GridViewBottomPinnedRowsMode.Fixed                                                                                                                                                                                                               | GridViewBottomPinnedRowsMode.Float                                                                                                                                                                                                                       |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ![Telerik UI for WinForms RadGridView showing bottom pinned summary rows fixed at the bottom of the viewport](https://www.telerik.com/products/winforms/documentation/assets/35679e6ca20d238a8177743e380b1bdb/gridview-rows-summary-rows008.png) | ![Telerik UI for WinForms RadGridView showing bottom pinned summary rows floating directly below the scrollable rows](https://www.telerik.com/products/winforms/documentation/assets/82600d8c82175d7e5419f1e3ddd8a090/gridview-rows-summary-rows009.png) |

> note When you use the *Fixed* mode, it is necessary to pin the summary row accessing them by the MasterView\.SummaryRows collection. Make sure to set the **IsPinned** property before setting the **PinPosition**.

## See Also

* [Adding and Inserting Rows](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/rows/adding-and-inserting-rows)

* [Conditional Formatting Rows](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/rows/conditional-formatting-rows)

* [Creating custom rows](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/rows/creating-custom-rows)

* [Drag and Drop](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/rows/drag-and-drop)

* [Formatting Rows](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/rows/formatting-rows)

* [GridViewRowInfo](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/rows/gridviewrowinfo)

* [Iterating Rows](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/rows/iterating-rows)

* [New Row](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/rows/new-row)

* [Show Summary Values for Collapsed Groups in RadGridView ](https://www.telerik.com/products/winforms/documentation/knowledge-base/group-rows-with-summary-values)
