ActiveReports has been the first choice of developers when it comes to creating hassle free and versatile reports. With the addition of the concept of PageReports, where layout is the priority, ActiveReports took a step further in simplifying any reporting need of an organization. Creating custom reports at runtime has been a very common requirement of many developers. Earlier, in this blog, we talked about creating SectionReports dynamically from scratch. This time, I would like to discuss how the same requirement can be accomplished for PageReports. The basic steps in creating a report at runtime would involve:
- Creating a Report Instance
- Binding the Report to a Data Source
- Adding Controls Dynamically and Binding them to DataSet Fields
- Rendering the Report
Step 1: Creating a Report Instance We start with creating a PageReport instance as follows:
GrapeCity.ActiveReport.PageReport _pageReport = new GrapeCity.ActiveReport.PageReport()
Step 2: Creating and Binding the Report to a Database a) Setting up a connection to a DataSource is the first step in binding a report to data. Once a connection is established, a DataSet is required to get the data you want to show on 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;
b) We will explicitly add fields to 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);
c) Now that our DataSource and DataSet objects are defined, we have to add them to the DataSource and DataSet Collections.
Step 3: Adding Controls Dynamically and Binding them to Report Fields The catch in creating a PageReport at runtime is accessing its properties and controls from code. The Report element of a PageReport object 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. We will use this property and it's ReportItemCollection to add controls to the report.
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"; _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);