Dynamic Control RDLC Report custom RDLC in ASP.

Source: Internet
Author: User

Reprinted from: http://dlwang2002.cnblogs.com/archive/2006/05/27/410499.html

In the ASP, you can choose to use Crystal Reports, which is really powerful. But the web version of the Crystal Report seems to have a copyright issue.
If the report is not a complicated mess, you can use the RDLC report that comes with Microsoft.

There have been a lot of interpretations made by the man:http://www.cnblogs.com/waxdoll/
more information can be found here:http://www.gotreportviewer.com/

RDLC Benefits:
1:RDLC Report Design is simple
2: Results are stored as XML, easy to control
3: Export format is very good

in this case, the dynamic control report refers to: In some cases, after the production of the report you want to be able to do some dynamic changes in the run, such as column location, user control display those columns and so on.

control methods, try out a few of these:
1: Control the properties of the report object provided by Microsoft;
2: All reports are automatically generated
3: Modify the report source file, and then load.

control the properties of the report object provided by Microsoft: Based on this feature requirement, the first way I think about this is by controlling the properties of these report objects provided by Microsoft. Because this approach is the most humane. But it backfired, Microsoft's ReportViewer object is used to display the report's, natural not, I use the paper is my own design, LocalReport, found the object of the project, the method has this several: "Report." Getdefaultpagesettings (); Getdocumentmap () and so on, the first is to get the printer paper Zhang settings, the second is to get the doc document (but always error), are read-only properties;

The second method is to generate all reports automatically. A complete example can be found here:http://www.gotreportviewer.com/Dynamictable.zip
In This example, he writes the RDLC report of the XML structure to a class reportdefinition, and then obtains a report by customizing the contents of the class. In fact, it is to construct a report object's XML itself. This is the process of loading a custom report: The code under win

This. Reportviewer1.reset ();
This. ReportViewer1.LocalReport.LoadReportDefinition (M_RDL);
This. REPORTVIEWER1.LOCALREPORT.DATASOURCES.ADD (NewReportDataSource ("MyData", M_dataset.tables[0]));
This. Reportviewer1.refreshreport ();

This is the code that generates the XML automatically:

PrivateMemoryStream Generaterdl (list<string> AllFields, list<string> Selectedfields)
{
MemoryStream ms = new MemoryStream ();
Rdlgenerator Gen = New rdlgenerator ();
Gen. AllFields = AllFields;
Gen. Selectedfields = Selectedfields;
Gen. WRITEXML (MS);
Ms. Position = 0;
return MS;
}

This is part of the complete reportdefinition definition:

namespaceRdl{
usingSystem.Xml.Serialization;


// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute ("xsd", "2.0.50727.42")]
[System.SerializableAttribute ()]
[System.Diagnostics.DebuggerStepThroughAttribute ()]
[System.ComponentModel.DesignerCategoryAttribute ("code")]
[System.Xml.Serialization.XmlTypeAttribute (anonymoustype=true)]
[System.Xml.Serialization.XmlRootAttribute (namespace= "http://schemas.microsoft.com/sqlserver/reporting/2005/01 /reportdefinition ", isnullable=false)]
PublicPartialclassReport{

Private Object [] Itemsfield;

But after several considerations, this program is not satisfactory, the reason is: all the report objects have to generate their own, all of a sudden back to the liberation, no visual tools design is cumbersome and complex. Especially if you design a few line, then a few groupings, the workload is huge.

So try the third method: Reportvivwer the report source file in memory before loading the report. This method is more ruthless, in fact, can solve a lot of problems, including the design of customized printing paper, etc. (here is another way to set the printing paper http://waxdoll.cnblogs.com/archive/2006/03/03/342435.html).
The design idea is to load the rdlc file into a XmlDocument object first, then modify the XML content, and serialize the XML into a byte stream and give it to the ReportViewer Display.
This is the code for this section:

PublicMemoryStream GENERATERDLC ()
{
XmlDocument SourceDoc =NewXmlDocument ();
stringPath = AppDomain.CurrentDomain.BaseDirectory + "TEST/ORDERLIST.RDLC";
Sourcedoc.load (path);
Hashtable reportcolumns = Getreportcolumns (sourcedoc.lastchild);
//just remove
for(inti = 0; i < Reportcolumns.count; i++)
{
if (! Findreportcoulmns (Reportcolumns[i]. ToString ()))
{
REMOVECOLUMNFROMRDLC (Sourcedoc.lastchild, i);
}
}

MemoryStream ms =NewMemoryStream ();
XmlSerializer serializer =NewXmlSerializer (typeof(XmlDocument));
Serializer. Serialize (MS, SourceDoc);
Ms. Position = 0;
returnMs
}

As for how to Getreportcolumns and REMOVECOLUMNFROMRDLC, it is very simple, it is a process of manipulating XML objects. Say

PrivateHashtable getreportcolumns (XmlNode root)
{
Hashtable cols =NewHashtable ();
//XmlNamespaceManager s=new XmlNamespaceManager (
XmlNode cells = Findchildnode (root, "body/reportitems/table/header/tablerows/tablerow/tablecells");
for(inti = 0; I < cells. Childnodes.count; i++)
{
XmlNode cell =findchildnode (cells. Childnodes[i], "reportitems/textbox/dataelementname");
Cols[i] = cell. InnerText;
}
returnCols
}


This is the code that uses this paragraph

This. ReportViewer1.LocalReport.LoadReportDefinition ( This. REPORT.GENERATERDLC ());
This. REPORTVIEWER1.LOCALREPORT.DATASOURCES.ADD (NewReportDataSource ("DataSet1", result. Tables[0]));
This. ReportViewer1.LocalReport.Refresh ();


This method has finally succeeded.
Attached: XML section structure of the RDLC file


1<?xml version= "1.0" encoding= "Utf-8"?>
2<report xmlns= "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd= "http// Schemas.microsoft.com/sqlserver/reporting/reportdesigner ">
3<DataSources>
4<datasource name= "ConnectionString" >
5<ConnectionProperties>
6<connectstring/>
7<DataProvider>SQL</DataProvider>
8</ConnectionProperties>
9<rd:DataSourceID>073016a7-6cb0-4e06-a6fd-f5882a039188</rd:DataSourceID>
Ten</DataSource>
One</DataSources>
A<BottomMargin>2.5cm</BottomMargin>
-<RightMargin>2.5cm</RightMargin>
-<PageWidth>21cm</PageWidth>
the<rd:DrawGrid>true</rd:DrawGrid>
-<InteractiveWidth>21cm</InteractiveWidth>
-<rd:GridSpacing>0.25cm</rd:GridSpacing>
-<rd:SnapToGrid>true</rd:SnapToGrid>
+<Body>
-<ColumnSpacing>1cm</ColumnSpacing>
+<ReportItems>
A<chart name= "Chart1" >



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.