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.
Employee | Spent Hours |
John Smith | 10 |
Rob McDonald | 20 |
Bob Kuch | 15 |
Total: | 45 |
- 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>
- 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>
- Set the Data Type
property to Integer or Float for the SpentHours
and Summ Label controls.
- 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() );
}
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
- Add a variable called Summ below the 'End Forms Definition
section.
'End Forms Definition
Dim Summ As Integer ' or Double
- Add the Before
Show Row event for the Grid.
'Employees_Summ_BeforeShow
EmployeesSumm.Text = Summ.ToString()
- Add the Before
Show event for the Summ Label.
'Employees_BeforeShowRow
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Summ += DataItem.SpentHours.Value
End If
C#
- Add a variable called Summ below the 'End Forms Definition
section.
//End Forms Definition
protected Int64 Summ;
-
Add the Before
Show Row event for the Grid.
//Employees_Summ_BeforeShow
EmployeesSumm.Text = Summ.ToString();
-
Add the Before
Show event for the Summ Label.
//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