CodeCharge Studio All ASP PHP Perl ColdFusion Java C#.NET VB.NET

Simple Report with Total

This example shows how to create a simple report with a Total row at the bottom of the grid. The Grid has a column for the Employee name and another for the number of hours spent by the employee. The bottom row sums up the spent hours for all the employees listed in the grid.
EmployeeSpent Hours
John Smith10
Rob McDonald20
Bob Kuch15
Total:45

  1. Switch to HTML mode and add the HTML code in blue below the Row block.
    <!-- BEGIN Row -->
      <tr>
       <td class="ClearDataTD">{Employee}</td>
       <td class="ClearDataTD">{SpentHours}</td>
      </tr>
    <!-- END Row -->
      <tr>
       <td class="ClearDataTD"><b>Total</b></td>
       <td class="ClearDataTD"></td>
      </tr>
    
  2. Add a Summ Label control into the last cell of the added code. ( Note that we use 'Summ' and not 'Sum' because 'Sum' is a reserved word in some of the languages including SQL).
    <!-- BEGIN Row -->
      <tr>
       <td class="ClearDataTD">{Employee}</td>
       <td class="ClearDataTD">{SpentHours}</td>
      </tr>
    <!-- END Row -->
      <tr>
       <td class="ClearDataTD"><b>Total</b></td>
       <td class="ClearDataTD">{Summ}</td>
      </tr>
    
  3. Set the Data Type property to Integer or Float for the SpentHours and Summ Label controls.
  4. In the Before Show Row event of the grid, add the code below:

ASP

Function Employees_BeforeShowRow(Sender)

  Employees.Summ.Value  = Employees.Summ.Value  + Employees.SpentHours.Value

End Function

PHP

function Employees_BeforeShowRow(& $sender) {
global $Employees;

  $Employees->Summ->SetValue($Employees->Summ->GetValue() + $Employees->SpentHours->GetValue() );

}

Perl

sub Employees_BeforeShowRow() {

  $Employees->{Summ}->SetValue($Employees->{Summ}->GetValue() + $Employees->{SpentHours}->GetValue() );

}

ColdFusion

<!---Employees_BeforeShowRow --->

  <CFSET fldSumm=Val(fldSumm) + fldSpentHours>

Java

//Employees_BeforeShowRow

  long summ = e.getGrid().getControl("Summ").getValue() == null ? 0 : Utils.convertToLong(e.getGrid().getControl("Summ").getValue()).longValue();
  long spent = e.getGrid().getControl("SpentHours").getValue() == null ? 0 : Utils.convertToLong(e.getGrid().getControl("SpentHours").getValue()).longValue();
  e.getGrid().getControl("Summ").setValue( summ + spent);

VB.Net

  1. Add a variable called Summ below the 'End Forms Definition section.
  2. 'End Forms Definition 
      Dim Summ As Integer ' or Double 
    
    
  3. Add the Before Show Row event for the Grid.
  4. 'Employees_Summ_BeforeShow
    
        EmployeesSumm.Text = Summ.ToString()
    
    
  5. Add the Before Show event for the Summ Label.
  6. 'Employees_BeforeShowRow
    
      If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
        Summ +=  DataItem.SpentHours.Value
      End If
    
    

C#

  1. Add a variable called Summ below the 'End Forms Definition section.
  2. //End Forms Definition 
    
     protected Int64 Summ;
    
  3. Add the Before Show Row event for the Grid.
  4. //Employees_Summ_BeforeShow
    
      EmployeesSumm.Text = Summ.ToString();
    
  5. Add the Before Show event for the Summ Label.
  6. //Employees_BeforeShowRow
    
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
          Summ +=  (Int64)(DataItem.SpentHours.Value);
      }


See also:

Before Show Row event, Grid form, Label control


On-line, printable versions and updates