本文转自:http://www.cnblogs.com/pszw/archive/2012/07/19/2599937.html

前言

最近接到一个需求:在给定的数据源中,某(些)列,可能需要单独统计,是否单独统计需要根据报表配置来决定。由于项目中一直使用RDLC来生成报表,临时为了一个需求换一种技术也不是很现实,所以自己捉摸了下。

认识RDLC

RDLC的主要有三个部分:

(1)*.rdlc文件,本质是一个XML文件,这里定义了报表样式;

(2)*.xsd文件,也是一个XML文件,这里定义了数据源格式;

(3)*.aspx文件,呈现报表的web页面。

注:RDLC是什么,可参考蜡人张的博客:http://www.cnblogs.com/waxdoll/archive/2006/02/25/337713.html

如何实现动态

(1)LocalReport对象提供了方法LoadReportDefinition(Stream stream)和属性ReportPath保证了我们不仅可以从流中读取文件,也可以指定本地文件路径加载rdlc文件;

(2).rdlc,.xsd都是xml文件,可使用XmlDocument进行读写操作。

实例

下面实现一个学生成绩统计报表为例,介绍如何实现动态列。

第一步 准备工作

新建空web项目->添加xsd文件,创建一个table(文件名Students.xsd)->添加rdlc文件,与xsd的table关联,并绑定相关字段(文件名FirstRdlc.rdlc)。

Students.xsd设计器视图:

对应的xml文件:

  1.  View Source<?xml version="1.0" encoding="utf-8"?> <xs:schema id="Students" targetNamespace="http://tempuri.org/Students.xsd" xmlns:mstns="http://tempuri.org/Students.xsd" xmlns="http://tempuri.org/Students.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" attributeFormDefault="qualified" elementFormDefault="qualified">   <xs:annotation>     <xs:appinfo source="urn:schemas-microsoft-com:xml-msdatasource">       <DataSource DefaultConnectionIndex="0" FunctionsComponentName="QueriesTableAdapter" Modifier="AutoLayout, AnsiClass, Class, Public" SchemaSerializationMode="IncludeSchema" xmlns="urn:schemas-microsoft-com:xml-msdatasource">         <Connections />         <Tables />         <Sources />       </DataSource>     </xs:appinfo>   </xs:annotation>   <xs:element name="Students" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msprop:EnableTableAdapterManager="true" msprop:Generator_DataSetName="Students" msprop:Generator_UserDSName="Students">     <xs:complexType>       <xs:choice minOccurs="0" maxOccurs="unbounded">         <xs:element name="dtStudent" msprop:Generator_TableClassName="dtStudentDataTable" msprop:Generator_TableVarName="tabledtStudent" msprop:Generator_TablePropName="dtStudent" msprop:Generator_RowDeletingName="dtStudentRowDeleting" msprop:Generator_UserTableName="dtStudent" msprop:Generator_RowChangingName="dtStudentRowChanging" msprop:Generator_RowEvHandlerName="dtStudentRowChangeEventHandler" msprop:Generator_RowDeletedName="dtStudentRowDeleted" msprop:Generator_RowEvArgName="dtStudentRowChangeEvent" msprop:Generator_RowChangedName="dtStudentRowChanged" msprop:Generator_RowClassName="dtStudentRow">           <xs:complexType>             <xs:sequence>               <xs:element name="Name" msprop:Generator_ColumnVarNameInTable="columnName" msprop:Generator_ColumnPropNameInRow="Name" msprop:Generator_ColumnPropNameInTable="NameColumn" msprop:Generator_UserColumnName="Name" type="xs:string" minOccurs="0" />               <xs:element name="Age" msprop:Generator_ColumnVarNameInTable="columnAge" msprop:Generator_ColumnPropNameInRow="Age" msprop:Generator_ColumnPropNameInTable="AgeColumn" msprop:Generator_UserColumnName="Age" type="xs:string" minOccurs="0" />               <xs:element name="Scores" msprop:Generator_ColumnVarNameInTable="columnScores" msprop:Generator_ColumnPropNameInRow="Scores" msprop:Generator_ColumnPropNameInTable="ScoresColumn" msprop:Generator_UserColumnName="Scores" type="xs:string" minOccurs="0" />               <xs:element name="RecId" msprop:Generator_ColumnVarNameInTable="columnRecId" msprop:Generator_ColumnPropNameInRow="RecId" msprop:Generator_ColumnPropNameInTable="RecIdColumn" msprop:Generator_UserColumnName="RecId" type="xs:string" />               <xs:element name="Class" msprop:Generator_ColumnVarNameInTable="columnClass" msprop:Generator_ColumnPropNameInRow="Class" msprop:Generator_ColumnPropNameInTable="ClassColumn" msprop:Generator_UserColumnName="Class" type="xs:string" minOccurs="0" />             </xs:sequence>           </xs:complexType>         </xs:element>       </xs:choice>     </xs:complexType>     <xs:unique name="dtStudentKey1" msdata:PrimaryKey="true">       <xs:selector xpath=".//mstns:dtStudent" />       <xs:field xpath="mstns:RecId" />     </xs:unique>   </xs:element> </xs:schema>

