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:
- Adding an ActiveReport to a Visual Studio project
- Connecting the report to a data source
- Adding controls to the report to contain data
- Adding code to retrieve data from the data source
- Adding conditions in code to the Format event
- Viewing the report
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
- Open a new project in Visual Studio.
- From the Project menu, select Add New Item.
- Select ActiveReports 3.0 File and rename the file rptCondSH.
- Click Open.
Connecting the report to a data source
To connect the report to a data source
- Click on the gray report DataSource icon in the Detail section to open the report DataSource dialog.
- Select the "OLE DB" tab.
- Click the Build button.
- Select Microsoft Jet 4.0 OLE DB Provider and click Next.
- Click the ellipsis button to browse for the access path to the Northwind database. Click Open once you have selected the appropriate access path.
- Click OK to continue.
- In the Query field, type "Select * from Products ORDER BY ProductName".
- Click OK to return to the report design surface.
Adding controls to the report to contain data
To add controls to the report
- 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 - Set the CanShrink property of the Detail section to True.
- 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#
- Double-click in the Detail section to create an event-handling method for rptCondSH's Detail_Format event. Add code to the handler to:
- Set conditions in the Format event
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
- Add the ActiveReports viewer control to a Windows Form.
- 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. |