跳到主要內容

RDLC中使用子報表

最近在處理報表時發現一個問題,就是子報表的設定,在主報表中可以加入子報表,如果只有根據MSDN官方的範例去撰寫,會發現其實在運行的時候會發生錯誤,錯誤訊息[An error occurred during local report processing. Value cannot be null. Parameter name: value ],經過了同事的協助後,終於解決了這個問題,原來魔鬼真的藏在細節中。

通常我們會在主報表中拉入子報表控制項,然後設定參數SNAGHTML115e2da
然後接下來的這一步是主要的關鍵,在報表資料的參數中,按右鍵選加入參數,然後在 (1) 參數名稱中,填入主報表傳遞的變數,接著在 (2) 勾選允許Null 值。image

再過來就可以依照MSDN的範例程式去呼叫子報表囉…

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Reporting.WebForms;

public partial class _Default : System.Web.UI.Page
{
private DataTable orderDetailsData = null;

private DataTable LoadOrdersData()
{
// Load data from XML file
DataSet dataSet = new DataSet();
dataSet.ReadXml(@"c:\My Reports\OrderData.xml");
return dataSet.Tables[0];
}

private DataTable LoadOrderDetailsData()
{
// Load data from XML file
DataSet dataSet = new DataSet();
dataSet.ReadXml(@"c:\My Reports\OrderDetailData.xml");
return dataSet.Tables[0];
}

void DemoSubreportProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
{
if (orderDetailsData == null)
orderDetailsData = LoadOrderDetailsData();
e.DataSources.Add(new ReportDataSource("DataSet1_OrderDetails", orderDetailsData));
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Set RDL file
ReportViewer1.LocalReport.ReportPath =
@"c:\My Reports\Orders.rdlc";

// Supply a DataTable corresponding to each report dataset
ReportViewer1.LocalReport.DataSources.Add(
new ReportDataSource("DataSet1_Orders",
LoadOrdersData()));
}
// Add a handler for SubreportProcessing
ReportViewer1.LocalReport.SubreportProcessing += new
SubreportProcessingEventHandler(DemoSubreportProcessingEventHandler);
}
}

以上範例為MSDN所提供。感謝同事RobinKyle的鼎力相助。

參考資料:
1.MSDN:http://msdn.microsoft.com/zh-tw/library/microsoft.reporting.webforms.localreport.subreportprocessing(v=vs.80).aspx
2.http://stackoverflow.com/questions/4018533/passing-parameters-to-sub-reports-in-rdlc-under-vs-2010
3.LocalReport - 使用子報表:http://kylesheng.blogspot.tw/2013/06/localreport.html

留言

這個網誌中的熱門文章

以管理者權限執行批次檔

最近有個專案需要執行批次檔,來進行某些設定或者城市的安裝,在XP上這個Script可以運行沒問題,可是一到Vista以後的Windows版本就無法運行了,最主要的原因是,UAC的管制的問題,幾經尋找,總算找到一個可行的解決辦法。

如何使用電子發票應用API

財政部在電子發票推行上,為了方便開發更多應用所以提供了API可供個人或者廠商進行相關應用的開發,申請的方式請參考 –> 電子發票API申請 最簡單的申請方式就是透過自然人憑證或者工商憑證申請,這樣就不需要檢附任何證明文件。不過這不是本篇的重點,當申請到時會給你一組AppID跟APIKey,然後你可以下載 電子發票應用規格1.4版 來了解怎樣使用這個API,或許是我才疏學淺,所以在1.3版的時候其實搞不太懂他的API文件,到了1.4版才摸清楚發生了什麼事,當然還是花了點時間才搞懂,現在就簡單說明一下API的使用方式,希望看到本篇文章的同好就不需要花時間測試了。

DataGridView欄位統一格式化

最近的工作內有一個需求,就是由於專案中有許多呈現資料的DataGridView,而其中的欄位需要呈現的包含金額、數字或者日期等格式,若要一個個的設定格式,如果有一天格式突然變更,可能就要苦工做到死,如何讓專案中的這些格式都統一就成了一個問題,經過了一番查找,發現可以透過DataGridView.CellFormatting Event來解決這個問題。