Skip to main content Skip to footer

Creating ActiveReports on the Fly

The extensive API of ActiveReports 7 allows Visual Basic and C# developers to completely control the report processing engine to fit their needs. It provides control over the following Events and Properties :

  • Data Retrieval
  • Data Transformation
  • Layout
  • Rendering

The API gives the developers complete control to create their reports from scratch which essentially means creating a report all together at runtime. Creating reports at runtime is a very common feature if we talk about a good reporting tool. This time I would like to write a blog for the customers who want to design a SectionReport dynamically and further bind it to a data source. Before moving further for the implementation, let us take a look how the resulting report will look: Results The simple implementation will include the following steps:

  • Creating a report instance
  • Adding sections and controls dynamically
  • Modifying the look and feel of the report sections and controls
  • Setting up a data source to assign to the report
  • Binding the report to the data source

Let us see what code is required to accomplish this:

private void Form1_Load(object sender, EventArgs e)  
{  
   rpt = new SectionReport();  

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

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

   //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 label to display second column's name  
   GrapeCity.ActiveReports.SectionReportModel.Label lblCategoryName = new GrapeCity.ActiveReports.SectionReportModel.Label();  
   lblCategoryName.Location = new PointF(1.459f, 0.05f);  
   lblCategoryName.Size = new SizeF(1.094f,0.2f);  
   lblCategoryName.Text = "Category Name";  
   lblCategoryName.Font = new System.Drawing.Font("Arial", 10, FontStyle.Bold);  
   rpt.Sections[0].Controls.Add(lblCategoryName);  

   //Adding label to display third column's name  
   GrapeCity.ActiveReports.SectionReportModel.Label lblDescription = new GrapeCity.ActiveReports.SectionReportModel.Label();  
   lblDescription.Location = new PointF(3.114f, 0.05f);  
   lblDescription.Text = "Description";  
   lblDescription.Font = new System.Drawing.Font("Arial", 10, FontStyle.Bold);  
   rpt.Sections[0].Controls.Add(lblDescription);  

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

   //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);  

   //Adding Textbox to display second column's records  
   GrapeCity.ActiveReports.SectionReportModel.TextBox txtCategoryName = new GrapeCity.ActiveReports.SectionReportModel.TextBox();  
   txtCategoryName.Location = new PointF(1.459f,0);  
   rpt.Sections[1].Controls.Add(txtCategoryName);  

   //Adding Textbox to display third column's records  
   GrapeCity.ActiveReports.SectionReportModel.TextBox txtDescription = new GrapeCity.ActiveReports.SectionReportModel.TextBox();  
   txtDescription.Location = new PointF(3.114f,0);  
   rpt.Sections[1].Controls.Add(txtDescription);  

   //Adding Picture control to display image  
   GrapeCity.ActiveReports.SectionReportModel.Picture picture = new Picture();  
   picture.Location = new PointF(5.219f,0);  
   rpt.Sections[1].Controls.Add(picture);  

   // Setting report's data source  
   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;  

   // Assigning DataField properties of controls in the detail section  
   txtCategoryID.DataField = "CategoryID";  
   txtCategoryName.DataField = "CategoryName";  
   txtDescription.DataField = "Description";  
   picture.DataField = "Picture";  
   reader.Close();  
   conn.Close();  
}

Samples implementing the above functionality can be downloaded in both C# and VB.NET from the following links: Download Sample-C# Download Sample-VB

MESCIUS inc.

comments powered by Disqus