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

Simple Report With Group Headers

This example shows how to create a simple report. The Grid form in this example contains a list of employees whereby each employee belongs to a department. The Grid lists the employees (: ID, Name and Title) in such a manner that a Header row indicates the department of the employee records appearing in the rows below it.
IDNameTitle
Finance
2John SmithManager
5Rob McDonaldManager
Software
1Bob KuchSr. Developer
3Pablo SanchezDeveloper
7David SnyderDeveloper
Support
6Stefan FeySupport

For ASP, PHP, Perl, ColdFusion

Make sure that you are in HTML mode then:

  1. Modify the HTML content as shown below. In this case, the code in blue has been added to the existing HTML code for the Grid.
  2. <!-- BEGIN Row -->
      <!-- BEGIN Label department_name -->
      <tr>
        <td bgcolor="#336699" colspan="3"><font color="White">{department_name}</font></td>
      </tr>
      <!-- END Label department_name -->
      <tr>
        <td class="ClearDataTD">{Label1} </td>
        <td class="ClearDataTD">{emp_name} </td>
        <td class="ClearDataTD">{title} </td>
      </tr>
      <!-- END Row --
  3. In the Before Show Row event of the grid, add the following code:

ASP

Dim DepName
Function departments_BeforeShowRow(Sender)

  If DepName = departments.department_name.Value Then
     departments.department_name.Visible = False
  Else
     departments.department_name.Visible = True
     DepName = departments.department_name.Value
   End  If 

End Function

PHP

function departments_BeforeShowRow(& $sender) {
global $DepName;
global $departments;

  if ($DepName == $departments->department_name->GetValue()) {
     $departments->department_name->Visible = False;
  } else {
     $departments->department_name->Visible = True;
     $DepName = $departments->department_name->GetValue();
  } 

}

Perl

sub departments_BeforeShowRow() {

  if ($DepName eq $departments->{department_name}->GetValue()) {
     $departments->{department_name}->{Visible} = 0;
  } else {
     $departments->{department_name}->{Visible} = 1;
     $DepName = $departments->{department_name}->GetValue();
  } 

}

ColdFusion

<!---departments_BeforeShowRow --->
  <CFPARAM Name="DepName" Default="">
  <CFIF DepName EQ flddepartment_name>
    <CFSET hideDepartment_name= True>
  <CFELSE>
    <CFSET hideDepartment_name=False>
    <CFSET DepName=flddepartment_name>
  </CFIF>

Java

//'departments_BeforeShowRow

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

.NET

  1. If you are using C# or VB.Net, modify the HTML as follows:
  2. <!-- BEGIN Row -->
      <tr id="departmentsNameTR" runat="server">
        <td bgcolor="#336699" colspan="3"><font color="White">{department_name}</font></td>
      </tr>
      <tr>
        <td class="ClearDataTD">{Label1} </td>
        <td class="ClearDataTD">{emp_name} </td>
        <td class="ClearDataTD">{title} </td>
      </tr>
      <!-- END Row -->
  3. Then within the code for the page, add the variable called DepName below the section that ends with the text 'End Forms Definition

VB.Net

'End Forms Definition 
  Dim DepName As String 

C#

//End Forms Definition 
 protected String DepName;

 

  • Then add the following code in the Before Show Row event of the grid.
  • VB.Net

    'departments_BeforeShowRow
    
      If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim departmentsNameTR As Control = e.Item.FindControl("departmentsNameTR")
        If DepName = DataItem.department_name.Value Then
           departmentsNameTR.Visible = False
        Else
           departmentsNameTR.Visible = True
           DepName = DataItem.department_name.Value
        End If 
      End If
    
    

    C#

    //departments_BeforeShowRow
    
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
        Control departmentsNameTR = e.Item.FindControl("departmentsNameTR");
        if (DepName == DataItem.department_name.Value.ToString()) {
           departmentsNameTR.Visible = false;
        } else {
           departmentsNameTR.Visible = true;
           DepName = DataItem.department_name.Value.ToString();
        } 
      }
    
    

    See also:

    Before Show Row event, Grid form


    On-line, printable versions and updates