观察这段xml,会注意这段代码:

  1. <xs:element name="Class" msprop:Generator_ColumnVarNameInTable="columnClass" msprop:Generator_ColumnPropNameInRow="Class" msprop:Generator_ColumnPropNameInTable="ClassColumn" msprop:Generator_UserColumnName="Class" type="xs:string" minOccurs="0" />

这句代码定义了报表数据源的“Class”这列。可想而知,我们如果动态添加一列,这里势必应该要修改。

FirstRdlc.rdlc文件设计器视图:

对应的xml文件如下:

  1.  View Source<?xml version="1.0" encoding="utf-8"?> <Report xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner" xmlns="http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition">   <DataSources>     <DataSource Name="Students">       <ConnectionProperties>         <DataProvider>System.Data.DataSet</DataProvider>         <ConnectString>/* Local Connection */</ConnectString>       </ConnectionProperties>       <rd:DataSourceID>9a61684e-8669-405d-b9ef-dc43279c6ff0</rd:DataSourceID>     </DataSource>   </DataSources>   <DataSets>     <DataSet Name="dsStudent">       <Fields>         <Field Name="Name">           <DataField>Name</DataField>           <rd:TypeName>System.String</rd:TypeName>         </Field>         <Field Name="Age">           <DataField>Age</DataField>           <rd:TypeName>System.String</rd:TypeName>         </Field>         <Field Name="Scores">           <DataField>Scores</DataField>           <rd:TypeName>System.String</rd:TypeName>         </Field>         <Field Name="RecId">           <DataField>RecId</DataField>           <rd:TypeName>System.String</rd:TypeName>         </Field>         <Field Name="Class">           <DataField>Class</DataField>           <rd:TypeName>System.String</rd:TypeName>         </Field>       </Fields>       <Query>         <DataSourceName>Students</DataSourceName>         <CommandText>/* Local Query */</CommandText>       </Query>       <rd:DataSetInfo>         <rd:DataSetName>Students</rd:DataSetName>         <rd:SchemaPath>E:\Demo\HelloRDLC\HelloRDLC\Students.xsd</rd:SchemaPath>         <rd:TableName>dtStudent</rd:TableName>         <rd:TableAdapterFillMethod />         <rd:TableAdapterGetDataMethod />         <rd:TableAdapterName />       </rd:DataSetInfo>     </DataSet>   </DataSets>   <Body>     <ReportItems>       <Tablix Name="Tablix5">         <TablixBody>           <TablixColumns>             <TablixColumn>               <Width>0.98425in</Width>             </TablixColumn>             <TablixColumn>               <Width>0.98425in</Width>             </TablixColumn>             <TablixColumn>               <Width>0.98425in</Width>             </TablixColumn>             <TablixColumn>               <Width>0.98425in</Width>             </TablixColumn>             <TablixColumn>               <Width>0.98425in</Width>             </TablixColumn>           </TablixColumns>           <TablixRows>             <TablixRow>               <Height>0.23622in</Height>               <TablixCells>                 <TablixCell>                   <CellContents>                     <Textbox Name="Textbox25">                       <CanGrow>true</CanGrow>                       <KeepTogether>true</KeepTogether>                       <Paragraphs>                         <Paragraph>                           <TextRuns>                             <TextRun>                               <Value>序号</Value>                               <Style />                             </TextRun>                           </TextRuns>                           <Style />                         </Paragraph>                       </Paragraphs>                       <rd:DefaultName>Textbox25</rd:DefaultName>                       <Style>                         <Border>                           <Color>LightGrey</Color>                           <Style>Solid</Style>                         </Border>                         <PaddingLeft>2pt</PaddingLeft>                         <PaddingRight>2pt</PaddingRight>                         <PaddingTop>2pt</PaddingTop>                         <PaddingBottom>2pt</PaddingBottom>                       </Style>                     </Textbox>                   </CellContents>                 </TablixCell>                 <TablixCell>                   <CellContents>                     <Textbox Name="Textbox27">                       <CanGrow>true</CanGrow>                       <KeepTogether>true</KeepTogether>                       <Paragraphs>                         <Paragraph>                           <TextRuns>                             <TextRun>                               <Value>姓名</Value>                               <Style />                             </TextRun>                           </TextRuns>                           <Style />                         </Paragraph>                       </Paragraphs>                       <rd:DefaultName>Textbox27</rd:DefaultName>                       <Style>                         <Border>                           <Color>LightGrey</Color>                           <Style>Solid</Style>                         </Border>                         <PaddingLeft>2pt</PaddingLeft>                         <PaddingRight>2pt</PaddingRight>                         <PaddingTop>2pt</PaddingTop>                         <PaddingBottom>2pt</PaddingBottom>                       </Style>                     </Textbox>                   </CellContents>                 </TablixCell>                 <TablixCell>                   <CellContents>                     <Textbox Name="Textbox29">                       <CanGrow>true</CanGrow>                       <KeepTogether>true</KeepTogether>                       <Paragraphs>                         <Paragraph>                           <TextRuns>                             <TextRun>                               <Value>年龄</Value>                               <Style />                             </TextRun>                           </TextRuns>                           <Style />                         </Paragraph>                       </Paragraphs>                       <rd:DefaultName>Textbox29</rd:DefaultName>                       <Style>                         <Border>                           <Color>LightGrey</Color>                           <Style>Solid</Style>                         </Border>                         <PaddingLeft>2pt</PaddingLeft>                         <PaddingRight>2pt</PaddingRight>                         <PaddingTop>2pt</PaddingTop>                         <PaddingBottom>2pt</PaddingBottom>                       </Style>                     </Textbox>                   </CellContents>                 </TablixCell>                 <TablixCell>                   <CellContents>                     <Textbox Name="Textbox32">                       <CanGrow>true</CanGrow>                       <KeepTogether>true</KeepTogether>                       <Paragraphs>                         <Paragraph>                           <TextRuns>                             <TextRun>                               <Value>班级</Value>                               <Style />                             </TextRun>                           </TextRuns>                           <Style />                         </Paragraph>                       </Paragraphs>                       <rd:DefaultName>Textbox32</rd:DefaultName>                       <Style>                         <Border>                           <Color>LightGrey</Color>                           <Style>Solid</Style>                         </Border>                         <PaddingLeft>2pt</PaddingLeft>                         <PaddingRight>2pt</PaddingRight>                         <PaddingTop>2pt</PaddingTop>                         <PaddingBottom>2pt</PaddingBottom>                       </Style>                     </Textbox>                   </CellContents>                 </TablixCell>                 <TablixCell>                   <CellContents>                     <Textbox Name="Textbox34">                       <CanGrow>true</CanGrow>                       <KeepTogether>true</KeepTogether>                       <Paragraphs>                         <Paragraph>                           <TextRuns>                             <TextRun>                               <Value>得分</Value>                               <Style />                             </TextRun>                           </TextRuns>                           <Style />                         </Paragraph>                       </Paragraphs>                       <rd:DefaultName>Textbox34</rd:DefaultName>                       <Style>                         <Border>                           <Color>LightGrey</Color>                           <Style>Solid</Style>                         </Border>                         <PaddingLeft>2pt</PaddingLeft>                         <PaddingRight>2pt</PaddingRight>                         <PaddingTop>2pt</PaddingTop>                         <PaddingBottom>2pt</PaddingBottom>                       </Style>                     </Textbox>                   </CellContents>                 </TablixCell>               </TablixCells>             </TablixRow>             <TablixRow>               <Height>0.23622in</Height>               <TablixCells>                 <TablixCell>                   <CellContents>                     <Textbox Name="RecId">                       <CanGrow>true</CanGrow>                       <KeepTogether>true</KeepTogether>                       <Paragraphs>                         <Paragraph>                           <TextRuns>                             <TextRun>                               <Value>=Fields!RecId.Value</Value>                               <Style />                             </TextRun>                           </TextRuns>                           <Style />                         </Paragraph>                       </Paragraphs>                       <rd:DefaultName>RecId</rd:DefaultName>                       <Style>                         <Border>                           <Color>LightGrey</Color>                           <Style>Solid</Style>                         </Border>                         <PaddingLeft>2pt</PaddingLeft>                         <PaddingRight>2pt</PaddingRight>                         <PaddingTop>2pt</PaddingTop>                         <PaddingBottom>2pt</PaddingBottom>                       </Style>                     </Textbox>                   </CellContents>                 </TablixCell>                 <TablixCell>                   <CellContents>                     <Textbox Name="Name">                       <CanGrow>true</CanGrow>                       <KeepTogether>true</KeepTogether>                       <Paragraphs>                         <Paragraph>                           <TextRuns>                             <TextRun>                               <Value>=Fields!Name.Value</Value>                               <Style />                             </TextRun>                           </TextRuns>                           <Style />                         </Paragraph>                       </Paragraphs>                       <rd:DefaultName>Name</rd:DefaultName>                       <Style>                         <Border>                           <Color>LightGrey</Color>                           <Style>Solid</Style>                         </Border>                         <PaddingLeft>2pt</PaddingLeft>                         <PaddingRight>2pt</PaddingRight>                         <PaddingTop>2pt</PaddingTop>                         <PaddingBottom>2pt</PaddingBottom>                       </Style>                     </Textbox>                   </CellContents>                 </TablixCell>                 <TablixCell>                   <CellContents>                     <Textbox Name="Age">                       <CanGrow>true</CanGrow>                       <KeepTogether>true</KeepTogether>                       <Paragraphs>                         <Paragraph>                           <TextRuns>                             <TextRun>                               <Value>=Fields!Age.Value</Value>                               <Style />                             </TextRun>                           </TextRuns>                           <Style />                         </Paragraph>                       </Paragraphs>                       <rd:DefaultName>Age</rd:DefaultName>                       <Style>                         <Border>                           <Color>LightGrey</Color>                           <Style>Solid</Style>                         </Border>                         <PaddingLeft>2pt</PaddingLeft>                         <PaddingRight>2pt</PaddingRight>                         <PaddingTop>2pt</PaddingTop>                         <PaddingBottom>2pt</PaddingBottom>                       </Style>                     </Textbox>                   </CellContents>                 </TablixCell>                 <TablixCell>                   <CellContents>                     <Textbox Name="Class">                       <CanGrow>true</CanGrow>                       <KeepTogether>true</KeepTogether>                       <Paragraphs>                         <Paragraph>                           <TextRuns>                             <TextRun>                               <Value>=Fields!Class.Value</Value>                               <Style />                             </TextRun>                           </TextRuns>                           <Style />                         </Paragraph>                       </Paragraphs>                       <rd:DefaultName>Class</rd:DefaultName>                       <Style>                         <Border>                           <Color>LightGrey</Color>                           <Style>Solid</Style>                         </Border>                         <PaddingLeft>2pt</PaddingLeft>                         <PaddingRight>2pt</PaddingRight>                         <PaddingTop>2pt</PaddingTop>                         <PaddingBottom>2pt</PaddingBottom>                       </Style>                     </Textbox>                   </CellContents>                 </TablixCell>                 <TablixCell>                   <CellContents>                     <Textbox Name="Scores">                       <CanGrow>true</CanGrow>                       <KeepTogether>true</KeepTogether>                       <Paragraphs>                         <Paragraph>                           <TextRuns>                             <TextRun>                               <Value>=Fields!Scores.Value</Value>                               <Style />                             </TextRun>                           </TextRuns>                           <Style />                         </Paragraph>                       </Paragraphs>                       <rd:DefaultName>Scores</rd:DefaultName>                       <Style>                         <Border>                           <Color>LightGrey</Color>                           <Style>Solid</Style>                         </Border>                         <PaddingLeft>2pt</PaddingLeft>                         <PaddingRight>2pt</PaddingRight>                         <PaddingTop>2pt</PaddingTop>                         <PaddingBottom>2pt</PaddingBottom>                       </Style>                     </Textbox>                   </CellContents>                 </TablixCell>               </TablixCells>             </TablixRow>           </TablixRows>         </TablixBody>         <TablixColumnHierarchy>           <TablixMembers>             <TablixMember />             <TablixMember />             <TablixMember />             <TablixMember />             <TablixMember />           </TablixMembers>         </TablixColumnHierarchy>         <TablixRowHierarchy>           <TablixMembers>             <TablixMember>               <KeepWithGroup>After</KeepWithGroup>             </TablixMember>             <TablixMember>               <Group Name="详细信息" />             </TablixMember>           </TablixMembers>         </TablixRowHierarchy>         <DataSetName>dsStudent</DataSetName>         <Top>0.52035cm</Top>         <Left>0.47625cm</Left>         <Height>1.2cm</Height>         <Width>12.49997cm</Width>         <Style>           <Border>             <Style>None</Style>           </Border>         </Style>       </Tablix>     </ReportItems>     <Height>2.96875in</Height>     <Style />   </Body>   <Width>6.5in</Width>   <Page>     <PageHeader>       <Height>1.34938cm</Height>       <PrintOnFirstPage>true</PrintOnFirstPage>       <PrintOnLastPage>true</PrintOnLastPage>       <ReportItems>         <Textbox Name="txtHeader">           <CanGrow>true</CanGrow>           <KeepTogether>true</KeepTogether>           <Paragraphs>             <Paragraph>               <TextRuns>                 <TextRun>                   <Value>学生成绩统计报表</Value>                   <Style />                 </TextRun>               </TextRuns>               <Style>                 <TextAlign>Center</TextAlign>               </Style>             </Paragraph>           </Paragraphs>           <Top>0.65299cm</Top>           <Height>0.6cm</Height>           <Width>16.51cm</Width>           <Style>             <Border>               <Style>None</Style>             </Border>             <VerticalAlign>Middle</VerticalAlign>             <PaddingLeft>2pt</PaddingLeft>             <PaddingRight>2pt</PaddingRight>             <PaddingTop>2pt</PaddingTop>             <PaddingBottom>2pt</PaddingBottom>           </Style>         </Textbox>       </ReportItems>       <Style>         <Border>           <Style>None</Style>         </Border>       </Style>     </PageHeader>     <PageFooter>       <Height>1.7507cm</Height>       <PrintOnFirstPage>true</PrintOnFirstPage>       <PrintOnLastPage>true</PrintOnLastPage>       <ReportItems>         <Textbox Name="txtFooter">           <CanGrow>true</CanGrow>           <KeepTogether>true</KeepTogether>           <Paragraphs>             <Paragraph>               <TextRuns>                 <TextRun>                   <Value>Copy Right 2001-2012 infosky R&amp;D</Value>                   <Style />                 </TextRun>               </TextRuns>               <Style>                 <TextAlign>Right</TextAlign>               </Style>             </Paragraph>           </Paragraphs>           <Top>1.04069cm</Top>           <Height>0.71cm</Height>           <Width>16.51cm</Width>           <Style>             <Border>               <Style>None</Style>             </Border>             <PaddingLeft>2pt</PaddingLeft>             <PaddingRight>2pt</PaddingRight>             <PaddingTop>2pt</PaddingTop>             <PaddingBottom>2pt</PaddingBottom>           </Style>         </Textbox>       </ReportItems>       <Style>         <Border>           <Style>None</Style>         </Border>       </Style>     </PageFooter>     <PageHeight>29.7cm</PageHeight>     <PageWidth>21cm</PageWidth>     <LeftMargin>2cm</LeftMargin>     <RightMargin>2cm</RightMargin>     <TopMargin>2cm</TopMargin>     <BottomMargin>2cm</BottomMargin>     <ColumnSpacing>0.13cm</ColumnSpacing>     <Style />   </Page>   <rd:ReportID>7d9ab7b5-369c-4d46-86a6-b8723598c7cd</rd:ReportID>   <rd:ReportUnitType>Cm</rd:ReportUnitType> </Report>

