Dynamic Control of RDLC reports in ASP. NET

Source: Internet
Author: User

In the asp.net program, you can choose to use the Crystal Report, which is indeed powerful. However, the web-based Crystal reports seem to have copyright issues. If the report is not a complex mess, you can use the Rdlc report that comes with Microsoft.

Advantages of Rdlc:

1: simple Rdlc report Design

2: The result is saved in xml, which is easy to control.

3: The export format is good.

The dynamic control report mentioned here refers to the following: in some cases, after a report is created, you want to make some small modifications dynamically during running, such as the column position, you can control the columns displayed.

The following methods have been used:

1: controls the attributes of report objects provided by Microsoft;

2: All reports are automatically generated.

3: Modify the report source file and load it.

Control the attributes of report objects provided by Microsoft: Based on this functional requirement, I first thought of how to control the attributes of these report objects provided by Microsoft. This method is the most humane. In contrast, Microsoft's ReportViewer object is used to display reports. Naturally, it cannot be used. The Report I use is designed by myself. localReport is used to find the report object. There are several methods in it: Report. getdefapagpagesettings (); report. getDocumentMap () and so on. The first one is to get the printing script settings, and the second is to get the doc document (but there is always an error), which is a read-only attribute. Therefore, the first attempt fails.

The second method is to automatically generate all reports. You can find a complete example here: http://www.gotreportviewer.com/DynamicTable.zip
In this example, he writes the rdlc report in the xml structure into a class ReportDefinition, and then obtains a report by customizing the content of this class. It is actually used to construct the xml of a report object for 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. CES. Add (new ReportDataSource ("MyData", m_dataSet.Tables [0]);
This. reportViewer1.RefreshReport (); this is the code for Automatically Generating 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 full 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 Report {
Private object [] itemsField;

However, after several considerations, this solution was not satisfactory because all report objects had to be generated by themselves. Before the liberation, the design without visualization tools was cumbersome and complex. Especially if several lines are designed and then several groups are added, the workload is huge.

Therefore, try the third method: Modify the report source file in the memory before the ReportVivwer loads the report. This method is relatively harsh, in fact, can solve a lot of problems, including the design of custom printing paper and so on (here there is another way to set printing paper http://waxdoll.cnblogs.com/archive/2006/03/03/342435.html ).

The design idea is: first load the rdlc file to an XmlDocument object, then modify the xml content, serialize the xml into word throttling, and hand it to ReportViewer for display.

Here is the 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, it is a process of operating xml objects. For example:

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;
}
Here is the code that uses this section:

This. ReportViewer1.LocalReport. LoadReportDefinition (this. Report. GenerateRdlc ());
This. ReportViewer1.LocalReport. CES. Add (new ReportDataSource ("DataSet1", result. Tables [0]);
This. ReportViewer1.LocalReport. Refresh ();
This method has finally succeeded.

Appendix: xml section structure of the 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: performanceid> 073016a7-6cb0-4e06-a6fd-f5882a039188 </rd: performanceid>
10 </DataSource>
11 </DataSources>
12 <BottomMargin> 2.5 </BottomMargin>
13 <RightMargin> 2.5 </RightMargin>
14 <PageWidth> 21 cm </PageWidth>
15 <rd: DrawGrid> true </rd: DrawGrid>
16 <InteractiveWidth> 21 cm </InteractiveWidth>
17 <rd: GridSpacing> 0.25 </rd: GridSpacing>
18 <rd: SnapToGrid> true </rd: SnapToGrid>
19 <Body>
20 <ColumnSpacing> 1 cm </ColumnSpacing>
21 <ReportItems>
22 <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.