/// <summary>
 /// Removes the summary rows that match the specified <paramref name="summaryPosition"/> and grouped by
 /// <paramref name="groupFromCol"/> and <paramref name="groupToCol"/>.
 /// </summary>
 /// <param name="grid">Target <see cref="DataGridView"/>.</param>
 /// <param name="summaryPosition">Position of the summary rows to remove.</param>
 /// <param name="groupFromCol">First column that determnines the group break values.</param>
 /// <param name="groupToCol">Last column that determines the group break values.</param>
 public static void RemoveSummaryRows(this DataGridView grid, SummaryRowPosition summaryPosition, DataGridViewColumn groupFromCol, DataGridViewColumn groupToCol)
 {
     lock (grid.Rows)
     {
         // clear existing aggregates.
         DataGridViewSummaryRow summaryRow = null;
         while ((summaryRow = RetrieveSummaryRow(grid, null, summaryPosition, groupFromCol, groupToCol)) != null)
         {
             grid.Rows.Remove(summaryRow);
         }
     }
 }
        private static DataGridViewSummaryRow CreateSummaryRow(
            DataGridView grid,
            DataGridViewRow[] group,
            SummaryType summaryType,
            SummaryRowPosition summaryPosition,
            DataGridViewColumn groupFromCol, DataGridViewColumn groupToCol,
            DataGridViewColumn summaryCol,
            DataGridViewCellStyle style)
        {
            if (group.Length > 0)
            {
                // create and add the summary row.
                var summaryRow = new DataGridViewSummaryRow(
                    group,
                    groupFromCol,
                    groupToCol,
                    summaryPosition);

                summaryRow.ReadOnly = true;

                if (style != null)
                {
                    summaryRow.DefaultCellStyle = style;
                }

                switch (summaryPosition)
                {
                case SummaryRowPosition.Above:
                    grid.Rows.Insert(FindInsertIndex(grid, group, summaryPosition), summaryRow);
                    break;

                case SummaryRowPosition.Parent:
                    grid.Rows.Insert(FindInsertIndex(grid, group, summaryPosition), summaryRow);
                    foreach (var r in group)
                    {
                        r.ParentRow = summaryRow;
                    }
                    break;

                case SummaryRowPosition.Below:
                default:
                    grid.Rows.Insert(FindInsertIndex(grid, group, summaryPosition), summaryRow);
                    break;
                }

                DisplayGroupValues(summaryRow);

                return(summaryRow);
            }

            return(null);
        }
 /// <summary>
 /// Removes the summary rows grouped by
 /// <paramref name="groupFromCol"/> and <paramref name="groupToCol"/>.
 /// </summary>
 /// <param name="grid">Target <see cref="DataGridView"/>.</param>
 /// <param name="groupFromCol">First column that determnines the group break values.</param>
 /// <param name="groupToCol">Last column that determines the group break values.</param>
 public static void RemoveSummaryRows(this DataGridView grid, DataGridViewColumn groupFromCol, DataGridViewColumn groupToCol)
 {
     lock (grid.Rows)
     {
         // clear existing aggregates.
         foreach (SummaryRowPosition position in Enum.GetValues(typeof(SummaryRowPosition)))
         {
             DataGridViewSummaryRow summaryRow = null;
             while ((summaryRow = RetrieveSummaryRow(grid, null, position, groupFromCol, groupToCol)) != null)
             {
                 grid.Rows.Remove(summaryRow);
             }
         }
     }
 }
        private static void CalculateSummary(DataGridViewSummaryRow summaryRow, SummaryType summaryType, DataGridViewRow[] group, DataGridViewColumn summaryCol)
        {
            object value = null;

            if (group.Length > 0)
            {
                switch (summaryType)
                {
                case SummaryType.None:
                    value = null;
                    break;

                case SummaryType.Sum:
                    value = CalculateSummarySum(group, summaryCol);
                    break;

                case SummaryType.Min:
                    value = CalculateSummaryMin(group, summaryCol);
                    break;

                case SummaryType.Max:
                    value = CalculateSummaryMax(group, summaryCol);
                    break;

                case SummaryType.Count:
                    value = CalculateSummaryCount(group, summaryCol);
                    break;

                case SummaryType.Average:
                    value = CalculateSummaryAverage(group, summaryCol);
                    break;

                case SummaryType.Std:
                    value = CalculateSummaryStd(group, summaryCol);
                    break;
                }
            }

            if (summaryCol != null)
            {
                summaryRow[summaryCol].Value = value;
            }
        }
        private static void DisplayGroupValues(DataGridViewSummaryRow row)
        {
            // copy the group values.
            if (row.GroupRows.Length > 0)
            {
                var groupRow     = row.GroupRows[0];
                int colFromIndex = row.GroupFromColumn?.Index ?? 0;
                int colToIndex   = row.GroupToColumn?.Index ?? row.CellCount - 1;

                for (var c = colFromIndex; c <= colToIndex; c++)
                {
                    if (c >= row.CellCount)
                    {
                        break;
                    }
                    if (c >= groupRow.CellCount)
                    {
                        break;
                    }

                    row[c].Value = groupRow[c].Value;
                }
            }
        }