仔细观察这段xml文件,不难看出有几部分代码是值得关注的:

(1)路径Report/DataSets/DataSet/Fields下得Field节点,这里定义的是同数据源相关的列;

  1.  View Source        <Field Name="Name">           <DataField>Name</DataField>           <rd:TypeName>System.String</rd:TypeName>         </Field>

(2)路径Report/DataSets/DataSet/rd:DataSetInfo节点,这里定义了rdlc关联的xsd文件的路径;

  1.  View Source  <rd:DataSetInfo>         <rd:DataSetName>Students</rd:DataSetName>         <rd:SchemaPath>E:\Demo\HelloRDLC\HelloRDLC\Students.xsd</rd:SchemaPath>         <rd:TableName>dtStudent</rd:TableName>         <rd:TableAdapterFillMethod />         <rd:TableAdapterGetDataMethod />         <rd:TableAdapterName />       </rd:DataSetInfo>

(3)路径Report/Body/ReportItems/Tablix/TablixBody/TablixColumns下的TablixColumn节点,这里应该定义了RDLC报表的列数

  1.  View Source <TablixColumn>               <Width>0.98425in</Width>             </TablixColumn>

(4)路径Report/Body/ReportItems/Tablix/TablixBody/TablixRows下的TablixRow。从名称可知是报表行相关内容,其中每个TablixRow,又定义了单元格信息(在TablixCells下的TablixCell节点)。这里默认情况下有两行,第一行定义了报表列头显示内容,如:姓名,性别等,第二行定义了报表数据的绑定项。如:姓名绑定到xsd的Name字段。这里便有:<Value>=Fields!Name.Value</Value>

  1.  View Source<TablixCell>                   <CellContents>                     <Textbox Name="Textbox27">                       <CanGrow>true</CanGrow>                       <KeepTogether>true</KeepTogether>                       <Paragraphs>                         <Paragraph>                           <TextRuns>                             <TextRun>                               <Value>姓名</Value>                               <Style />                             </TextRun>                           </TextRuns>                           <Style />                         </Paragraph>                       </Paragraphs>                       <rd:DefaultName>Textbox27</rd:DefaultName>                       <Style>                         <Border>                           <Color>LightGrey</Color>                           <Style>Solid</Style>                         </Border>                         <PaddingLeft>2pt</PaddingLeft>                         <PaddingRight>2pt</PaddingRight>                         <PaddingTop>2pt</PaddingTop>                         <PaddingBottom>2pt</PaddingBottom>                       </Style>                     </Textbox>                   </CellContents>                 </TablixCell>

