Sunday, September 20, 2009

Infragistics - Custom Grid Summary

I am currently working on an application for my company.  It contains a rather complicated grid that shows items and transactions.  I was asked to added multiple custom summaries to the grid under the quantity column.

Infragistics allows you to use Custom Summary Calculators to basically perform any type of formula.  In this case I needed a Quantity * Price and Quantity * Weight.

Below is an example of one of the classes I made along with the code needed to add it to the grid.


This is the class code.

Imports Infragistics.Shared
Imports Infragistics.Win
Imports Infragistics.Win.UltraWinGrid

Public Class clsTransItemWeightSummary
   Implements ICustomSummaryCalculator

   Private nTotal As Double = 0

   Public Sub AggregateCustomSummary(ByVal summarySettings As Infragistics.Win.UltraWinGrid.SummarySettings, ByVal row As Infragistics.Win.UltraWinGrid.UltraGridRow) Implements Infragistics.Win.UltraWinGrid.ICustomSummaryCalculator.AggregateCustomSummary
      ' set variables equal to the columns that will be used.
      Dim cItemWeight As Object = row.GetCellValue(summarySettings.SourceColumn.Band.Columns("nWeight"))
      Dim cIQuantity As Object = row.GetCellValue(summarySettings.SourceColumn)

      ' Handle null values
      If cItemWeight Is DBNull.Value Or cIQuantity Is DBNull.Value Then
         Return
      End If

      Try
         Dim nItemWeight As Decimal = Convert.ToDecimal(cItemWeight)
         Dim nQuantity As Decimal = Convert.ToDecimal(cIQuantity)

         Me.nTotal += nQuantity * nItemWeight
      Catch e As Exception
         ' This should not happen if the columns are numeric.
         Debug.Assert(False, "Exception thrown while trying to convert cell's value to decimal !")
      End Try

   End Sub

   Public Sub BeginCustomSummary(ByVal summarySettings As Infragistics.Win.UltraWinGrid.SummarySettings, ByVal rows As Infragistics.Win.UltraWinGrid.RowsCollection) Implements Infragistics.Win.UltraWinGrid.ICustomSummaryCalculator.BeginCustomSummary
      nTotal = 0
   End Sub

   Public Function EndCustomSummary(ByVal summarySettings As Infragistics.Win.UltraWinGrid.SummarySettings, ByVal rows As Infragistics.Win.UltraWinGrid.RowsCollection) As Object Implements Infragistics.Win.UltraWinGrid.ICustomSummaryCalculator.EndCustomSummary
      Return nTotal
   End Function
End Class

Add the following code to the InitializeLayout event
e.Layout.Bands(0).Summaries.Add(String.Format("EXTWGT_{0}", sColumnName), SummaryType.Custom, New clsTransItemWeightSummary, .Columns(sColumnName), SummaryPosition.UseSummaryPositionColumn, Nothing)

No comments:

Post a Comment