Click here to Skip to main content
15,506,025 members
Articles / Web Development / ASP.NET
Tip/Trick
Posted 19 Mar 2015

Stats

57.6K views
2.4K downloads
19 bookmarked

Dynamically Creating an RDLC Report Just Using a DataSet

Rate me:
Please Sign up or sign in to vote.
3.83/5 (4 votes)
19 Mar 2015CPOL1 min read
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India

Comments and Discussions

 
QuestionGrouping option Pin
imamul.karim17-Feb-21 2:46
imamul.karim17-Feb-21 2:46 
QuestionDrillDown Functionality(Subreports) Pin
Oyis I31-May-20 16:04
Oyis I31-May-20 16:04 
Questionrequire windows form version to create RDLC Report dynamically Pin
Member 1415117719-Apr-20 5:32
Member 1415117719-Apr-20 5:32 
QuestionNeed a Solution Pin
Member 141752698-Mar-19 1:27
Member 141752698-Mar-19 1:27 
AnswerRe: Need a Solution Pin
Nadir Muhammed13-Mar-19 20:34
professionalNadir Muhammed13-Mar-19 20:34 
QuestionHow can I display spaces in column names in the report? Pin
mkamoski7-Dec-17 4:24
mkamoski7-Dec-17 4:24 
AnswerRe: How can I display spaces in column names in the report? Pin
mkamoski7-Dec-17 4:32
mkamoski7-Dec-17 4:32 
AnswerRe: How can I display spaces in column names in the report? Pin
Nadir Muhammed3-Feb-18 19:45
professionalNadir Muhammed3-Feb-18 19:45 
AnswerRe: How can I display spaces in column names in the report? Pin
lrhage15-Mar-18 15:56
lrhage15-Mar-18 15:56 
QuestionHow to perform grouping? Pin
mufeed k7-Mar-17 18:23
mufeed k7-Mar-17 18:23 
AnswerRe: How to perform grouping? Pin
Nadir Muhammed16-Mar-17 4:26
professionalNadir Muhammed16-Mar-17 4:26 
QuestionBlank Pin
kamashwanee3-Aug-16 23:24
kamashwanee3-Aug-16 23:24 
Questionblank report Pin
madhavi dusari27-Mar-16 20:18
madhavi dusari27-Mar-16 20:18 
AnswerRe: blank report Pin
fandrade020530-Mar-16 9:25
fandrade020530-Mar-16 9:25 
GeneralRe: blank report Pin
Nadir Muhammed19-Apr-16 21:24
professionalNadir Muhammed19-Apr-16 21:24 
GeneralRe: blank report Pin
Nadir Muhammed19-Apr-16 21:25
professionalNadir Muhammed19-Apr-16 21:25 
QuestionSource code Pin
Ahmed Nazmy20-Mar-15 21:03
Ahmed Nazmy20-Mar-15 21:03 
AnswerRe: Source code Pin
Nadir Muhammed20-Mar-15 21:09
professionalNadir Muhammed20-Mar-15 21:09 
GeneralRe: Source code Pin
Ahmed Nazmy20-Mar-15 21:40
Ahmed Nazmy20-Mar-15 21:40 
QuestionPlease add link to download your code please Pin
Member 1041007620-Mar-15 13:44
Member 1041007620-Mar-15 13:44 
AnswerRe: Please add link to download your code please Pin
Nadir Muhammed20-Mar-15 21:08
professionalNadir Muhammed20-Mar-15 21:08 
GeneralRe: Please add link to download your code please Pin
Suraj Pune8-Mar-16 2:17
Suraj Pune8-Mar-16 2:17 
GeneralRe: Please add link to download your code please Pin
Nadir Muhammed8-Mar-16 2:33
professionalNadir Muhammed8-Mar-16 2:33 
GeneralRe: Please add link to download your code please Pin
Suraj Pune8-Mar-16 3:41
Suraj Pune8-Mar-16 3:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.