所以,这以上几处在我们修改xml时,势必可能需要修改。其实,这里还有一处需要修改,路径:Report/Body/ReportItems/Tablix/TablixColumnHierarchy/TablixMembers下的TablixMember,该节点个数一定要和报表列数相同。否则编译便会报错。

第二步:操作XML,动态添加列(GPA列)

(1)操作XML文件使用XmlDocument类,需要添加节点时,可以使用XmlNode.CloneNode(bool)方法。

  1.  View Source            //添加Field节点             XmlNodeList fileds = xmlDoc.GetElementsByTagName("Fields");
  2.             XmlNode filedNode = fileds.Item(0).FirstChild.CloneNode(true);             filedNode.Attributes["Name"].Value = "GPA";             filedNode.FirstChild.InnerText = "GPA";             fileds.Item(0).AppendChild(filedNode);

(2)操作xsd文件,将需要添加的列,加入到xsd中,并保存指定路径(保存文件命名为Student1.xsd);

  1.  View Source        private void ModifyXSD()         {             XmlDocument xmlDoc = new XmlDocument();             xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "Students.xsd");
  2.             XmlNodeList nodeList = xmlDoc.GetElementsByTagName("xs:sequence");             XmlNode node = nodeList.Item(0).FirstChild.CloneNode(true);             node.Attributes["name"].Value = "GPA";             node.Attributes["msprop:Generator_ColumnVarNameInTable"].Value = "columnGPA";             node.Attributes["msprop:Generator_ColumnPropNameInRow"].Value = "GPA";             node.Attributes["msprop:Generator_ColumnPropNameInTable"].Value = "GPAColumn";             node.Attributes["msprop:Generator_UserColumnName"].Value = "GPA";             nodeList.Item(0).AppendChild(node);
  3.             xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "Students1.xsd");         }

