ActiveReports objects and controls are completely accessible at run time. The properties of any of the report sections or controls can be modified to produce a dynamic view of the report. The format event allows the properties of report sections and controls to be modified including height, visibility, and other visual properties. The format event is the only event in which the printable area of a section can be modified. Once this event is completed, any changes to the section's height will not be reflected in the report output.
Controls may be added dynamically in the ReportStart event but should not be added dynamically after the ReportStart event is raised as problems may result. |
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 Windows Form to contain data
- Generating a dataset for the Windows Form
- Adding code to create the report layout
- Adding code to the Windows Form
- Adding code to the report's Detail_Format event
- Adding code to the report's ReportStart event
- Adding code to the Windows Form's "Generate Report" button Click event
- Adding code to the Checked ListBox's SelectedIndexChanged event on the Windows Form
- Adding code to the Windows Form_Load 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 an output that looks similar to the following.
Adding an ActiveReport to your project
To add an ActiveReport to your project
- From the Project menu, select Add New Item.
- Select ActiveReports 3.0 File and rename the file rptFieldsRT.
- 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.
- Click on Build.
- 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".
- Click OK to return to the report design surface.
Adding controls to the Windows Form to contain data
To add controls to the form
Control | Location | Dock Property | Name | Text |
---|---|---|---|---|
Panel | Windows Form | Left | Panel1 | |
Label | Panel1 | Top | lblSelectFields | Select Fields for Your Report |
Checked ListBox | Panel1 | Fill | clbFields | |
Button | Panel1 | Bottom | btnGenRep | Generate Report |
CheckBox | Panel1 | Bottom | chkGroup | Group By Category ID |
Viewer | Windows Form | Fill | Viewer1 |
Generating a dataset for the Windows Form
To generate the dataset
- From the Project menu, select Add New Item.
- Select DataSet, rename the file to NWINDDataSet.xsd and click the Add button.
- Open the Server Explorer by clicking its link in the DataSet designer.
- In the Server Explorer, expand the node for your local copy of the Northwind database and drag the Products table onto the DataSet designer. If you do not see a copy of the Northwind database, click the "Connect to Database" icon and follow the prompts.
- In the design view of your Windows form, expand the Data section of the Visual Studio Toolbox and double-click DataSet to open the Add Dataset dialog.
- Under Typed dataset, select <project name>.NWINDDataSet and click OK to make the dataset available to your Windows form.
Adding code to create the report layout
To add code to create the report layout
- Right-click on rptFieldsRT and select "View Code." Insert the following code within the class declaration of the report.
'Visual Basic Private m_arrayFields As ArrayList Private m_useGroups As Boolean Public WriteOnly Property FieldsList() As ArrayList Set(ByVal Value As ArrayList) m_arrayFields = Value End Set End Property Public WriteOnly Property UseGroups() As Boolean Set(ByVal Value As Boolean) m_useGroups = False m_useGroups = Value End Set End Property Private m_defaultHeight As Single = 0.2F Private m_defaultWidth As Single = 4.0F Private m_currentY As Single = 0.0F Private Sub constructReport() Try Me.Detail1.CanGrow = True Me.Detail1.CanShrink = True Me.Detail1.KeepTogether = True If m_useGroups = True Then Me.Sections.InsertGroupHF() CType(Me.Sections("GroupHeader1"), GroupHeader). _ DataField = "CategoryID" Me.Sections("GroupHeader1").BackColor = System.Drawing. _ Color.Gray Me.Sections("GroupHeader1").CanGrow = True Me.Sections("GroupHeader1").CanShrink = True CType(Me.Sections("GroupHeader1"), GroupHeader).RepeatStyle _ = RepeatStyle.OnPageIncludeNoDetail Dim txt As New TextBox txt.DataField = "CategoryID" txt.Location = New System.Drawing.PointF(0.0F, 0) txt.Width = 2.0F txt.Height = 0.3F txt.Style = "font-weight: bold; font-size: 16pt" Me.Sections("GroupHeader1").Controls.Add(txt) End If Dim i As Integer For i = 0 To m_arrayFields.Count - 1 If m_arrayFields(i).ToString <> "CategoryID" Then Dim lbl As New Label lbl.Text = m_arrayFields(i) + ":" lbl.Location() = New System.Drawing.PointF _ (0.0F, m_currentY) lbl.Width = 0.9F lbl.Height = m_defaultHeight Me.Detail1.Controls.Add(lbl) Dim txt As New TextBox txt.DataField = m_arrayFields(i) txt.Location = New System.Drawing.PointF _ (1.0F, m_currentY) txt.Width = m_defaultWidth txt.Height = m_defaultHeight Me.Detail1.Controls.Add(txt) If m_arrayFields(i) = "UnitPrice" Then txt.OutputFormat = "$#.00" End If m_currentY = m_currentY + m_defaultHeight End If Next Catch ex As Exception System.Windows.Forms.MessageBox.Show _ ("Error in Report-constructReport: " + ex.Message, "Project Error", _ System.Windows.Forms.MessageBoxButtons.OK, _ System.Windows.Forms.MessageBoxIcon.Error) End Try End Sub //C# private ArrayList m_arrayFields; public ArrayList FieldsList { set{m_arrayFields = value;} } private bool m_useGroups = false; public bool UseGroups { set{m_useGroups = value;} } float m_defaultHeight = .2f; float m_defaultWidth = 4f; float m_currentY = 0f; private void constructReport() { try { this.detail.CanGrow = true; this.detail.CanShrink = true; this.detail.KeepTogether = true; if(m_useGroups) { this.Sections.InsertGroupHF(); ((GroupHeader)this.Sections["GroupHeader1"]).DataField = "CategoryID"; this.Sections["GroupHeader1"].BackColor = System.Drawing .Color.Gray; this.Sections["GroupHeader1"].CanGrow = true; this.Sections["GroupHeader1"].CanShrink = true; ((GroupHeader)this.Sections["GroupHeader1"]).RepeatStyle = RepeatStyle.OnPageIncludeNoDetail; this.Sections["GroupFooter1"].Height = 0; TextBox txt = new TextBox(); txt.DataField = "CategoryID"; txt.Location = new System.Drawing.PointF(0f,0); txt.Width =2f; txt.Height = .3f; txt.Style = "font-weight: bold; font-size: 16pt;"; this.Sections["GroupHeader1"].Controls.Add(txt); } for(int i=0;i<m_arrayFields.Count;i++) { if(!m_useGroups || (m_useGroups && m_arrayFields[i] .ToString() != "CategoryID")) { Label lbl = new Label(); lbl.Text = m_arrayFields[i].ToString() + ":"; lbl.Location = new System.Drawing.PointF (0f,m_currentY); lbl.Width =.9f; lbl.Height = m_defaultHeight; this.detail.Controls.Add(lbl); TextBox txt = new TextBox(); txt.DataField = m_arrayFields[i].ToString(); txt.Location = new System.Drawing.PointF (1f,m_currentY); txt.Width = m_defaultWidth; txt.Height = m_defaultHeight; this.detail.Controls.Add(txt); m_currentY = m_currentY + m_defaultHeight; } } } catch(Exception ex) { System.Windows.Forms.MessageBox.Show("Error in Report-constructReport: " + ex.Message,"Project Error",System.Windows.Forms .MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error); } }
Adding code to the Windows Form
To add code to the form
- Right-click on the Windows Form and select "View Code." Insert the following code within the class declaration of the Form (after the Main() method for C# 2003).
'Visual Basic Dim i As Integer Dim c As Integer Dim m_arrayField As New ArrayList() Private Sub fillCheckBox() For i = 0 To Me.DataSet11.Tables.Count - 1 For c = 0 To Me.DataSet11.Tables(i).Columns.Count - 1 Me.clbFields.Items.Add(Me.DataSet11.Tables(i).Columns(c).ColumnName) Next Next End Sub Private Sub launchReport() Dim rpt As New rptFieldsRT() Try rpt.FieldsList = m_arrayField rpt.UseGroups = chkGroup.Checked rpt.DataSource = this.DataSet11.Products.TableName Viewer1.Document = rpt.Document rpt.Run() Catch ex As Exception System.Windows.Forms.MessageBox.Show(Me, "Error in launchReport: " _ + ex.Message, "Project Error", MessageBoxButtons.OK, _ MessageBoxIcon.Error) End Try End Sub //C# //Place this code after the Main() method in Visual Studio for .NET 2003 ArrayList m_arrayField = new ArrayList(); private void fillCheckBox() { for(int i = 0; i < this.dataSet11.Tables.Count; i++) { for(int c = 0; c < this.dataSet11.Tables[i].Columns.Count; c++) { this.clbFields.Items.Add(this.dataSet11.Tables[i].Columns[c] .ColumnName); } } } private void launchReport() { try { rptFieldsRT rpt = new rptFieldsRT(); rpt.FieldsList = m_arrayField; rpt.UseGroups = chkGroup.Checked; rpt.DataSource = this.dataSet11.Products.TableName; this.viewer1.Document = rpt.Document; rpt.Run(); } catch(Exception ex) { MessageBox.Show(this,"Error in launchReport: " + ex.Message,"Project Error",MessageBoxButtons.OK,MessageBoxIcon.Error); } }
Adding code to the Detail_Format event
To write the code in Visual Basic or C#
- Double-click in the detail section of rptFields. This creates an event-handling method for rptFieldsRT's Detail_Format event.
The following example shows what the code for the method looks like.
'Visual Basic Dim m_count As Integer Private Sub Detail1_Format(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles Detail1.Format If m_count Mod 2 = 0 Then Me.Detail1.BackColor = System.Drawing.Color.SlateGray Else Me.Detail1.BackColor = System.Drawing.Color.Gainsboro End If m_count = m_count + 1 End Sub //C# int m_count; private void detail_Format(object sender, System.EventArgs eArgs) { if(m_count % 2 == 0) { this.detail.BackColor = System.Drawing.Color.SlateGray; } else { this.detail.BackColor = System.Drawing.Color.Gainsboro; } m_count++; }
Adding code to the ReportStart event
To write the code in Visual Basic or C#
- Double-click in the gray area below rptFieldsRT to create an event-handling method for rptFieldsRT's ReportStart event. Add the following code to call the ConstructReport() method.
The following example shows what the code for the method looks like.
'Visual Basic Private Sub rptFieldsRT_ReportStart(ByVal sender As Object, ByVal e As _ System.EventArgs) Handles MyBase.ReportStart constructReport() End Sub //C# private void rptFieldsRT_ReportStart(object sender, System.EventArgs eArgs) { constructReport(); }
Adding code for btnGenRep's Click event
To write the code in Visual Basic or C#
- Double-click btnGenRep to create an event-handling method for btnGenRep's Click event. Add the following code to the btnGenRep_Click event.
The following example shows what the code for the method looks like.
'Visual Basic Private Sub btnGenRep_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles btnGenRep.Click Me.m_arrayField.Clear() For i = 0 To Me.clbFields.CheckedItems.Count - 1 m_arrayField.Add(Me.clbFields.CheckedItems(i).ToString) Next launchReport() End Sub //C# ArrayList m_arrayField = new ArrayList(); private void btnGenRep_Click(object sender, System.EventArgs e) { this.m_arrayField.Clear(); for(int i = 0; i < this.clbFields.CheckedItems.Count; i++) { m_arrayField.Add(this.clbFields.CheckedItems[i].ToString()); } launchReport(); }
Adding code to the clbFields_SelectedIndexChanged event
To write the code in Visual Basic
- Right-click in any section of the Windows Form, and click on View Code to display the code view for the Windows Form. At the top left of the code view for the form, click the drop-down arrow and select clbFields. At the top right of the code window, click the drop-down arrow and select SelectedIndexChanged. This creates an event-handling method for the clbFields_SelectedIndexChanged event.
To write the code in C#
- Click clbFieds to select it. Click on the events icon in the Properties window to display available events and double-click SelectedIndexChanged. This creates an event-handling method for the clbFields_SelectedIndexChanged event.
The following example shows what the code for the method looks like.
'Visual Basic Private Sub clbFields_SelectedIndexChanged(ByVal sender As Object, ByVal e _ As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged If Me.clbFields.CheckedItems.Count < 0 Then Me.btnGenRep.Enabled = False Else Me.btnGenRep.Enabled = True End If End Sub //C# private void clbFields_SelectedIndexChanged(object sender, System.EventArgs e) { if(this.clbFields.CheckedItems.Count>0) { this.btnGenRep.Enabled = true; } else { this.btnGenRep.Enabled = false; } }
Adding code to the Windows Form_Load event
To write the code for the viewer in Visual Basic or C#
- Double-click the title bar of the form. This creates an event-handling method for the Windows Form_Load event. Add code to the handler to:
- Call the fillCheckBox() method to populate clbFields with field values
The following example shows what the code for the method looks like.
'Visual Basic Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Try fillCheckBox() Catch ex As Exception System.Windows.Forms.MessageBox.Show(Me, "Error in Form1_Load: " + ex _ .Message, "Project Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub //C# private void Form1_Load(object sender, System.EventArgs e) { try { fillCheckBox(); } catch(Exception ex) { MessageBox.Show(this,"Error in Form1_Load: " + ex.Message,"Project Error", + MessageBoxButtons.OK,MessageBoxIcon.Error); } }
Viewing the report
To view the report
- Press F5 to run the project.
- Select fields in the Checked list box.
- Click the Generate Report button.
You can quickly view your report at design time by clicking the Preview tab at the bottom of the designer. |