Glossary Item Box
ActiveReports allows you to set up grouping in unbound reports. When setting up grouping, the group header's DataField property is used in the same manner as a textbox's DataField property to retrieve the grouping data from the database.
This walkthrough illustrates how to set up grouping in an unbound report.
This walkthrough is split into the following activities:
To complete the walkthrough, you must have access to the NorthWind database (NWind.mdb).
When you have completed this walkthrough, you will have a report that looks similar to the following.
To add an ActiveReport to your project
To write the code in Visual Basic
To write the code in C#
The following example shows what the code for the method looks like.
'Visual Basic Dim m_cnnString As String Dim sqlString As String Dim m_reader As OleDbDataReader Dim m_cnn As OleDbConnection Private Sub rptUnboundGrp_ReportStart(ByVal sender As Object, ByVal _ e As System.EventArgs) Handles MyBase.ReportStart m_cnnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program _ Files\Data Dynamics\ActiveReports.NET\Data\NWIND.MDB;Persist Security Info _ =False" sqlString = "SELECT * FROM categories INNER JOIN products ON categories.categoryid _ = products.categoryid ORDER BY products.categoryid, products.productid" m_cnn = New OleDb.OleDbConnection(m_cnnString) Dim m_Cmd As New OleDb.OleDbCommand(sqlString, m_cnn) If m_cnn.State = ConnectionState.Closed Then m_cnn.Open() End If m_reader = m_Cmd.ExecuteReader() End Sub
//C#
private OleDbConnection m_cnn;
private OleDbDataReader m_reader;
private void rptUnboundGrp_ReportStart(object sender, System.EventArgs eArgs)
{
string m_dbPath = getDatabasePath();
string m_cnnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program
Files\Data Dynamics\ActiveReports.NET\Data\NWIND.MDB;Persist Security
Info=False";
string sqlString = "SELECT * FROM categories INNER JOIN products ON categories
.categoryid = products.
categoryid ORDER BY products.categoryid, products.productid";
m_cnn = new OleDbConnection(m_cnnString);
OleDbCommand m_Cmd = new OleDbCommand(sqlString,m_cnn);
if(m_cnn.State == ConnectionState.Closed)
{
m_cnn.Open();
}
m_reader = m_Cmd.ExecuteReader();
}To write the code in Visual Basic
To write the code in C#
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)
m_reader.Close
m_cnn.Close
End Sub
//C#
private void rptGrp_ReportEnd(object sender, System.EventArgs eArgs)
{
m_reader.Close();
m_cnn.Close();
}To add controls to the report
| Control | DataField | Name | Text/Caption | Location |
|---|---|---|---|---|
| TextBox | CategoryName | txtCategoryName | Category Name | 0.0625, 0.0625 |
| TextBox | Description | txtDescription | Description | 0.0625, 0.375 |
| Label | (Empty string) | lblProductName | Product Name | 0.0625, 0.6875 |
| Label | (Empty string) | lblUnitsInStock | Units In Stock | 4.75, 0.6875 |
| Control | DataField | Name | Text/Caption | Location |
|---|---|---|---|---|
| TextBox | ProductName | txtProductName | Product Name | 0.0625, 0.0625 |
| TextBox | UnitsInStock | txtUnitsInStock | Units In Stock | 4.75, 0.0625 |
| Control | DataField | Name | Text/Caption | Misc Details | Location |
|---|---|---|---|---|---|
| Label | TotalLabel | lblTotalLabel | Total Label | (Empty string) | 1.875, 0 |
| TextBox | ProductName | txtTotalItems | Total Items | SummaryType = SubTotal
SummaryFunc = Count SummaryRunning = Group SummaryGroup = ghCategories |
5, 0 |
| Line | (Empty string) | Line1 | (Empty string) | LineWeight = 3 | X1 = 1.875
Y1 = 0 X2 = 6.4375 Y2 = 0 |
Note: The Fields collection should never be accessed outside the DataInitialize and FetchData events.
To write the code in Visual Basic
To write the code in C#
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");
}To write the code in Visual Basic
To write the code in C#
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.ActiveReport.FetchEventArgs) Handles MyBase.FetchData
Try
m_reader.Read()
Me.Fields("CategoryID").Value = m_reader("categories.CategoryID")
Me.Fields("CategoryName").Value = m_reader("CategoryName")
Me.Fields("ProductName").Value = m_reader("ProductName")
Me.Fields("UnitsInStock").Value = m_reader("UnitsInStock")
Me.Fields("Description").Value = m_reader("Description")
Me.Fields("TotalLabel").Value = "Total Number of " + m_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.ActiveReport
.FetchEventArgs eArgs)
{
try
{
m_reader.Read();
Fields["CategoryID"].Value = m_reader["categories.CategoryID"].ToString();
Fields["CategoryName"].Value = m_reader["CategoryName"].ToString();
Fields["ProductName"].Value = m_reader["ProductName"].ToString();
Fields["UnitsInStock"].Value = m_reader["UnitsInStock"].ToString();
Fields["Description"].Value = m_reader["Description"].ToString();
Fields["TotalLabel"].Value = "Total Number of " + m_reader
["CategoryName"].ToString() + ":";
eArgs.EOF = false;
}
catch
{
eArgs.EOF = true;
}
}To view the report
| See Also |
Samples | Walkthroughs | Grouping Data | SummaryFunc enumeration | GroupKeepTogether enumeration
Copyright © 2004-2005 Data Dynamics, Ltd. All rights reserved.