Dynamically Creating an RDLC Report Just Using a DataSet - CodeProject



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.


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.

public void DataBind(DataSet ds)
  int count = 0;
  foreach (DataTable dt in ds.Tables)
   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
 for (int i = 0; i < ds.Tables.Count; i++)
      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, 
 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.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?

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

  DataTable table1 = new DataTable("patients");
  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();

  //Report Binding

  rpt_daily.ReportTitle = "Wastage Report";
  rpt_daily.ReportName = "WastageReport";
  rpt_daily.Visible = true;