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

Simple Report with Simple Grouping

This example shows how to hide duplicate values on a Grid form. In this case, the Grid has a column for Department and another for Employee. The records in the Grid are sorted by department so records with the same department appear in consecutive rows. Instead of having the department name appear in each row, this example shows how to suppress the department name if it already appeared in a higher row.
DepartmentEmployee
AdministrationJohn Smith
Rob McDonald
SoftwareBob Kuch
David Snyder

  1. Add the Before Show Row event for the Grid form.
  2. Within the event, add the code below:

ASP

Dim PrevDepartment

Function Employees_BeforeShowRow(Sender)
  
  If PrevDepartment = Employees.Department.Value Then
     Employees.Department.Value = "" 
  Else
     PrevDepartment = Employees.Department.Value
  end if 

End Function

PHP

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

  if ($PrevDepartment == $Employees->department->GetValue()) {
     $Employees->department->SetValue(""); 
  } else {
     $PrevDepartment = $Employees->department->GetValue();
  } 

}

Perl

sub Employees_BeforeShowRow() {

  if ($PrevDepartment eq $Employees->{department}->GetValue()) {
     $Employees->{department}->SetValue(""); 
  } else {
     $PrevDepartment = $Employees->{department}->GetValue();
  } 

}

ColdFusion

<!---Employees_BeforeShowRow --->
  <CFPARAM Name="PrevDepartment" Default="">
  <CFIF PrevDepartment EQ fldDepartment>
    <CFSET fldDepartment="">
  <CFELSE>
    <CFSET PrevDepartment = fldDepartment>
  </CFIF>

VB.Net

  1. Add a variable called PrevDepartment below the End Forms Definition section
  2. Add the Before Show Row event for the grid.
  3. Within the event, add the code below:
Dim PrevDepartment As String

'Employees_BeforeShowRow 
  If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
    Dim EmployeesDepartment As System.Web.UI.WebControls.Literal = DirectCast(e.Item.FindControl("EmployeesDepartment"),System.Web.UI.WebControls.Literal)
    If PrevDepartment = DataItem.Department.Value Then
      EmployeesDepartment.Text = "" 
    Else
      PrevDepartment = DataItem.Department.Value
    End If 
  End If

C#

  1. Add a variable called PrevDepartment below the End Forms Definition section
  2. Add the Before Show Row event for the grid.
  3. Within the event, add the code below:
protected string PrevDepartment;

//Employees_BeforeShowRow 
  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
     System.Web.UI.WebControls.Literal EmployeesDepartment = (System.Web.UI.WebControls.Literal)e.Item.FindControl("EmployeesDepartment");
     if (PrevDepartment == (DataItem.Department.Value).ToString()) {
        EmployeesDepartment.Text = ""; 
     } else {
        PrevDepartment = (DataItem.Department.Value).ToString();
     } 
  }

Java

  1. Add the Before Show Row event for the Grid form.
  2. Within the event, add the code below:
//departments_BeforeShowRow

  String depName = (String) e.getPage().getRequest().getAttribute("departmentname");
  if ( depName == null ) {
    depName = "";
  }
  if (depName.equals(e.getGrid().getControl("Department").getValue()) ) {
    e.getGrid().getControl("Department").setValue("");
  } else {
    e.getPage().getRequest().setAttribute("departmentname", e.getGrid().getControl("Department").getValue());
  }

See also:

Before Show Row event, Grid form


On-line, printable versions and updates