Category / Section
How to freeze the summary row at bottom of the grid in WinForms GridGroupingControl?
6 mins read
Summaries
By default,
the WinForms GridGroupingControl does not have support to freeze a summary row. To
achieve this, place a single row at the bottom of the grid and set the
calculated summary values to that grid cell which acts as a summary row.
GridControl Customization
C#
public class EngineSummaryGrid
{
private GridControl grid;
private GridGroupingControl parentGrid;
public void WiredSummary(GridGroupingControl ggc)
{
parentGrid = ggc;
parentGrid.TopLevelGroupOptions.ShowSummaries = false;
parentGrid.TableModel.QueryColWidth += TableModel_QueryColWidth;
parentGrid.TableControl.Layout += TableControl_Layout;
//Initializing the grid with single row.
grid = new GridControl();
grid.ThemesEnabled = true;
grid.Model.Options.NumberedRowHeaders = false;
grid.Model.RowCount = 1;
this.grid.Model.ColCount = this.parentGrid.TableDescriptor.Columns.Count;
grid.Properties.ColHeaders = false;
ggc.TableControl.Controls.Add(grid);
SetSummary(ggc);
}
private void TableModel_QueryColWidth(object sender, GridRowColSizeEventArgs e)
{
this.grid.Model.ColWidths[e.Index] = e.Size;
}
private void TableControl_Layout(object sender, LayoutEventArgs e)
{
SummaryRowSize();
}
private void SummaryRowSize()
{
parentGrid.TableControl.ResetGridBounds();
this.parentGrid.TableControl.ResetGridBounds();
this.parentGrid.TableControl.GridBounds = new Rectangle(this.parentGrid.TableControl.GridBounds.X,
this.parentGrid.TableControl.GridBounds.Y, this.parentGrid.TableControl.GridBounds.Width,
this.parentGrid.TableControl.GridBounds.Height - (this.parentGrid.TableControl.DefaultRowHeight));
int height = this.parentGrid.TableControl.GridBounds.Height + this.parentGrid.TableControl.DefaultRowHeight;
this.grid.Location = new Point(0, height - this.parentGrid.TableControl.DefaultRowHeight);
this.grid.Size = new Size(this.parentGrid.TableControl.ClientSize.Width, this.parentGrid.TableControl.DefaultRowHeight
}
}
VB
Public Class EngineSummaryGrid
Private grid As GridControl
Private parentGrid As GridGroupingControl
Public Sub WiredSummary(ByVal ggc As GridGroupingControl)
parentGrid = ggc
parentGrid.TopLevelGroupOptions.ShowSummaries = False
AddHandler parentGrid.TableModel.QueryColWidth, AddressOf TableModel_QueryColWidth
AddHandler parentGrid.TableControl.Layout, AddressOf TableControl_Layout
'Initializing the grid with single row.
grid = New GridControl()
grid.ThemesEnabled = True
grid.Model.Options.NumberedRowHeaders = False
grid.Model.RowCount = 1
Me.grid.Model.ColCount = Me.parentGrid.TableDescriptor.Columns.Count
grid.Properties.ColHeaders = False
ggc.TableControl.Controls.Add(grid)
SetSummary(ggc)
End Sub
Private Sub TableModel_QueryColWidth(ByVal sender As Object, ByVal e As GridRowColSizeEventArgs)
Me.grid.Model.ColWidths(e.Index) = e.Size
End Sub
Private Sub TableControl_Layout(ByVal sender As Object, ByVal e As LayoutEventArgs)
SummaryRowSize()
End Sub
Private Sub SummaryRowSize()
parentGrid.TableControl.ResetGridBounds()
Me.parentGrid.TableControl.ResetGridBounds()
Me.parentGrid.TableControl.GridBounds = New Rectangle(Me.parentGrid.TableControl.GridBounds.X,
Me.parentGrid.TableControl.GridBounds.Y,
Me.parentGrid.TableControl.GridBounds.Width,
Me.parentGrid.TableControl.GridBounds.Height - (Me.parentGrid.TableControl.DefaultRowHeight))
Dim height As Integer = Me.parentGrid.TableControl.GridBounds.Height + Me.parentGrid.TableControl.DefaultRowHeight
Me.grid.Location = New Point(0, height - Me.parentGrid.TableControl.DefaultRowHeight)
Me.grid.Size = New Size(Me.parentGrid.TableControl.ClientSize.Width, Me.parentGrid.TableControl.DefaultRowHeight)
End Sub
End Class
Setting
the Summary value to GridControl
C#
private void SetSummary(GridGroupingControl ggc)
{
for (int j = 0; j < grid.Model.ColCount; j++)
{
GridSummaryRowDescriptor row = ggc.TableDescriptor.SummaryRows[0];
GridSummaryColumnDescriptor scd = row.GetSummaryColumnAtCol(j);
if (scd != null)
{
string text = scd.GetDisplayText(ggc.Engine.Table.DisplayElements[0].ParentGroup);
grid.Model[1, j].CellValue = row.Title;
grid.Model[1, j + 1].CellValue = text;
grid.Model[1, j + 1].Enabled = false;
}
}
}
VB
Private Sub SetSummary(ByVal ggc As GridGroupingControl)
For j As Integer = 0 To grid.Model.ColCount - 1
Dim row As GridSummaryRowDescriptor = ggc.TableDescriptor.SummaryRows(0)
Dim scd As GridSummaryColumnDescriptor = row.GetSummaryColumnAtCol(j)
If scd IsNot Nothing Then
Dim text As String = scd.GetDisplayText(ggc.Engine.Table.DisplayElements(0).ParentGroup)
grid.Model(1, j).CellValue = row.Title
grid.Model(1, j + 1).CellValue = text
grid.Model(1, j + 1).Enabled = False
End If
Next j
End Sub
Adding a customized grid to GridGroupingControl
C#
//To add the summary row grid to GridGrouping control
SummaryGrid = new EngineSummaryGrid();
SummaryGrid.WiredSummary(this.gridGroupingControl1);
VB
'To add the summary row grid to GridGrouping control
SummaryGrid = New EngineSummaryGrid()
SummaryGrid.WiredSummary(Me.gridGroupingControl1)
The frozen summary row is displayed in the screenshot below
Note:
By this solution, the summary values will not be updated when modifying records in the grid.
Samples:
Reference Link: Summaries