ActiveReports for .NET 3 Online Help Request technical support
Walkthrough: Conditional Show-Hide Detail
See Also
User Guide > Samples and Walkthroughs > Walkthroughs > Standard Edition Walkthroughs > Basic > Grouping Data Walkthroughs > Walkthrough: Conditional Show-Hide Detail

Glossary Item Box

ActiveReports allows you to hide or show information from the data source in the Detail section of your report based on conditions in your data. This can be achieved by setting section properties in the Format event.

This walkthrough illustrates how to create a report based on conditions that will show specific data from your data source at run time.

This walkthrough is split up into the following activities:

To complete the walkthrough, you must have access to the Northwind database.
A copy is located at C:\Program Files\Data Dynamics\ActiveReports for .NET 3.0\Data\NWIND.MDB.

When you have completed this walkthrough, you will have a report that looks similar to the following.

Adding an ActiveReport to a Visual Studio project

To add an ActiveReport to your project

  1. Open a new project in Visual Studio.
  2. From the Project menu, select Add New Item.
  3. Select ActiveReports 3.0 File and rename the file rptCondSH.
  4. Click Open.

Connecting the report to a data source

To connect the report to a data source

  1. Click on the gray report DataSource icon in the Detail section to open the report DataSource dialog.
  2. Select the "OLE DB" tab.
  3. Click the Build button.
  4. Select Microsoft Jet 4.0 OLE DB Provider and click Next.
  5. Click the ellipsis button to browse for the access path to the Northwind database. Click Open once you have selected the appropriate access path.
  6. Click OK to continue.
  7. In the Query field, type "Select * from Products ORDER BY ProductName".
  8. Click OK to return to the report design surface.

Adding controls to the report to contain data

To add controls to the report

  1. Add labels with the following properties set to the PageHeader section:

    Miscellaneous Name Text Location
    Bold lblProductID Product ID 0, 0
    Bold  lblProductName Product Name 1.063, 0
    Bold  lblInStock Units In Stock 2.5, 0
    Bold  lblReorderLevel  Reorder Level 3.813, 0

  2. Set the CanShrink property of the Detail section to True.
  3. In the Report Explorer, expand the Fields node, then the Bound node. Drag the following fields onto the detail section and set the following properties of each textbox as indicated.

    Field Text Location
    ProductID Product ID 0, 0
    ProductName Product Name 1.063, 0
    UnitsInStock Units In Stock 2.510, 0
    ReorderLevel Reorder Level 3.833, 0
    Discontinued Discontinued 5.291, 0

Adding conditions in code to the format event

To write the code in Visual Basic or C#

The following example shows what the code for the method looks like.

'Visual Basic
Private Sub Detail1_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
	Detail1.Format
	If  Me.txtReorderLevel.Value=0 And Me.txtDiscontinued.Value = False Then
		Me.Detail1.Visible = True
		Me.txtDiscontinued.Text = ""
		Me.txtReorderLevel.Text = "Need to Reorder"
		Me.txtReorderLevel.ForeColor = System.Drawing.Color.DarkRed
	Else
		Me.Detail.Visible = False
	End If
End Sub

//C#
int m_dis;
private void detail1_Format(object sender, System.EventArgs eArgs)
{
   m_dis = bool.Parse(txtDiscontinued.Text)? 1 : 0;
   
      if((short)txtReorderLevel.Value == 0 && m_dis == 0)
	{
		this.detail1.Visible = true;
		this.txtDiscontinued.Text = "";
		this.txtReorderLevel.Text = "Need to Reorder";
		this.txtReorderLevel.ForeColor = System.Drawing.Color.DarkRed;
	}
	else
	{
		this.detail1.Visible = false;
	}
}

Viewing the report

To view the report

  1. Add the ActiveReports viewer control to a Windows Form.
  2. Add the code needed to set the viewer document equal to the report document. See Using the ActiveReports Windows Form Viewer for help.
You can quickly view your report at design time by clicking the Preview tab at the bottom of the designer.

See Also

©2009. All Rights Reserved.