(2)修改rdlc文件,包括,待添加列、xsd文件路径等,中(或保存为rdlc文件);

  1. /// <summary>
  2. /// 修改RDLC文件
  3. /// </summary>
  4. /// <returns></returns>
  5. private XmlDocument ModifyRdlc()
  6. {
  7. XmlDocument xmlDoc = new XmlDocument();
  8.  
  9. xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "FirstRdlc.rdlc");
  10.  
  11. //添加Field节点
  12. XmlNodeList fileds = xmlDoc.GetElementsByTagName("Fields");
  13.  
  14. XmlNode filedNode = fileds.Item().FirstChild.CloneNode(true);
  15. filedNode.Attributes["Name"].Value = "GPA";
  16. filedNode.FirstChild.InnerText = "GPA";
  17. fileds.Item().AppendChild(filedNode);
  18.  
  19. //添加TablixColumn
  20.  
  21. XmlNodeList tablixColumns = xmlDoc.GetElementsByTagName("TablixColumns");
  22. XmlNode tablixColumn = tablixColumns.Item().FirstChild;
  23. XmlNode newtablixColumn = tablixColumn.CloneNode(true);
  24. tablixColumns.Item().AppendChild(newtablixColumn);
  25.  
  26. //TablixMember
  27. XmlNodeList tablixMembers = xmlDoc.GetElementsByTagName("TablixColumnHierarchy");
  28.  
  29. XmlNode tablixMember = tablixMembers.Item().FirstChild.FirstChild;
  30. XmlNode newTablixMember = tablixMember.CloneNode(true);
  31. tablixMembers.Item().FirstChild.AppendChild(newTablixMember);
  32.  
  33. XmlNodeList tablixRows = xmlDoc.GetElementsByTagName("TablixRows");
  34.  
  35. //TablixRows1
  36. var tablixRowsRowCells1 = tablixRows.Item().FirstChild.ChildNodes[];
  37. XmlNode tablixRowCell1 = tablixRowsRowCells1.FirstChild;
  38. XmlNode newtablixRowCell1 = tablixRowCell1.CloneNode(true);
  39. var textBox1 = newtablixRowCell1.FirstChild.ChildNodes[];
  40. textBox1.Attributes["Name"].Value = "GPA1";
  41.  
  42. var paragraphs = textBox1.ChildNodes.Cast<XmlNode>().Where(item => item.Name == "Paragraphs").FirstOrDefault();
  43. paragraphs.FirstChild.FirstChild.FirstChild.FirstChild.InnerText = "GPA";
  44. var defaultName1 = textBox1.ChildNodes.Cast<XmlNode>().Where(item => item.Name == "rd:DefaultName").FirstOrDefault().InnerText = "GPA1";
  45.  
  46. tablixRowsRowCells1.AppendChild(newtablixRowCell1);
  47.  
  48. //TablixRows2
  49. var tablixRowsRowCells2 = tablixRows.Item().ChildNodes[].ChildNodes[];
  50. XmlNode tablixRowCell2 = tablixRowsRowCells2.FirstChild;
  51. XmlNode newtablixRowCell2 = tablixRowCell2.CloneNode(true);
  52. var textBox2 = newtablixRowCell2.FirstChild.ChildNodes[];
  53. textBox2.Attributes["Name"].Value = "GPA";
  54.  
  55. var paragraphs2 = textBox2.ChildNodes.Cast<XmlNode>().Where(item => item.Name == "Paragraphs").FirstOrDefault();
  56. paragraphs2.FirstChild.FirstChild.FirstChild.FirstChild.InnerText = "=Fields!GPA.Value";
  57. var defaultName2 = textBox2.ChildNodes.Cast<XmlNode>().Where(item => item.Name == "rd:DefaultName").FirstOrDefault().InnerText = "GPA";
  58.  
  59. tablixRowsRowCells2.AppendChild(newtablixRowCell2);
  60.  
  61. xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "FirstRdlc1.rdlc");
  62. return xmlDoc;
  63. }

