Asp. Dynamic Control RDLC Report in net

Source: Internet
Author: User
Tags count modify serialization xmlns
asp.net| Dynamic | Control 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<?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>
<BottomMargin> 2.5cm </BottomMargin>
<RightMargin> 2.5cm </RightMargin>
<PageWidth> 21cm </PageWidth>
<rd:DrawGrid> true </rd:DrawGrid>
<InteractiveWidth> 21cm </InteractiveWidth>
<rd:GridSpacing> 0.25cm </rd:GridSpacing>
<rd:SnapToGrid> true </rd:SnapToGrid>
<Body>
<ColumnSpacing> 1cm </ColumnSpacing>
<ReportItems>
<chart name= "Chart1"

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.