Category / Section
How to have summary value in GridDataBoundGrid?
1 min read
By default, the GridDataBoundGrid doesn’t have support for Summary row. The summary row can be added to grid by using GridSummaryRow helper class. Using this class you can use predefined summary type or create a custom summary calculation also.
C#
//create a summary row... sumRow = new GridSummaryRow(); //define the summary type for each column object[] summaryTypes = new object[this.gridDataBoundGrid1.Model.ColCount]; for (int i = 0; i < this.gridDataBoundGrid1.Model.ColCount; i++) { if (i == 2 || i == 3 || i == 5) //sum these { summaryTypes[i] = GridSummaryRow.SummaryType.Sum; } else if (i == 6) //do max { summaryTypes[i] = new GridSummaryRow.CustomSummaryCalculation(calc_Max); } else if (i == 7) //do min { summaryTypes[i] = new GridSummaryRow.CustomSummaryCalculation(calc_Min); } else //no summary { summaryTypes[i] = GridSummaryRow.SummaryType.None; } } this.gridDataBoundGrid1.Binder.InternalColumns(0).StyleInfo.BackColor = SystemColors.Control; this.gridDataBoundGrid1.Binder.InternalColumns(2).StyleInfo.BackColor = SystemColors.Control; this.gridDataBoundGrid1.Binder.InternalColumns(5).StyleInfo.BackColor = SystemColors.Control; //two ways to wire the summary row //1) use default table style //sumRow.WireSummaryRow(this.gridDataBoundGrid1, summaryTypes); //2) or set each column style somehow.... GridStyleInfo[] styles = new GridStyleInfo[this.gridDataBoundGrid1.Model.ColCount]; for (int i = 0; i < this.gridDataBoundGrid1.Model.ColCount; i++) { styles[i] = new GridStyleInfo((GridStyleInfoStore)(this.gridDataBoundGrid1.Binder.InternalColumns(i).StyleInfo.Store.Clone())); styles[i].Font.Bold = true; } styles[0].Text = "Total"; sumRow.WireSummaryRow(this.gridDataBoundGrid1, summaryTypes, styles);
VB
'create a summary row... sumRow = New GridSummaryRow() 'define the summary type for each column Dim summaryTypes(Me.gridDataBoundGrid1.Model.ColCount - 1) As Object For i As Integer = 0 To Me.gridDataBoundGrid1.Model.ColCount - 1 If i= 2 OrElse i = 3 OrElse i = 5 Then 'sum these summaryTypes(i) = GridSummaryRow.SummaryType.Sum ElseIf i = 6 Then 'do max summaryTypes(i) = New GridSummaryRow.CustomSummaryCalculation(AddressOf calc_Max) ElseIf i = 7 Then 'do min summaryTypes(i) = New GridSummaryRow.CustomSummaryCalculation(AddressOf calc_Min) Else 'no summary summaryTypes(i) = GridSummaryRow.SummaryType.None End If Next i Me.gridDataBoundGrid1.Binder.InternalColumns(0).StyleInfo.BackColor = SystemColors.Control Me.gridDataBoundGrid1.Binder.InternalColumns(2).StyleInfo.BackColor = SystemColors.Control Me.gridDataBoundGrid1.Binder.InternalColumns(5).StyleInfo.BackColor = SystemColors.Control 'two ways to wire the summary row 1) use default table style 'sumRow.WireSummaryRow(this.gridDataBoundGrid1, summaryTypes); '2) or set each column style somehow.... Dim styles(Me.gridDataBoundGrid1.Model.ColCount - 1) As GridStyleInfo For i As Integer = 0 To Me.gridDataBoundGrid1.Model.ColCount - 1 styles(i) = New GridStyleInfo(CType(Me.gridDataBoundGrid1.Binder.InternalColumns(i).StyleInfo.Store.Clone(), GridStyleInfoStore)) styles(i).Font.Bold = True Next i styles(0).Text = "Total" sumRow.WireSummaryRow(Me.gridDataBoundGrid1, summaryTypes, styles) 'this.gridDataBoundGrid1.EnableAddNew = false; 'padd the grid's client area with empty rows Me.gridDataBoundGrid1.Model.Options.DisplayEmptyRows = True
Screenshot
Sample Links
C#: AddSummary_CS
VB: AddSummary_VB