(3)将得到的XmlDocument实例,序列化到MemoryStream。

  1. /// <summary>
  2. /// 序列化到内存流
  3. /// </summary>
  4. /// <returns></returns>
  5. private Stream GetRdlcStream(XmlDocument xmlDoc)
  6. {
  7. Stream ms = new MemoryStream();
  8. XmlSerializer serializer = new XmlSerializer(typeof(XmlDocument));
  9. serializer.Serialize(ms, xmlDoc);
  10.  
  11. ms.Position = ;
  12. return ms;
  13. }

第三步:加载报表,并显示

(1)添加一个Page页面,并添加ReportView控件和ScriptManager控件,页面代码如下:

 View Source<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">     <title></title> </head> <body>     <form id="form1" runat="server">     <div>     <rsweb:ReportViewer ID="rvDemo" runat="server" Width="100%">     </rsweb:ReportViewer>         <asp:ScriptManager ID="smDemo" runat="server">         </asp:ScriptManager>     </div>     </form> </body> </html>

(2)加载报表定义,并绑定数据源(使用LoadReportDefinition(Stream stream)方法加载MemoryStream中信息)

  1. /// <summary>
  2. /// 加载报表
  3. /// </summary>
  4. private void LoadReport()
  5. {
  6. //获取数据源
  7. DataTable dataSource = GetDataSource();
  8.  
  9. //修改xsd文件
  10. ModifyXSD();
  11.  
  12. //修改rdlc文件
  13. XmlDocument xmlDoc = ModifyRdlc();
  14.  
  15. //将修改后的rdlc文档序列化到内存流中
  16. Stream stream = GetRdlcStream(xmlDoc);
  17.  
  18. //加载报表定义
  19. rvDemo.LocalReport.LoadReportDefinition(stream);
  20. //rvDemo.LocalReport.ReportPath = "FirstRdlc.rdlc";
  21.  
  22. //添加数据源,rvDemo是页面上的ReportView控件
  23. rvDemo.LocalReport.DataSources.Add(new ReportDataSource("dsStudent", dt));
  24. rvDemo.LocalReport.Refresh();
  25. }
  26.  
  27. /// <summary>
  28. /// 获取数据源
  29. /// </summary>
  30. /// <returns></returns>
  31. private DataTable GetDataSource()
  32. {
  33. //伪造一个数据源
  34. DataTable dt = new DataTable();
  35. dt.Columns.AddRange(new DataColumn[]
  36. {
  37. new DataColumn() { ColumnName = "RecId" },
  38. new DataColumn() { ColumnName = "Name" },
  39. new DataColumn() { ColumnName = "Age" },
  40. new DataColumn() { ColumnName = "Class" },
  41. new DataColumn() { ColumnName = "Scores" },
  42. new DataColumn() { ColumnName = "GPA" }
  43. });
  44.  
  45. DataRow dr = dt.NewRow();
  46. dr["RecId"] = "";
  47. dr["Name"] = "小明";
  48. dr["Age"] = "";
  49. dr["Class"] = "1年级";
  50. dr["Scores"] = "";
  51. dr["GPA"] = "4.0";
  52.  
  53. dt.Rows.Add(dr);
  54. return dt;
  55. }

