Click here to Skip to main content
Licence CPOL
First Posted 28 Mar 2006
Views 337,440
Bookmarked 98 times

Binding DataSet and Generic *.rdlc Reports to a ReportViewer at Runtime

By | 25 Jul 2006 | Article
Binding DataSet and Generic *.rdlc Reports to a ReportViewer at runtime

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 article presents how to form a Report Display Component to display data within different DataSet objects that are described by a Report document (*.rdlc).

During my previous searches about displaying data within generic DataSet objects, I've encountered a set of different solutions. Most of them assumed DataSet objects are accessible during design time. Some others used TableAdapter objects that include SQL statements and/or Connection strings, etc. Probably, these solutions cover most of the situations a developer can encounter. But what if you don't have access to DataSet objects during design time, what if the overall architecture of the software doesn't let you to use SQL statements or connection strings on the architectural level you are currently working, what if you need to use data without knowing its structure until runtime, what if you need dynamic data sources rather than static?

I'm currently in need of a component that displays generic reports. My organization is developing accounting software and in accounting domain there are lots of changes in reports. After distributing and deploying the software, organization must give supporting service about newly added or changed reports. In the maintenance phase, it is costly to distribute a whole update that often. So what we need is some kind of a Report Import / Generic Report Display ability.

Before We Begin

As I've said before, in order to display a report, we need a DataSource (in this article, a populated DataSet instance) and a report document (*.rdlc file ). Once we have the Report-DataSet pair, we can display any report we want.

Logical representation of the Report Display Component

Figure 1a: Logical representation of the Report Display Component

How Do We Use DataSet as a Data Source?

A DataSet is an object that includes tabular data which can be represented by using XML format. What we need to know is that the structure of the data can be described by an XSD schema document which is also another XML based document.

DataSet <code>dsHesapPlan = new DataSet();

//...
//... Some code to populate data within the DataSet dsHesapPlan
//...

// Set the name of the DataSet which will be useful later
dsHesapPlan.DataSetName = "HESAP_PLAN";

// Can be used to write the of the DataSet
dsHesapPlan.WriteXmlSchema();

// Can be used to write the data within the DataSet as XML
dsHesapPlan.WriteXml();

Figure 1.b: Useful operations on a DataSet

Let's take a look at the schema of the DataSet, which we get by running the method dsHesapPlan.WriteXmlSchema():

Sample DataSet Schema which will be useful when designing the report.

Figure 1.c: Sample DataSet Schema which will be useful when designing the report.
Highlighted areas show naming information that will be used later.

What is a *.rdlc Report Document?

An rdlc Report Document is also another XML based document that defines how the data should be displayed. I'm not going to demonstrate how to form a report here. But there are some things you should know, so I'll form a checklist of actions to build a report.

  1. Form a schema of the DataSet which you will use (dsHesapPlan.WriteXmlSchema()).
  2. Add the schema to your Visual Studio Project. The schema will be visible within the Data Sources window.
  3. By using this schema, design your report.
  4. After you complete designing the report, save the project.
  5. Inside your project folder, you'll find the *.rdlc report document. Take it for redistribution.

Here is a part of the structure of a *.rdlc Report Document:

Sample .rdlc Report Document

Figure 1.d: Sample *.rdlc Report Document.
Highlighted areas show naming information which must show consistency with your DataSet definitions.

How to Bind a DataSet-Report Pair?

So, now we have a Report and a DataSet that can be bundled and sent to the client. Now what we need is a system to know what to do with this DataSet-Report pair.

Note that, you can send the report as the *.rdlc file itself. And DataSet can be distributed in a set of different ways. For example, executing CREATE scripts in client side to generate STORED PROCEDUREs that return the data forming the desired DataSet, or, by XML data itself, etc.

This part of the article assumes you have the properly designed Report document and the DataSet.

The following code shows how to bind a Report-DataSet pair to a ReportViewer object dynamically.

// A method to populate data inside the referenced DataSet
getDataSet(ref <code>dsHesapPlan);

// Set the path of the rdlc document
string reportPath = "Reports/AccountReport.rdlc";

