Asp. NET in Dynamic control RDLC Report _ Practical Skills

Source: Internet
Author: User
Tags serialization xmlns
In the ASP.net program, you can choose to use the Crystal Report, the function is really powerful. But the web version of the Crystal Report seems to have a copyright problem. If the report is not a complex mess, you can use Microsoft's own RDLC report.

RDLC Advantages:

1:RDLC Report Design is simple

2: The results are stored as XML, easy to control

3: The export format is very good.

The dynamic control report here refers to the following: In some cases, after making the report, you want to make some small changes in the running, such as column position, 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 Microsoft-provided report objects: Based on this functional requirement, the approach I thought of at the outset was achieved by controlling the properties of these report objects provided by Microsoft. Because this method is the most humane. But to the contrary, Microsoft's ReportViewer object is used to display the show, naturally not; I use the "the" Is my own design, LocalReport, find the project, the method has this several: a. Getdefaultpagesettings (); Getdocumentmap (), and so on, the first is to get the paper Zhang settings, the second is to get Doc documents (but always error), are read-only properties; So, one attempt failed.

The second method is that the report is all generated automatically. Can find a complete example here: Http://www.gotreportviewer.com/DynamicTable.zip
In this example, he writes the RDLC report of the XML structure as a class reportdefinition and then gets a report by customizing the contents of the class. It is actually the XML that constructs a report object for itself. This is the process of loading a custom report: Win code This.reportViewer1.Reset ();

This.reportViewer1.LocalReport.LoadReportDefinition (M_RDL);
THIS.REPORTVIEWER1.LOCALREPORT.DATASOURCES.ADD (New ReportDataSource ("MyData", m_dataset.tables[0));
This.reportViewer1.RefreshReport (); This is the code that automatically generates XML:
Private MemoryStream 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:

Namespace Rdl {
Using System.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)]
public partial class
Private object[] Itemsfield;


But after several considerations, this program is not satisfactory, because: all the report objects have to generate their own, suddenly returned to the liberation, there is no visual tool design is cumbersome and complex. Especially if you design a few line, and then come to a few groups, the workload is huge.

So try a third way: Reportvivwer to modify 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. (there is another way to set the printing paper http://waxdoll.cnblogs.com/archive/2006/03/03/342435.html).

The idea is to load the rdlc file to a XmlDocument object first, then modify the XML content, serialize the XML into a byte stream, and give the ReportViewer display.

This is the section code:

Public MemoryStream GENERATERDLC ()
{
XmlDocument sourcedoc = new XmlDocument ();
String path = AppDomain.CurrentDomain.BaseDirectory + "TEST/ORDERLIST.RDLC";
Sourcedoc.load (path);
Hashtable reportcolumns = Getreportcolumns (sourcedoc.lastchild);
Just remove
for (int i = 0; I reportcolumns.count i++)
{
if (! Findreportcoulmns (Reportcolumns[i]. ToString ()))
{
REMOVECOLUMNFROMRDLC (Sourcedoc.lastchild, i);
}
}

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

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

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

This is the code that uses this section:

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

This method has finally succeeded.

Attachment: XML section structure of RDLC file

XML Structure

1
2
3
4
5
6
7 SQL
8
9 073016a7-6cb0-4e06-a6fd-f5882a039188
Ten
One
2.5cm
2.5cm
(21cm )
true
21cm
0.25cm
of true
20
1cm

Related Article

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.