Click here to Skip to main content
15,885,546 members
Articles / Web Development / ASP.NET
Tip/Trick

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 65.7K   2.5K   19   26
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

 
QuestionHow to change column text color Pin
pibas pvt5-Feb-24 23:56
pibas pvt5-Feb-24 23:56 
QuestionHow I including space one table to another table Pin
Member 1602802713-Jun-23 1:25
Member 1602802713-Jun-23 1:25 
QuestionGrouping option Pin
imamul.karim17-Feb-21 1:46
imamul.karim17-Feb-21 1:46 
QuestionDrillDown Functionality(Subreports) Pin
Oyis I31-May-20 15:04
Oyis I31-May-20 15:04 
Questionrequire windows form version to create RDLC Report dynamically Pin
Member 1415117719-Apr-20 4:32
Member 1415117719-Apr-20 4:32 
QuestionNeed a Solution Pin
Member 141752698-Mar-19 0:27
Member 141752698-Mar-19 0:27 
AnswerRe: Need a Solution Pin
Nadir Muhammed13-Mar-19 19:34
professionalNadir Muhammed13-Mar-19 19:34 
QuestionHow can I display spaces in column names in the report? Pin
mkamoski7-Dec-17 3:24
mkamoski7-Dec-17 3:24 
AnswerRe: How can I display spaces in column names in the report? Pin
mkamoski7-Dec-17 3:32
mkamoski7-Dec-17 3:32 
AnswerRe: How can I display spaces in column names in the report? Pin
Nadir Muhammed3-Feb-18 18:45
professionalNadir Muhammed3-Feb-18 18:45 
Hello Mark Kamoski,
Sorry for the late reply. You can use keyword 'as' if you are using sql.

for eg : Select customer_name as 'Customer Name' from table Customer.Its the easiest way.
- - - - -
Nadir V V

AnswerRe: How can I display spaces in column names in the report? Pin
lrhage15-Mar-18 14:56
lrhage15-Mar-18 14:56 
QuestionHow to perform grouping? Pin
mufeed k7-Mar-17 17:23
mufeed k7-Mar-17 17:23 
AnswerRe: How to perform grouping? Pin
Nadir Muhammed16-Mar-17 3:26
professionalNadir Muhammed16-Mar-17 3:26 
QuestionBlank Pin
kamashwanee3-Aug-16 22:24
kamashwanee3-Aug-16 22:24 
Questionblank report Pin
madhavi dusari27-Mar-16 19:18
madhavi dusari27-Mar-16 19:18 
AnswerRe: blank report Pin
fandrade020530-Mar-16 8:25
fandrade020530-Mar-16 8:25 
GeneralRe: blank report Pin
Nadir Muhammed19-Apr-16 20:24
professionalNadir Muhammed19-Apr-16 20:24 
GeneralRe: blank report Pin
Nadir Muhammed19-Apr-16 20:25
professionalNadir Muhammed19-Apr-16 20:25 
QuestionSource code Pin
Ahmed Nazmy20-Mar-15 20:03
Ahmed Nazmy20-Mar-15 20:03 
AnswerRe: Source code Pin
Nadir Muhammed20-Mar-15 20:09
professionalNadir Muhammed20-Mar-15 20:09 
GeneralRe: Source code Pin
Ahmed Nazmy20-Mar-15 20:40
Ahmed Nazmy20-Mar-15 20:40 
QuestionPlease add link to download your code please Pin
Member 1041007620-Mar-15 12:44
Member 1041007620-Mar-15 12:44 
AnswerRe: Please add link to download your code please Pin
Nadir Muhammed20-Mar-15 20:08
professionalNadir Muhammed20-Mar-15 20:08 
GeneralRe: Please add link to download your code please Pin
Suraj Pune8-Mar-16 1:17
Suraj Pune8-Mar-16 1:17 
GeneralRe: Please add link to download your code please Pin
Nadir Muhammed8-Mar-16 1:33
professionalNadir Muhammed8-Mar-16 1:33 

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.