ActiveReports allows you to set up grouping in unbound reports. When setting up grouping, the group header's DataField property is used to retrieve the grouping data from the database in the same manner as a textbox's DataField property.
This walkthrough illustrates how to set up grouping in an unbound report.
This walkthrough is split into the following activities:
- Adding an ActiveReport to a Visual Studio project
- Adding code to connect the report to a data source
- Adding controls to contain the data
- Using the DataInitialize event to add fields to the report's fields collection
- Using the FetchData event to populate the report fields
- Adding code to close the connection to the data source
- 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 rptUnboundGrp.
- Click Open.
Adding code to connect the report to a data source
To write the code in Visual Basic or C#
- Double-click the gray area below rptUnboundGrp. This creates an event-handling method for rptUnboundGrp's ReportStart event. Add code to the handler to:
- Set the data source connection string
- Set the data source SQL query
- Open the connection and retrieve the data with the data reader
The following example shows what the code for the method looks like.
'Visual Basic Dim connection As System.Data.OleDb.OleDbConnection Dim reader As System.Data.OleDb.OleDbDataReader Private Sub rptUnboundGrp_ReportStart(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.ReportStart 'Create the data connection and change the data source path as necessary Dim connectionString As String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Data Dynamics\ActiveReports for .NET 3.0\Data\NWIND.MDB" connection = New System.Data.OleDb.OleDbConnection(connectionString) connection.Open() Dim sqlString As String sqlString = "SELECT * FROM categories INNER JOIN products ON categories.categoryid" _ & "= products.categoryid WHERE categories.categoryid = 1" Dim command As System.Data.OleDb.OleDbCommand(sqlString, connection) 'Retrieve data reader = command.ExecuteReader() End Sub //C# private System.Data.OleDb.OleDbConnection connection; private System.Data.OleDb.OleDbDataReader reader; private void rptUnboundGrp_ReportStart(object sender, System.EventArgs eArgs) { //Create the data connection and change the data source path as necessary string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Data Dynamics\ActiveReports for .NET 3.0\Data\NWIND.MDB"; connection=new System.Data.OleDb.OleDbConnection(connectionString); connection.Open(); string sqlString = "SELECT * FROM categories INNER JOIN products ON categories.categoryid" + "= products.categoryid WHERE categories.categoryid = 1"; System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand(sqlString, connection); //Retrieve data reader = command.ExecuteReader(); }
Adding controls to the report to contain data
To add controls to the report
- Add a GroupHeader/Footer section to your report by right-clicking the design surface of the report and selecting Insert > Group Header/Footer.
- Make the following changes to the group header:
- Change the Name property to ghCategories
- Change the BackColor property to Silver
- Change the DataField property to CategoryID
- Set the GroupKeepTogether property to All
- Set the KeepTogether property to True
- Make the following change to the group footer:
- Change the name to gfCategories
- Add the following controls to the GroupHeader section:
Control DataField Name Text Miscellaneous Location TextBox CategoryName txtCategoryName Category Name ForeColor = Blue; BackColor = LightGray; Font size = 12 0.063, 0.063 TextBox Description txtDescription Description Size = 6, 0.2 0.063, 0.375 Label lblProductName Product Name Bold 0.063, 0.688 Label lblUnitsInStock Units In Stock Bold 4.38, 0.688 - Add the following controls to the Detail section:
-
Control DataField Name Text Location TextBox ProductName txtProductName Product Name 0.063, 0.063 TextBox UnitsInStock txtUnitsInStock Units In Stock 4.75, 0.064 - Add the following controls to the GroupFooter section:
Control DataField Name Text Miscellaneous Location Label lblTotalLabel Total Number of Beverages NA 2.6, 0 TextBox ProductName txtTotalItems Total Items SummaryType = SubTotal
SummaryFunc = Count
SummaryRunning = Group
SummaryGroup = ghCategories4.75, 0 Line Line1 LineWeight = 3 X1 = 1.875
X2 = 6.44
Y1 = 0
Y2 = 0
Using the DataInitialize event to add fields
The Fields collection should never be accessed outside the DataInitialize and FetchData events. |
To write the code in Visual Basic
- Right-click in any section of the design window of rptUnboundGrp, and click on View Code to display the code view for the report. At the top left of the code view for rptUnboundGrp, click the drop-down arrow and select (rptUnboundGrp Events). At the top right of the code window, click the drop-down arrow and select DataInitialize. This creates an event-handling method for rptUnboundGrp's DataInitialize event. Add code to the handler to:
- Add fields to the report's Fields collection.
To write the code in C#
- Click in the gray area below rptUnboundGrp to select the report. Click on the events icon in the Properties window to display available events for the report. Double-click DataInitialize. This creates an event-handling method for rptUnboundGrp's DataInitialize event. Add code to the handler to:
- Add fields to the report's Fields collection.
The following example shows what the code for the method looks like.
'Visual Basic Private Sub rptUnboundGrp_DataInitialize(ByVal sender As Object, ByVal e As _ System.EventArgs) Handles MyBase.DataInitialize Fields.Add("CategoryID") Fields.Add("CategoryName") Fields.Add("ProductName") Fields.Add("UnitsInStock") Fields.Add("Description") Fields.Add("TotalLabel") End Sub //C# private void UnboundGrp_DataInitialize(object sender, System.EventArgs eArgs) { Fields.Add("CategoryID"); Fields.Add("CategoryName"); Fields.Add("ProductName"); Fields.Add("UnitsInStock"); Fields.Add("Description"); Fields.Add("TotalLabel"); }
Using the FetchData event to populate the report fields
To write the code in Visual Basic
- Right-click in any section of the design window of rptUnboundGrp, and click on View Code to display the code view for the report. At the top left of the code view for rptUnboundGrp, click the drop-down arrow and select (rptUnboundGrp Events). At the top right of the code window, click the drop-down arrow and select FetchData. This creates an event-handling method for rptUnboundGrp's FetchData event. Add code to the handler to:
- Retrieve information to populate the report fields.
To write the code in C#
- Click in the gray area below rptUnboundGrp to select the report. Click on the events icon in the Properties window to display available events for the report. Double-click FetchData. This creates an event-handling method for rptUnboundGrp's FetchData event. Add code to the handler to:
- Retrieve information to populate the report fields.
The following example shows what the code for the method looks like.
'Visual Basic Private Sub rptUnboundGrp_FetchData(ByVal sender As Object, ByVal eArgs As DataDynamics_ .ActiveReports.ActiveReport3.FetchEventArgs) Handles MyBase.FetchData Try reader.Read() Me.Fields("CategoryID").Value = reader("categories.CategoryID") Me.Fields("CategoryName").Value = reader("CategoryName") Me.Fields("ProductName").Value = reader("ProductName") Me.Fields("UnitsInStock").Value = reader("UnitsInStock") Me.Fields("Description").Value = reader("Description") Me.Fields("TotalLabel").Value = "Total Number of " + reader("CategoryName") + ":" eArgs.EOF = False Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.ToString()) eArgs.EOF = True End Try End Sub //C# private void UnboundGrp_FetchData(object sender, DataDynamics.ActiveReports.ActiveReport3 .FetchEventArgs eArgs) { try { reader.Read(); Fields["CategoryID"].Value = reader["categories.CategoryID"].ToString(); Fields["CategoryName"].Value = reader["CategoryName"].ToString(); Fields["ProductName"].Value = reader["ProductName"].ToString(); Fields["UnitsInStock"].Value = reader["UnitsInStock"].ToString(); Fields["Description"].Value = reader["Description"].ToString(); Fields["TotalLabel"].Value = "Total Number of " + reader["CategoryName"].ToString() + ":"; eArgs.EOF = false; } catch { eArgs.EOF = true; } }
Adding code to close the connection to the data source
To write the code in Visual Basic
- At the top left of the code view for rptUnboundGrp, click the drop-down arrow and select (rptUnboundGrp Events). At the top right of the code window, click the drop-down arrow and select ReportEnd. This creates an event-handling method for rptUnboundGrp's ReportEnd event. Add code to the handler to:
- Close the connection
To write the code in C#
- Click in the gray area below rptUnboundGrp to select the report. Click on the events icon in the Properties window to display available events for the report. Double-click ReportEnd. This creates an event-handling method for rptUnboundGrp's ReportEnd event. Add code to the handler to:
- Close the connection
The following example shows what the code for the method looks like.
'Visual Basic Private Sub rptGrp_ReportEnd(ByVal sender As Object, ByVal eArgs As System.EventArgs) reader.Close() connection.Close() End Sub //C# private void rptGrp_ReportEnd(object sender, System.EventArgs eArgs) { reader.Close(); connection.Close(); }
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. When previewing reports that have SQL statements generated at run time, a Save File As dialog will prompt you to save your queries in the project folder. |