專案架構如下,專案下產生兩個報表(父報表與子報表)。
新增TypedDataSet(Ex:dsReport.xsd),範例定義如下。
在myReport.rdlc報表中,設定資料集屬性。
在myReport.rdlc報表中,新增子報表。
在myReport.rdlc報表中,設定[子報表屬性]。
設定要傳給子報表的參數。
在mySubReport.rdlc報表中,設定資料集屬性。
在mySubReport.rdlc報表中,設定參數,參數名稱要和[子報表參數]設定一致(EX:OrderID)。
程式部分:
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head runat="server"> | |
<title></title> | |
</head> | |
<body> | |
<form id="form1" runat="server"> | |
<div> | |
<asp:ScriptManager ID="ScriptManager1" runat="server"> | |
</asp:ScriptManager> | |
</div> | |
<asp:Button ID="btnShowReport" runat="server" Text="顯示報表" onclick="btnShowReport_Click"/> | |
<rsweb:ReportViewer ID="ReportViewer1" Width="100%" | |
runat="server"> | |
</rsweb:ReportViewer> | |
</form> | |
</body> | |
</html> |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.UI; | |
using System.Web.UI.WebControls; | |
using System.Data; | |
using Microsoft.Reporting.WebForms; | |
namespace TestSubReport | |
{ | |
public partial class ShowReport : System.Web.UI.Page | |
{ | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
if (this.IsPostBack) | |
return; | |
} | |
private void ShowReportData() | |
{ | |
ReportViewer1.Visible = true; | |
//子報表繫結的事件 | |
ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing); | |
ReportViewer1.LocalReport.DataSources.Clear(); | |
ReportViewer1.ProcessingMode = ProcessingMode.Local; | |
ReportViewer1.LocalReport.ReportPath = HttpContext.Current.Server.MapPath(string.Format("~\\bin\\{0}", "myReport.rdlc")); | |
DataTable tbl = this.GetMasterData(); | |
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("myOrders", tbl)); | |
ReportViewer1.LocalReport.Refresh(); | |
} | |
/// <summary> | |
/// 繫結子報表 | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e) | |
{ | |
//取得參數 | |
string orderid = e.Parameters["OrderID"].Values[0]; | |
//取得明細資料 | |
DataTable tbl = this.GetDetailData(orderid); | |
//繫結子報表 | |
e.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("myOrdersDetail", tbl)); | |
} | |
/// <summary> | |
/// 點擊[顯示報表] | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
protected void btnShowReport_Click(object sender, EventArgs e) | |
{ | |
this.ShowReportData(); | |
} | |
/// <summary> | |
/// 取得單頭資料 | |
/// </summary> | |
/// <returns></returns> | |
private DataTable GetMasterData() | |
{ | |
DataTable tbl = new DataTable(); | |
DataColumn col = new DataColumn("OrderID"); | |
tbl.Columns.Add(col); | |
DataRow row = tbl.NewRow(); | |
row["OrderID"] = "A001"; | |
DataRow row2 = tbl.NewRow(); | |
row2["OrderID"] = "A002"; | |
tbl.Rows.Add(row); | |
tbl.Rows.Add(row2); | |
return tbl; | |
} | |
/// <summary> | |
/// 取得明細資料 | |
/// </summary> | |
/// <param name="OrderID"></param> | |
/// <returns></returns> | |
private DataTable GetDetailData(string OrderID) | |
{ | |
DataTable dtDetail = new DataTable(); | |
DataColumn col = new DataColumn("OrderID"); | |
DataColumn col2 = new DataColumn("Item"); | |
dtDetail.Columns.Add(col); | |
dtDetail.Columns.Add(col2); | |
DataRow row = dtDetail.NewRow(); | |
row["OrderID"] = "A001"; | |
row["Item"] = "A001-1"; | |
DataRow row2 = dtDetail.NewRow(); | |
row2["OrderID"] = "A002"; | |
row2["Item"] = "A002-2"; | |
dtDetail.Rows.Add(row); | |
dtDetail.Rows.Add(row2); | |
var query = from data in dtDetail.AsEnumerable() | |
where data.Field<String>("OrderID") == OrderID | |
select data; | |
DataTable tbl = query.CopyToDataTable<DataRow>(); | |
return tbl; | |
} | |
} | |
} |
打包:下載