【问题标题】:Adding Programmatically datasource / dataset to LocalReport以编程方式将数据源/数据集添加到 LocalReport
【发布时间】:2013-07-10 19:32:31
【问题描述】:

当报表 XmlFile (*.rdlc) 在设计时没有数据源/数据集定义时,有没有办法以编程方式将数据源/数据集添加到 Microsoft.Reporting.WebForms.LocalReport?

如果我的 *.rdlc 中已有数据源/数据集定义,则此方法有效

C#

public byte[] RenderReport(string reportName, string reportFormat)
{
    LocalReport report = LoadReport(reportName);

    //Has same name like DataSet in *.rdlc
    ReportDataSource rds = new ReportDataSource("DataSet1", getData());

    report.DataSources.Clear();
    report.DataSources.Add(rds);

    return report.Render(reportName);
}

private DataTable getData()
{
    DataTable dt = new DataTable();

    dt.Columns.Add(new DataColumn("ID",typeof(System.String)));
    dt.Columns.Add(new DataColumn("NAME", typeof(System.String)));

    dt.Rows.Add(new string[] { "1", "Me" });
    return dt;
}

*.rdlc

  <DataSources>
    <DataSource Name="DataSource1">
      <ConnectionProperties>
        <DataProvider>System.Data.DataSet</DataProvider>
        <ConnectString>/* Local Connection */</ConnectString>
      </ConnectionProperties>
    </DataSource>
  </DataSources>
  <DataSets>
    <DataSet Name="DataSet1">
      <Query>
        <DataSourceName>DataSource1</DataSourceName>
        <CommandText>/* Local Query */</CommandText>
      </Query>
      <Fields>
        <Field Name="ID">
          <DataField>ID</DataField>
          <rd:TypeName>System.String</rd:TypeName>
        </Field>
        <Field Name="NAME">
          <DataField>NAME</DataField>
          <rd:TypeName>System.String</rd:TypeName>
        </Field>
      </Fields>
    </DataSet>
  </DataSets>

但如果我删除数据源/数据集定义,我会得到 ​​p>

{Microsoft.Reporting.DefinitionInvalidException: 的定义 报告''无效。 ---> Microsoft.ReportingServices.ReportProcessing.ReportPublishingException: 文本框“Textbox1”的值表达式引用该字段 'ID'。报表项表达式只能引用 当前数据集范围,或者,如果在聚合内,则指定的 数据集范围。字段名称中的字母必须使用正确的 }

我是否总是需要创建类似“Dummy”-DataSource/DataSet 的东西,还是我的代码中遗漏了一些东西? 我希望在渲染过程之前有另一种解决方案来操作 XML,有什么想法吗?

谢谢!

【问题讨论】:

    标签: webforms reporting rdlc localreport microsoft-reporting


    【解决方案1】:

    如果您正在使用 RDLC 并且 RDLC 嵌入在您的项目中,那么您不能离开没有 DataSets 的 RDLC。 要么您将 DataSet 固定并仅更改它的项目要么尝试从 XML 加载报告定义

    // Valid XML with dynamic DataSources and DataSets
    string s = @"<?xml version=""1.0"" encoding=""utf-8""?><Report ...>...</Report>";
    report.LoadReportDefinition(new MemoryStream(Encoding.UTF8.GetBytes(s)));
    return report.Render(reportName);
    

    【讨论】:

    • report.Render 接受“字符串格式”作为参数,而不是报告名称。
    猜你喜欢
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多