Skip to main content Skip to footer

AR8 : Creating Reports on the Fly

ActiveReports has been the first choice of developers when it comes to creating hassle free and versatile reports. The extensive API of ActiveReports allows Visual Basic and C# developers to completely control the report processing engine to fit their needs. Creating custom reports at runtime has been a very common requirement of many developers. And the API of ActiveReports gives the developers a complete control to create their reports from scratch which essentially means creating a report all together at runtime. The basic steps involved in creating an ActiveReport at runtime are:

  • Creating a Report Instance
  • Binding the Report to a Data Source
  • Adding Sections/Controls Dynamically and Binding them to Required Fields
  • Rendering the Report

The steps are common, with a little difference in code though, for either SectionReport and PageReport, hence we will take it one by one.

SectionReports :

Step 1: Creating a Report Instance

You need to first create an instance of SectionReport

GrapeCity.ActiveReport.SectionReport rpt = new SectionReport();

Step 2: Creating and Binding the Report to a Database

This step includes setting up a connection to a Database. Once a connection is established, you need to set the DataSource property of the report :

//Creating and setting the datasource property of the report  
conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\stduser\\Documents\\ComponentOne Samples\\ActiveReports Developer 7\\Data\\NWIND.mdb;Persist Security Info=False");  
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("SELECT * FROM Categories", conn);  
conn.Open();  
reader = cmd.ExecuteReader();  
rpt.DataSource = reader

Step 3: Adding Sections/Controls Dynamically and Binding them to Required Fields

Every SectionReport has 3 basic sections: Detail, Page Header, and Page Footer. Use the following code to set up the page header by setting a couple of properties:

//Adding Page Header/Footer sections  
 rpt.Sections.InsertPageHF();  
 rpt.Sections[0].BackColor = Color.LightGray;

Similarly, other sections can also be added in the report:

//Adding Detail section  
 rpt.Sections.Insert(1, new Detail());  
 rpt.Sections[1].BackColor = Color.PeachPuff;  
 rpt.Sections[1].Height = 1.5f;

The section object has a Controls collection. The collection's Add method creates a new control and assigns it to the Section. Next, you need to set the DataField property of the Control to the name of the column whose data is to be displayed :

//Adding label to display first column's name  
 GrapeCity.ActiveReports.SectionReportModel.Label lblCategoryID = new GrapeCity.ActiveReports.SectionReportModel.Label();  
 lblCategoryID.Location = new PointF(0, 0.05F);  
 lblCategoryID.Text = "Category ID";  
 lblCategoryID.Alignment = GrapeCity.ActiveReports.Document.Section.TextAlignment.Center;  
 lblCategoryID.Font = new System.Drawing.Font("Arial", 10, FontStyle.Bold);  
 rpt.Sections[0].Controls.Add(lblCategoryID);  

//Adding Textbox to display first column's records  
 GrapeCity.ActiveReports.SectionReportModel.TextBox txtCategoryID = new GrapeCity.ActiveReports.SectionReportModel.TextBox();  
 txtCategoryID.Location = new PointF(0,0);  
 txtCategoryID.Alignment = GrapeCity.ActiveReports.Document.Section.TextAlignment.Center;  
 rpt.Sections[1].Controls.Add(txtCategoryID);  

//Assigning DataField properties of controls in the detail section  
 txtCategoryID.DataField = "CategoryID";

Step 4: Rendering the report

Here comes the final step, where the created report needs to be run and can finally be previewed in the Viewer.

rpt.Run();  
viewer1.Document = rpt.Document;

Once done, following is how the report will look like : SectionReport DownloadSample_SR_VB DownloadSample_SR_CS

PageReports :

Step 1: Creating a Report Instance

You need to first create an instance of PageReport:

GrapeCity.ActiveReport.PageReport _pageReport = new GrapeCity.ActiveReport.PageReport();

Step 2: Creating and Binding the Report to a Database

Yet again a connection needs to be established and once done a DataSet is required to get the data you want to show in the report :

GrapCity.ActiveReports.PageReportModel.DataSource _dataSource= new GrapCity.ActiveReportsPageReportModel.DataSource();  
_dataSource.Name = "NWINDdS";  
_dataSource.ConnectionProperties.ConnectionString = @"....";  
_dataSource.ConnectionProperties.DataProvider = "OLEDB";  
GrapCity.ActiveReports.PageReportModel.DataSource _dataSet= new GrapCity.ActiveReports.PageReportModel.DataSet();  
_dataSet.Name = "Customers";  
_dataSet.Query.CommandText = "Select * From Products";  
_dataSet.Query.CommandType = GrapCity.ActiveReports.PageReportModel.QueryCommandType.Text;

In PageReports, fields needs to be explicitly added in the FieldCollection of the DataSet.

GrapeCity.ActiveReports.PageReportModel.Field _fID = new GrapeCity.ActiveReports.PageReportModel.Field();  
//Assigning field values  
_fID.Name = "CategoryID";  
_fID.DataField = "CategoryID";  
//Adding the Field to DataSet  
\_dataSet.Fields.Add(\_fID);

Once, the DataSource and DataSet objects are defined, these are added to the DataSource and DataSet Collections :

_pageReport.DataSources.Add(dataSource);  
_PageReport.DataSets.Add(dataSet);

Step 3: Adding Controls Dynamically and Binding them to Required Fields

Unlike SectionReports, PageReports has no sections but a Report element that contains property, data, and layout information about the report which is the top-level element. The Report element makes accessible the ‘Body‘ property which sets the structure for the body of the report. The controls need to be added in the ReportItem Collection of the body of the report. Before adding the control, make sure to set the Value property of the Control to the respective Dataset Field.

GrapeCity.ActiveReports.PageReportModel.TextBox _objTextBox = new GrapeCity.ActiveReports.PageReportModel.TextBox();  
//It is very important that Name, Size and Location properties of all controls be set  
_objTextBox.Name = "TextBox1";  
_objTextBox.Top = "0in";  
_objTextBox.Left = "0in";  
_objTextBox.Height = "0.25in";  
_objTextBox.Width = "0.25in";  
//Setting the Value property to get the text from the desired Datset Field  
_objTextBox.Value = "=Fields!CategoryID.Value";  
_objTextBox.Style.BackgroundColor = "Blue";  
//Adding the TextBox to Report Body  
\_pageReport.Report.Body.ReportItems.Add(\_objList);

Step 4: Rendering the report

Now that our report is ready we can either render it as a PDF document or we can directly assign it to a viewer to view it!

GrapeCity.ActiveReports.Document.PageDocument runtime = new GrapeCity.ActiveReports.Document.PageDocument(_pageReport);  
viewer1.LoadDocument(runtime);

And following is how the final report will look like : PageReport DownloadSample_PR_VB DownloadSample_PR_CS

MESCIUS inc.

comments powered by Disqus