Protected Function AggregatedTable(ByVal TableToAggregate As Data.DataTable, ByVal SortFieldName As String, ByVal FieldsToAggregate() As String) As Data.DataTable
Dim ReturnTable As Data.DataTable = New Data.DataView(TableToAggregate, Nothing, SortFieldName, Data.DataViewRowState.CurrentRows).ToTable
Dim RowNumberColumn As New Data.DataColumn
RowNumberColumn.ColumnName = "Row~Number"
RowNumberColumn.DataType = GetType(Integer)
ReturnTable.Columns.Add(RowNumberColumn)
Dim RowsToAddList As New Generic.List(Of Data.DataRow)
Do
Dim NewRow As Data.DataRow = ReturnTable.NewRow
ReturnTable.Rows.Add(NewRow)
Exit Do
Loop
Dim LastMatch As String = "~"
Dim LastRowNumber As New Integer
For Each ReturnRow As Data.DataRow In ReturnTable.Rows
ReturnRow.Item("Row~Number") = ReturnTable.Rows.IndexOf(ReturnRow) * 2
LastRowNumber = ReturnRow.Item("Row~Number")
If LastMatch <> "~" Then
If LastMatch <> ReturnRow.Item(SortFieldName).ToString Then
Dim NewRow As Data.DataRow = ReturnTable.NewRow
NewRow.Item("Row~Number") = ReturnRow.Item("Row~Number") - 1
NewRow.Item("RowFont") = "Bold"
If ReturnTable.Compute("Sum(Grand_Inv_Total)", String.Format("(Exported = 'True') AND (Salesman1 = '{0}')", Server.HtmlEncode(LastMatch))) IsNot DBNull.Value Then
Dim Exported As Decimal = ReturnTable.Compute("Sum(Grand_Inv_Total)", String.Format("(Exported = 'True') AND (Salesman1 = '{0}')", LastMatch))
Dim ExportedPercent As Decimal = Exported / ReturnTable.Compute("Sum(Grand_Inv_Total)", String.Format("(Salesman1 = '{0}')", LastMatch))
NewRow.Item("Cust_Name") = "Exported: " & Exported.ToString("c") & StrDup(3, ChrW(160)) & ExportedPercent.ToString("p")
End If
NewRow.Item(SortFieldName) = LastMatch
For Each AggregateField In FieldsToAggregate
Dim FormatString As String = String.Format("Sum({0})", AggregateField)
Dim FilterString As String = String.Format("{0} = '{1}'", SortFieldName, LastMatch.Replace("'", "''"))
NewRow.Item(AggregateField) = ReturnTable.Compute(FormatString, FilterString)
Next AggregateField
RowsToAddList.Add(NewRow)
End If
End If
LastMatch = ReturnRow.Item(SortFieldName).ToString
Next ReturnRow
For Each RowToAdd As Data.DataRow In RowsToAddList
ReturnTable.Rows.Add(RowToAdd)
Next RowToAdd
ReturnTable = New Data.DataView(ReturnTable, "[Row~Number] <> " & LastRowNumber, "Row~Number", Data.DataViewRowState.CurrentRows).ToTable
ReturnTable.Columns.Remove("Row~Number")
Return ReturnTable
End Function
Tips and tricks for .NET using ASP and VB code.
Add aggregate rows to a table
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment