[RDLC]MasterDetail報表(Subreport)(Web Form)

  • 4040
  • 0

摘要:[RDLC]MasterDetail報表, subreport

首先在.aspx拉出scriptmanager + report viewer, 設定好報表路徑以及子報表事件


<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <rsweb:ReportViewer ID="ReportViewer1" runat="server" Height="100%" Width="100%"
        AsyncRendering="False" SizeToReportContent="True">
        <LocalReport ReportPath="Reports/rptOrders.rdlc" OnSubreportProcessing="ReportViewer1_SubreportProcessing"></LocalReport>
    </rsweb:ReportViewer>

然後在.aspx.cs加入主報表以及子報表的SQL, 子報表的資料一定要自己想辦法在

SQL中過濾,不然保證效能超慢慢慢......


protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ReportViewer1.ProcessingMode = ProcessingMode.Local;
                //ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reports/rptOrders.rdlc");

                string strSQL = "";
                strSQL = strSQL + "select top 3 * from Orders " + "\n";
                
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = strSQL;
                DataTable dt = Common.GetDataTable(cmd);


                ReportDataSource datasource = new ReportDataSource("dsOrders", dt);
                ReportViewer1.LocalReport.DataSources.Clear();
                ReportViewer1.LocalReport.DataSources.Add(datasource);
                ReportViewer1.LocalReport.Refresh();
            }
        }

        protected void ReportViewer1_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
        {
            //DataSet dataSet = new DataSet();
            //dataSet.ReadXml(MapPath("Employee.xml"));
            //由於開網頁很慢
            //想改善performance的話,要自己去過濾合理範圍內的OrderID
            //ex:如果main report查詢條件是某天OrderDate,那麼subReport的sql要自己去過濾出適當的
            //(OrderDetails inner join Orders).OrderDate = "ooxx"
            string strSQL = "";
            strSQL = strSQL + "SELECT  [Order Details].* " + "\n";
            strSQL = strSQL + "FROM [Order Details] " + "\n";
            //strSQL = strSQL + "where OrderID = @OId" + "\n";
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = strSQL;
            DataTable dt = Common.GetDataTable(cmd);

            ReportDataSource dataSource = new ReportDataSource("dsOrderDetails", dt);
            e.DataSources.Add(dataSource);
        }

 子報表的部分:除了拉好簡單幾個欄位以外,

 

最重要的是,加入一個參數,讓報表跟

主報表有關連

 

然後很技術性的在資料表的左上角的方塊按下右鍵,選擇tabfix屬性

 

然後設定filter如下, 讓資料表的OrderID等於變數@OId

 

主報表的部分,除了在清單中簡單拉幾個主報表的欄位還有一個子報表之外

 

最重要的是設定子報表的一般性以及參數,讓主報表跟子報表產生關連

 

最後的最後,因為每張訂單應該都佔據一整個頁面,所以必須在群組後換頁

(我個人是覺得,只要是清單元件裡面的東西,都是同一個群組)

如下設定群組換頁,即可

 

最後直接按下F5執行.aspx即可,完工!