Easily Calculate Summary of Selected Rows with WinForms DataGrid
Tags: csharp, dotnet, productivity
Published: 2020-03-27 11:22:56

---
title: Easily Calculate Summary of Selected Rows with WinForms DataGrid
published: true
date: 2020-03-26 11:30:07 UTC
tags: csharp, dotnet, productivity
canonical_url: https://www.syncfusion.com/blogs/post/easily-calculate-summary-of-selected-rows-with-winforms-datagrid.aspx
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/qnpcpbisw55femtau3lo.jpg
---


We are happy to announce that in our [Essential Studio 2020 Volume 1 beta](https://www.syncfusion.com/forums/152560/essential-studio-2020-volume-1-beta-release-v18-1-0-36-is-available-for-download) release, we added summary calculation for selected rows in the [WinForms DataGrid](https://www.syncfusion.com/winforms-ui-controls/datagrid) control. This blog provides a walk-through of how to calculate summaries for selected rows and how to use the available options.

The DataGrid allows users to calculate summaries for:

- Selected rows.
- All rows.
- All rows until rows are selected (mixed rows).

The [SummaryCalculationUnit](https://help.syncfusion.com/cr/windowsforms/Syncfusion.Data.WinForms~Syncfusion.Data.SummaryCalculationUnit.html) enumeration is used to perform these operations.

To calculate a summary of selected rows in the DataGrid, set the [SfDataGrid.SummaryCalculationUnit](https://help.syncfusion.com/cr/windowsforms/Syncfusion.SfDataGrid.WinForms~Syncfusion.WinForms.DataGrid.SfDataGrid~SummaryCalculationUnit.html) property to **SelectedRows**.

In the following code example, a summary is calculated for group caption summary rows. This summary calculation support is available for both group and table summary rows too.

```csharp
this.sfDataGrid.SummaryCalculationUnit = Syncfusion.Data.SummaryCalculationUnit.SelectedRows;
this.sfDataGrid.SelectionMode = GridSelectionMode.Multiple;
this.sfDataGrid.AutoGenerateColumns = true;
SalesInfoCollection sales = new SalesInfoCollection();
sfDataGrid.DataSource = sales.YearlySalesDetails;
```

![Group Caption Summary Rows in DataGrid](https://www.syncfusion.com/blogs/wp-content/uploads/2020/03/Group-Caption-Summary-Rows-in-DataGrid.gif)<figcaption>Group Caption Summary Rows in DataGrid</figcaption>

A summary row can also be considered while calculating the summary.

```csharp
this.sfDataGrid.CaptionSummaryRow = new GridSummaryRow()
{
    Name = "CaptionSummary",
    ShowSummaryInRow = false,
    CalculationUnit= Syncfusion.Data.SummaryCalculationUnit.SelectedRows,
    Title="Sales details in {ColumnName} : {Key}", 
    TitleColumnCount=1,
    SummaryColumns = new System.Collections.ObjectModel.ObservableCollection()
    {
        new GridSummaryColumn()
        {
            Name = "SQ1",
            SummaryType = Syncfusion.Data.SummaryType.DoubleAggregate,
            Format="{Sum:c}",
            MappingName="Q1",
        },
        new GridSummaryColumn()
        {
            Name = "SQ2",
            SummaryType = Syncfusion.Data.SummaryType.DoubleAggregate,
            Format="{Sum:c}",
            MappingName="Q2",
        },
        new GridSummaryColumn()
        {
            Name = "SQ3",
            SummaryType = Syncfusion.Data.SummaryType.DoubleAggregate,
            Format="{Sum:c}",
            MappingName="Q3",
        },
        new GridSummaryColumn()
        {
            Name = "SQ4",
            SummaryType = Syncfusion.Data.SummaryType.DoubleAggregate,
            Format="{Sum:c}",
            MappingName="Q4",
        },
         new GridSummaryColumn()
        {
            Name = "SQ5",
            SummaryType = Syncfusion.Data.SummaryType.DoubleAggregate,
            Format="{Sum:c}",
            MappingName="Total",
        }
    }
};
```

Here, let’s use the value **Mixed** for the property **SummaryCalculationUnit** of the DataGrid to calculate a summary of the selected rows; otherwise, the summary for all rows will be calculated.

```csharp
this.sfDataGrid.SummaryCalculationUnit = Syncfusion.Data.SummaryCalculationUnit.Mixed;
this.sfDataGrid.SelectionMode = GridSelectionMode.Multiple;
this.sfDataGrid.AutoGenerateColumns = true;
SalesInfoCollection sales = new SalesInfoCollection();
sfDataGrid.DataSource = sales.YearlySalesDetails;
```

![Calculated Summary for Mixed Rows](https://www.syncfusion.com/blogs/wp-content/uploads/2020/03/Calculated-Summary-for-Mixed-Rows.gif)<figcaption>Calculated Summary for Mixed Rows</figcaption>

I hope you now understand this feature and how to calculate summaries for selected rows in the WinForms DataGrid. You can download a demo of calculating summaries for selected rows from our [GitHub samples](https://github.com/syncfusion/winforms-demos/tree/master/DataGrid.WinForms/Samples/Summaries/Summaries). To learn more about the summaries in the Syncfusion [DataGrid](https://www.syncfusion.com/winforms-ui-controls/datagrid), please refer to our [documentation](https://help.syncfusion.com/windowsforms/datagrid/summaries). You can download our [2020 Volume 1 beta](https://www.syncfusion.com/forums/152560/essential-studio-2020-volume-1-beta-release-v18-1-0-36-is-available-for-download) release to check out all our new features and controls.

If you have any questions or require clarifications about this control, please let us know in the comments section. You can also contact us through our [support forum](https://www.syncfusion.com/forums), [Direct-Trac](https://www.syncfusion.com/support/directtrac/), or [feedback portal](https://www.syncfusion.com/feedback). We are happy to assist you!

The post [Easily Calculate Summary of Selected Rows with WinForms DataGrid](https://www.syncfusion.com/blogs/post/easily-calculate-summary-of-selected-rows-with-winforms-datagrid.aspx) appeared first on [Syncfusion Blogs](https://www.syncfusion.com/blogs).