Dynamic Control of rdlc reports in ASP. NET

Source: Internet
Author: User

In Asp.net Program . 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: Under WinCodeThis. 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.