gpt4 book ai didi

c# - ReportViewer 在子报表中出现空错误

转载 作者:行者123 更新时间:2023-11-30 21:13:10 24 4
gpt4 key购买 nike

我正在使用 .Net 4.0 和 C# 在 Visual Studio 2010 中构建一个小型应用程序,我从列表中生成 ReportViewer 报告。然后我的 tablix 中有一个子报表,它应该从 WebLink 传递一个名为 ProviderIdentifier 的属性值。我在我的报告上实现了 SubReportProcessing 事件,以将数据返回到子报告,如下所示:

private void localReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
List<WebLink> links = LinkProvider.FetchSublinks(LinkProvider.Fetch(new WebLink(new Uri("http://www.contentstudio.se/"))));
e.DataSources.Add(new ReportDataSource("ParentLinks", links));
}

目前,我为子报表的所有实例返回相同的链接。在我尝试将参数传递给子报表之前,报表工作正常。当我使用 ProviderIdentifier 添加参数时(我可以在我的报告中毫无问题地显示它),我总是收到 NullReferenceException 消息“对象引用未设置为对象的实例”。当我在 LocalReport 对象上调用 Render() 时。如果我向报告添加一个静态值(如 1)而不是传递 ProviderIdentifier,也会发生同样的情况。如果我一起删除参数,虽然效果很好,但我无法确定哪些链接返回到子报表。

有人知道是什么导致了这个问题吗?

完整代码:

public void RenderReport()
{
LocalReport localReport = new LocalReport
{
ReportPath = ("BrokenLink.rdlc"),
EnableHyperlinks = true,
ShowDetailedSubreportMessages = true
};

List<WebLink> links = LinkProvider.FetchSublinks(LinkProvider.Fetch(new WebLink(new Uri("http://www.contentstudio.se/"))));

ReportDataSource reportDataSource = new ReportDataSource("Weblinks", links);
localReport.DataSources.Add(reportDataSource);

localReport.SubreportProcessing += localReport_SubreportProcessing;

const string reportType = "PDF";
string mimeType;
string encoding;
string fileNameExtension;

//The DeviceInfo settings should be changed based on the reportType
//http://msdn2.microsoft.com/en-us/library/ms155397.aspx
const string deviceInfo = "<DeviceInfo>" +
" <OutputFormat>PDF</OutputFormat>" +
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>" +
" <MarginTop>0.5in</MarginTop>" +
" <MarginLeft>1in</MarginLeft>" +
" <MarginRight>1in</MarginRight>" +
" <MarginBottom>0.5in</MarginBottom>" +
" <DpiX>72</DpiX>" +
" <DpiY>72</DpiY>" +
"</DeviceInfo>";
Warning[] warnings;
string[] streams;

//Render the report
byte[] renderedBytes = localReport.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);

File.WriteAllBytes("I:\\report.pdf", renderedBytes);
}

最佳答案

花了几个小时,但我终于找到了我错过的东西。如果您检查报告的属性,您可以在那里设置“变量”,我已经测试了很多方法来为来自主报告的参数创建匹配。我完全错过(并且无法在网上找到正确描述)的是,在编辑器右侧的 TreeView 中,您有一个名为“Parameters”的文件夹。我在那里添加了一个参数,该参数与我的主要报告传递的参数相对应,现在它可以正常工作了!

关于c# - ReportViewer 在子报表中出现空错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6981963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com