如此,我们便可以动态的添加GPA这列到报表上了,结果如下:

源码地址:HelloRdlc.7z

作者:ps_zw
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

[转]RDLC报表——动态添加列的更多相关文章

  1. asp.net gridview动态添加列,并获取其数据;

    1,绑定数据前先动态添加列,见方法CreateGridColumn(只在第一次加载动态添加): 2,gvlist_RowDataBound为对应列添加控件: 前台代码: <%@ Page Lan ...

  2. GridView动态添加列之后,导致PostBack(回发)页面数据丢失问题解决

    直入主题,首先声明,这个问题是无法解决的,特此在这说明 一.如何动态添加列,如下: 在页面重写OnInit事件,至于为什么要在这个事件写,根据页面的声明周期和经验可知(不用去别的地方找了,这个我找了之 ...

  3. DataGridview动态添加列

    1.获取数据源(select * from table名称) 2.动态绑定数据源 private void GetTableInfo(DataTable dt) { listBh = new List ...

  4. Wpf DataGrid动态添加列,行数据(二)

    这是第二中方法,可直接绑定,我这里只是做出了一种思路,并不是最完美. 这里注意一下,因为我里面引用了MVVMLight,所以可能代码不是复制过去就能用了的. 样式也是,所以复制过去看不是我贴出来的界面 ...

  5. [RDLC]报表根据字段列动态加载图片(二)

    参照:http://www.cnblogs.com/hcbin/archive/2010/03/26/1696803.html 改动地方value的值可以用报表的字段进行编辑. 效果:

  6. gridview动态添加列的问题

    相信大家也和我一样遇到过这种问题,gridview在生成列的时候当列不确定怎么办?下面分享一下自己的解决方法. 举个列子说明一下. 普通列的添加比较简单. BoundField bf = new Bo ...

  7. GridView动态添加列并判断绑定数据DataTable的列类型控制展示内容

    此篇随笔是2013年根据项目需求开发记录的,不一定符合大众口味,只需了解开发思路,毕竟解决方案多种多样. 下面简单说说需求点吧: (1)通过下拉列表可以选择一个DataSet(数据集),一个DataS ...

  8. extjs动态添加列

    可以根据日期,动态的插入一列 controller层: StdDayWordQuery:function(btn,event){ var form=Ext.getCmp('queryFormSDW') ...

  9. C# DataGridView 动态添加列和行

    https://blog.csdn.net/alisa525/article/details/7350471 dataGridView1.ReadOnly = true ;      //禁用编辑功能 ...

随机推荐

  1. 小程序 之picker-view省市县

    代码地址:https://github.com/yangsphp/area-picker

  2. 学习csv

    1.csv文件读取,csv文件是常用的数据存储格式之一,我们使用Python模块来处理csv文件,这是一个天气信息表 import csv from matplotlib import pyplot ...

  3. idea中配置xml不自动提示解决方案

    1.打开设置File-->Settings(或者Ctrl + Alt + S)--->Languages&Frameworks-->Schemas and DTDS 2.选择 ...

  4. 20190625 Oracle优化查询(一)

    与其惴惴不安,不如定心应变 前提:我的Oracle服务器是安装在Windows环境中的,没有上到Linux 查看表结构 查询全表 查找空值, 使用“=”是没有结果的,应该使用IS NULL

  5. APIshop精选接口助力双十一电商业务

    距离2018年双11的购物盛典已经不到一个月了,各大电商之间的战役已经悄然打响,今年的双11仍会是一场电商鏖战,想必又会打破2017年双11近2540亿的全网成交总额记录. 据统计,去年双11全天共产 ...

  6. 腾讯云,搭建Java开发环境

    搭建 JAVA 开发环境 任务时间:18min ~ 20min 此实验教大家如何配置 JDK .Tomcat 和 Mysql 安装 JDK JDK 是开发Java程序必须安装的软件,我们查看一下 yu ...

  7. 【codeforces 755F】PolandBall and Gifts

    [题目链接]:http://codeforces.com/contest/755/problem/F [题意] n个人; 计划是每个人都拿一个礼物来送给一个除了自己之外的人; 且如果一个人没有送出礼物 ...

  8. App后台开发运维和架构实践学习总结(2)——RESTful API设计技巧

    前言 移动互联网时代,RESTful API成为越来越重要的移动端和服务器端交互的形式.尤其是在很多互联网公司或者传统行业拥抱移动互联网的时候,一套设计良好的Restful API能够帮助互联网产品支 ...

  9. BNUOJ 1021 信息战(七)——情报传递

    信息战(七)——情报传递 Time Limit: 3000ms Memory Limit: 262144KB 64-bit integer IO format: %lld      Java clas ...

  10. mysql :=和=的区别

    :=和=的区别 = 只有在set和update时才是和:=一样,赋值的作用,其它都是等于的作用.鉴于此,用变量实现行号时,必须用:= := 不只在set和update时时赋值的作用,在select也是 ...