Edit this page

Summary Rows

Summary rows are RadGridView's rows which allow you to display information about the data in the control such as first item, last item, count of items, etc. The summary rows are top and bottom pinned rows. They can be used in a flat RadGridView or in groups and hierarchical views.

Basic Example

The following example demonstrates how to create a top pinned summary row, showing the count of the RadGridView rows:

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);

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)

Figure 1: Top summary row

gridview-rows-summary-rows 001

SummaryItem properties

Summary item's major properties are:

  • Name: The name of the column which the summary item refers to.

  • Aggregate: The function which will be evaluated.

  • AggregateExpression: The final expression to evaluate.

  • FormatString: Formatting string which is applied to the calculated value of the summary item.

Predefined aggregates

The predefined aggregates are:

  • 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 in a summary row and how the FormatString is used:

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);

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)

Figure 2: Format the summary item

gridview-rows-summary-rows 002

Using an AggregateExpression

The next example demonstrates how to use AggregateExpression to calculate an average value excluding the Max and Min values:

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);

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)

Figure 3: Using AggregateExpression

gridview-rows-summary-rows 003

Summary rows in a grouped RadGridView

When you use grouping, the summary rows are calculated for each group's child rows and displayed as top or bottom pinned rows in each group. Using the template's property ShowTotals you can define if total summary rows will be shown. They are calculated for the child rows of all groups and pinned at the top or bottom of all groups.

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

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);

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)

Figure 4: Summary rows in a grouped RadGridView

gridview-rows-summary-rows 004

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 RadGridView hierarchy:

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);

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 it to apply custom formatting to the summary row:

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

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

Figure 5: Customizing the summary row

gridview-rows-summary-rows 006

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 above example.

Extending the GridViewSummaryItem

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 which calculates the orders count with Freight less than 50:

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;
    }
}

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

gridview-rows-summary-rows 007

BottomPinnedRowsMode

You can control the location of the bottom summary rows by 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.
GridViewBottomPinnedRowsMode.Fixed GridViewBottomPinnedRowsMode.Float
gridview-rows-summary-rows 008 gridview-rows-summary-rows 009
note

When you use the Fixed mode, it is necessary to pin the summary row accessing them by the MasterView.SummaryRows collection.

See Also