Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Dynamically Creating an RDLC Report Just Using a DataSet

0.00/5 (No votes)
19 Mar 2015 1  
Creating an RDLC report by just binding a Dataset

Introduction

In order to display a report by using ReportViewer Control, we need a DataSource that contains the data to be shown and a Report document that describes how that data should be displayed.

This tip presents how to create a Report dynamically using a DataSet in Report document (*.rdlc). and show the report without much effort.

During my previous searches about displaying data, we need to create the report and bind the dataset. Here, I am providing a way to generate a report in RDLC by just passing the DataSet.

I'm currently in need of a component that displays reports easily because these reports can be easily implemented for the System Maintanance table and because you will not be spending much time on these System Maintanace Reports.

Background

To use this report, you don't need to know RDLC, just a beginner or novice can use this Report Builder.

Using the Code

For using this in your code:

First, you need to copy the ReportBuilder.cs and ReportBuilderEngine.cs to your project file. These files are used to Generate Reports from your Dataset.

Secondly, you need to copy the UserControl folder to your solution. It contains the report_viewer.ascx file which is actually generating the report. If you need to change any footer details, you can change in the report_viewer.ascx.

C#
public void DataBind(DataSet ds)
  {
  int count = 0;
  foreach (DataTable dt in ds.Tables)
  {
   count++;
   var report_name = "Report" + count;
   DataTable dt1 = new DataTable(report_name.ToString());
   dt1 = ds.Tables[count - 1];
   dt1.TableName = report_name.ToString();
  }

  //Report Viewer, Builder and Engine
       
 ReportViewer1.Reset();
 for (int i = 0; i < ds.Tables.Count; i++)
   ReportViewer1.LocalReport.DataSources.Add(
      new ReportDataSource(ds.Tables[i].TableName,ds.Tables[i]));

 ReportBuilder reportBuilder = new ReportBuilder();
 reportBuilder.DataSource = ds;
 reportBuilder.Page = new ReportPage();
 ReportSections reportFooter = new ReportSections();
 ReportItems reportFooterItems = new ReportItems();
 ReportTextBoxControl[] footerTxt = new ReportTextBoxControl[3];
 string footer = string.Format
 ("Copyright  {0}         Report Generated On  {1}          Page  {2}  of {3} ", 
 DateTime.Now.Year, DateTime.Now, ReportGlobalParameters.CurrentPageNumber, 
ReportGlobalParameters.TotalPages);
 footerTxt[0] = new ReportTextBoxControl() 
 { Name = "txtCopyright", ValueOrExpression = new string[] { footer } }
 reportFooterItems.TextBoxControls = footerTxt;
 reportFooter.ReportControlItems = reportFooterItems;
 reportBuilder.Page.ReportFooter = reportFooter;
 ReportSections reportHeader = new ReportSections();
 reportHeader.Size = new ReportScale();
 reportHeader.Size.Height = 0.56849;
 ReportItems reportHeaderItems = new ReportItems();
 ReportTextBoxControl[] headerTxt = new ReportTextBoxControl[1];
 headerTxt[0] = new ReportTextBoxControl() { Name = "txtReportTitle", 
 ValueOrExpression = new string[] { "Report Name: "+ReportTitle } };
 reportHeaderItems.TextBoxControls = headerTxt;
 reportHeader.ReportControlItems = reportHeaderItems;
 reportBuilder.Page.ReportHeader = reportHeader;
 ReportViewer1.LocalReport.LoadReportDefinition(ReportEngine.GenerateReport(reportBuilder));
 ReportViewer1.LocalReport.DisplayName = ReportName;
 }

You can change the footer contents in line that starts from footerTxt.

Thirdly, you need to copy the img folder to the solution and replace the logo.png with your logo. You need to rename your logo to "logo.png".

How to Bind a DataSet-With the Report?

C#
public void ReportBinding()
{
  //Data for binding to the Report

  DataTable table1 = new DataTable("patients");
  table1.Columns.Add("Name");
  table1.Columns.Add("State");
  table1.Columns.Add("Country");
  table1.Columns.Add("CurrentResidence");
  table1.Columns.Add("MaritalStatus");
  table1.Columns.Add("EmploymentStatus");
  table1.Rows.Add("Nadir", "Kerala", "India", 
  "Bangalore", "Single","Is SelfEmployed");
  table1.Rows.Add("Lijo", "Kerala", "India", 
  "Philipines", "Single", "Is Salaried");
  table1.Rows.Add("Shelley", "Kerala", "India", 
  "Kashmir", "Married", "Is SelfEmployed");
  DataSet ds = new DataSet();
  ds.Tables.Add(table1);

  //Report Binding

  rpt_daily.ReportTitle = "Wastage Report";
  rpt_daily.ReportName = "WastageReport";
  rpt_daily.DataBind(ds);
  rpt_daily.Visible = true; 
}

History

  • Demo Version 1.0

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here