// Initiate ReportViewer object
ReportViewer rView = new ReportViewer();
rView.Dock = DockStyle.Fill;
this.Controls.Add(rView);

// Add data source to the local report
// Note that the name should either be passed as parameter or 
// parsed from the report document
rView.LocalReport.DataSources
       .Add(new ReportDataSource("HESAP_PLAN_dtLast", dsHesapPlan.Tables[0]));

// Set the active report path of the ReportViewer object
rView.LocalReport.ReportPath = reportPath;

Figure 1.e: How to bind the DataSet and the generic Report dynamically.

Conclusion

This technique may be useful for developers experiencing similar problems with me. Surely, it is possible to build a real smart system to understand and display a report. I hope this article guides efforts for building such a system.

You may visit www.rdleditor.com for a series of related links.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

DIren

Web Developer

Turkey Turkey

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberbhalodiyapragnesh7:03 5 May '11  
QuestionI want to display in reportViexer1 report.rdlc data from a xml file with VB.net if possible Pinmemberelmeksoaui112:49 10 Mar '11  
NewsHave you checked out the RDLReportViewer control... Pinmemberchris17518:18 16 Apr '10  
QuestionRemote Report PinmemberAcidAndroid12:04 17 Dec '08  
AnswerRe: Remote Report PinmemberDIren20:50 17 Dec '08  
GeneralRe: Remote Report Pinmemberdg784:42 2 Sep '09  
GeneralRe: Remote Report PinmemberDIren5:36 2 Sep '09  
QuestionReport is ok but subreport PinmemberAcidAndroid8:57 12 Dec '08  
AnswerRe: Report is ok but subreport PinmemberDIren9:17 12 Dec '08  
GeneralRe: Report is ok but subreport PinmemberAcidAndroid13:32 17 Dec '08  
GeneralSome problems Pinmember_str_615:44 8 May '08  

DataRow dr;
DataTable dt = new DataTable();
dt.Columns.Add();
dt.Columns.Add();
Random rand = new Random(10000);
 
for (int i = 0; i < 100; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = rand.Next();
dt.Rows.Add(dr);
}
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.DataSetName = "ggg";
ds.WriteXmlSchema(@"c:\scheme");
ds.WriteXml(@"c:\scheme1");
string reportPath = @"C:\Documents and Settings\...\Report1.rdlc";//full path
Microsoft.Reporting.WinForms.ReportViewer rv = new Microsoft.Reporting.WinForms.ReportViewer();
rv.Dock = DockStyle.Fill;
this.Controls.Add(rv);
 
rv.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("ggg", ds.Tables[0]));
rv.LocalReport.ReportPath = reportPath;
rv.RefreshReport();

In the issue I receive an empty ReportViewer...
Help!!! May be I forget about something
GeneralRe: Some problems PinmemberDIren22:30 14 May '08  
GeneralRe: Some problems PinmemberHari Om Prakash Sharma20:30 6 Jul '08  
GeneralRe: Some problems PinmemberDIren0:55 7 Jul '08  
GeneralRe: Some problems PinmemberHari Om Prakash Sharma2:18 7 Jul '08  
GeneralRe: Some problems Pinmembersanzopaol7:51 9 Feb '11  
GeneralRe-loading the report data to the ReportViewer PinmemberAlec MacLean6:10 7 Mar '08  
QuestionHow to fill dataset derived from XSD file? PinmemberColizobble1:29 9 Oct '07  
AnswerRe: How to fill dataset derived from XSD file? PinmemberDIren3:07 9 Oct '07  
GeneralRe: How to fill dataset derived from XSD file? PinmemberColizobble3:34 9 Oct '07  
Question3d charts in rdlc files Pinmemberkriskan18:48 4 Sep '07  
AnswerRe: 3d charts in rdlc files PinmemberDIren1:45 5 Sep '07  
GeneralTransform DataSet Xml Schema to RDLC Pinmembernbohr99a15:33 16 Aug '07  
QuestionHow to bind data source in rdlc Pinmemberimran_Shaikh6:24 4 Jul '07  
AnswerRe: How to bind data source in rdlc PinmemberDIren2:00 5 Jul '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 25 Jul 2006
Article Copyright 2006 by DIren
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid