2013年7月22日 星期一

[學習] ReportViewer SubReport 子報表

CODE:使用 自定義事件 DemoSubreportProcessingEventHandler

Imports Microsoft.Reporting.WinForms
Public Class Printer_YJSalary

    Private _MyCallForm As Object
    Public Property MyCallForm() As Object
        Get
            Return _MyCallForm
        End Get
        Set(ByVal value As Object)
            _MyCallForm = value
        End Set
    End Property

    Private orderDetailsData As DataTable = Nothing
    Function LoadOrderDetailsData(ByVal a As String) As DataTable
        Dim salaryM As New Class_conn(ConnRegNfunction.connMode, _
                            ConnRegNfunction.連線字串Local, _
                            ConnRegNfunction.連線字串WebUri)
        Dim salaryDT = salaryM.DataTable("select e.*,f.姓名,f.部門,f.name,z.身份2,ass.名稱,ass.金額 from salary_month as e " & _
            "left join" & _
            "     (select a.emp_sn,a.身份2  from salary_basic as a, " & _
            "          (select emp_sn,max(調整日期) as 調整日期 from salary_basic GROUP BY emp_sn) " & _
            "     as b where a.emp_sn = b.emp_sn and a.調整日期 = b.調整日期 and a.身份2=" & ComboBox1.SelectedValue & ") as z " & _
            "on z.emp_sn = e.emp_sn  " & _
            "left join " & _
            "     (select c.staff_sn,c.zhFname+c.zhName as 姓名,c.部門,d.name from employee as c left join " & _
            "           (select a.* from employeePosition as a ,(select staff_sn,max(createDate) as createDate from employeePosition GROUP BY staff_sn) " & _
            "      as b where a.staff_sn = b.staff_sn and a.createDate = b.createDate) " & _
            "as d on d.staff_sn = c.staff_sn) as f on e.emp_sn = f.staff_sn " & _
            "left join salary_month_others as ass on ass.salary_month_sn = e.sn " & _
            "where e.日期 = '" & ComboBox3.Text.Trim & ComboBox2.Text.Trim & "' and z.身份2 = " & ComboBox1.SelectedValue & "", Nothing, Nothing)

        salaryM.Dispose()
        Return salaryDT
    End Function

    Public Sub DemoSubreportProcessingEventHandler(ByVal sender As Object, _
  ByVal e As SubreportProcessingEventArgs)
         orderDetailsData = LoadOrderDetailsData("XXXX")
        e.DataSources.Add(New ReportDataSource("YJeHRsysDataSet_salary_month_others", orderDetailsData))
    End Sub

    Private Sub Printer_YJSalary_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        Form.CheckForIllegalCrossThreadCalls = False
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle

        Dim dt_identity2 As New DataTable
        Dim col3 As New DataColumn
        Dim col4 As New DataColumn
        col3.ColumnName = "名稱"
        col4.ColumnName = "編號"
        dt_identity2.Columns.Add(col3)
        dt_identity2.Columns.Add(col4)
        dt_identity2.Rows.Add("辦公室", 1)
        dt_identity2.Rows.Add("現場", 2)
        dt_identity2.Rows.Add("阿姨", 3)
        ComboBox1.ValueMember = "編號"
        ComboBox1.DisplayMember = "名稱"
        ComboBox1.DataSource = dt_identity2

        Dim month() As String = {"01", "02", "03", "04", "05", "06", _
                                    "07", "08", "09", "10", "11", "12"}
        ComboBox2.Items.AddRange(month)

        For i As Integer = 2010 To Integer.Parse(Now.Date.Year.ToString)
            ComboBox3.Items.Add(i)
        Next

        Dim sqlQuery As String = "SELECT 中文名稱 FROM [公司別] ORDER BY 中文名稱;"
        Dim staffDB As New Class_conn(ConnRegNfunction.connMode, _
    ConnRegNfunction.連線字串Local, _
    ConnRegNfunction.連線字串WebUri)
        Dim dt = staffDB.DataTable(sqlQuery, Nothing, Nothing)
        
        ComboBox4.Items.Add("公司名稱")

        ComboBox3.Text = Now.Date.Year.ToString
        ComboBox3.DropDownStyle = ComboBoxStyle.DropDown
        ComboBox3.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        ComboBox3.AutoCompleteSource = AutoCompleteSource.ListItems

        ComboBox2.Text = Now.Date.Month.ToString("00")
        ComboBox2.DropDownStyle = ComboBoxStyle.DropDown
        ComboBox2.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        ComboBox2.AutoCompleteSource = AutoCompleteSource.ListItems

        ComboBox4.DropDownStyle = ComboBoxStyle.DropDownList
        ComboBox4.SelectedIndex = 0
        
        ComboBox5.SelectedIndex = 0

        Call Button1_Click(Me, Nothing)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.ReportViewer1.Reset()
        Dim rds As ReportDataSource = Nothing
        Dim salaryM As New Class_conn(ConnRegNfunction.connMode, _
                    ConnRegNfunction.連線字串Local, _
                    ConnRegNfunction.連線字串WebUri)
        Dim salaryDT As New DataTable
        If ComboBox5.Text = "薪資大表" Then
            Me.ReportViewer1.LocalReport.ReportEmbeddedResource = "YJeHRsys.ReportOfficeB.rdlc"
            If ComboBox4.Text.Trim.ToUpper = "ALL" Then
                
            Else
                salaryDT = salaryM.DataTable("select e.*,f.姓名,f.部門,f.name,z.身份2,f.卡號 from salary_month as e left join (select a.emp_sn,a.身份2  from salary_basic as a, (select emp_sn,max(調整日期) as 調整日期 from salary_basic GROUP BY emp_sn) as b where a.emp_sn = b.emp_sn and a.調整日期 = b.調整日期 and a.身份2=" & ComboBox1.SelectedValue & ") as z on z.emp_sn = e.emp_sn  left join (select c.staff_sn,c.zhFname+c.zhName as 姓名,c.部門,d.name,c.卡號 from employee as c left join (select a.* from employeePosition as a ,(select staff_sn,max(createDate) as createDate from employeePosition GROUP BY staff_sn) as b where a.staff_sn = b.staff_sn and a.createDate = b.createDate) as d on d.staff_sn = c.staff_sn) as f on e.emp_sn = f.staff_sn where e.日期 = '" & ComboBox3.Text.Trim & ComboBox2.Text.Trim & "' and z.身份2 = " & ComboBox1.SelectedValue & " ORDER BY f.卡號;", Nothing, Nothing)
            End If
            rds = New ReportDataSource("YJeHRsysDataSet_salary_month", salaryDT)

        Else
            Me.ReportViewer1.LocalReport.ReportEmbeddedResource = "YJeHRsys.ReportOfficeS.rdlc"
            If ComboBox4.Text.Trim.ToUpper = "ALL" Then
               
            Else
                salaryDT = salaryM.DataTable("select e.*,f.姓名,f.部門,f.name,z.身份2,f.卡號 from salary_month as e left join (select a.emp_sn,a.身份2  from salary_basic as a, (select emp_sn,max(調整日期) as 調整日期 from salary_basic GROUP BY emp_sn) as b where a.emp_sn = b.emp_sn and a.調整日期 = b.調整日期 and a.身份2=" & ComboBox1.SelectedValue & ") as z on z.emp_sn = e.emp_sn  left join (select c.staff_sn,c.zhFname+c.zhName as 姓名,c.部門,d.name,c.卡號 from employee as c left join (select a.* from employeePosition as a ,(select staff_sn,max(createDate) as createDate from employeePosition GROUP BY staff_sn) as b where a.staff_sn = b.staff_sn and a.createDate = b.createDate) as d on d.staff_sn = c.staff_sn) as f on e.emp_sn = f.staff_sn where e.日期 = '" & ComboBox3.Text.Trim & ComboBox2.Text.Trim & "' and z.身份2 = " & ComboBox1.SelectedValue & " ORDER BY f.卡號;", Nothing, Nothing)
            End If
            rds = New ReportDataSource("YJeHRsysDataSet_salary_month", salaryDT)

        End If
        ReportViewer1.LocalReport.DataSources.Clear()
        ReportViewer1.LocalReport.DataSources.Add(rds)

        Dim rp1 As ReportParameter = _
        New ReportParameter("列印年份", ComboBox3.Text.Trim)
        Dim rp2 As ReportParameter = _
        New ReportParameter("列印月份", ComboBox2.Text.Trim)
        ReportViewer1.LocalReport.SetParameters(New ReportParameter() {rp1})
        ReportViewer1.LocalReport.SetParameters(New ReportParameter() {rp2})

        AddHandler Me.ReportViewer1.LocalReport.SubreportProcessing, AddressOf DemoSubreportProcessingEventHandler

        ReportViewer1.RefreshReport()
        salaryM.Dispose()
        salaryDT.Dispose()
    End Sub
End Class



設計畫面:
主報表


子報表


步驟:
STEP1.
各自建立報表 ReportOfficeS.rdlc(主報表) & ReportOfficeS2.rdlc(子報表) 及報表來源

注意:
報表來源不可以有沒使用到的數據來源
不然會一直出錯,沒有 DataSet_dt 數據來源

STEP2.
在主報表的子報表控制項按右鍵, 選擇屬性!!
(1)設定子報表 (2)設定參數

STEP3.
在子報表設定報表參數...

SETP4.
在子報表設定Table籂選...



範例畫面:

參考:
LocalReport - 使用子報表
"因為主表的明細同尺寸會有不用厚度,所以處理子報表時,需把主表的尺寸做群組"
"因為各道次會有相同的尺寸&厚度,所以子報表的dt來源需要篩選條件〔道次 & 尺寸〕"

    Dim paraName(7) As String
    Dim paraValue(7) As Object
    Dim dt As DataTable
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dt = New DataTable()
        ReportViewer1.Reset()
        Try
                paraName = New String() {"產品類別ID", "售別ID", "起始道次ID", "結束道次ID", "起始尺寸ID", "結束尺寸ID"}
                paraValue = New Object() {cbo產品類別.SelectedValue, cbo售別.SelectedValue, cbo道次1.SelectedValue, cbo道次2.SelectedValue, cbo尺寸1.SelectedValue, cbo尺寸2.SelectedValue}
                dt = ws.OleDbDataTable(conn, localhost.CommandType.StoredProcedure, "qry結果查詢產品類別售別道次不分機台", paraName, paraValue).Tables(0)
                ReportViewer1.LocalReport.ReportEmbeddedResource = "Statistics.rpt效率基準整合表_不分機台_copy.rdlc"

            Dim rp As New ReportParameter("每日工作時間", txt每日工作時間.Text)
            ReportViewer1.LocalReport.SetParameters(New ReportParameter() {rp})
            Dim rds As New ReportDataSource("效率基準整合表", dt)
            ReportViewer1.LocalReport.DataSources.Add(rds)

            AddHandler Me.ReportViewer1.LocalReport.SubreportProcessing, AddressOf DemoSubreportProcessingEventHandler

            ReportViewer1.RefreshReport()
        Catch ex As Exception
            MessageBox.Show("【請通知資訊工程師】\n錯誤訊息:\n" + ex.Message)
        End Try

    End Sub

    Public Sub DemoSubreportProcessingEventHandler(ByVal sender As Object, ByVal e As SubreportProcessingEventArgs)      
        Dim detaildt As New DataTable()
        detaildt = dt.Copy().Select("道次ID='" + e.Parameters("道次ID").Values(0) + "' AND 尺寸ID='" + e.Parameters("尺寸ID").Values(0) + "'").CopyToDataTable()
        e.DataSources.Add(New ReportDataSource("效率基準整合表_detail", detaildt)) 'orderDetailsData))

    End Sub





RDLC 子报表不出来的原因
[SSRS] 子報表 [MSDN]設定子報表和鑽研報表 (Visual Studio 報表設計工具)
[MSDN]LocalReport.SubreportProcessing 事件
RDL(C) Report Design Step by Step 2: SubReport
[原创] RDLC 报表系列(四) 子报表的使用
[置顶] ReportViewer 嵌套子报表
RDLC 報表學習筆記—子報表

Reporting Services
Beginning SQL Server 2005 Reporting Services Part 1
Beginning SQL Server 2005 Reporting Services Part 2
Beginning SQL Server 2005 Reporting Services Part 3: The Chart Control
Beginning SQL Server Reporting Services Part 4

子報表的應用-主管的簽名檔
ReportViewer 動態改變TableRow是否顯示(或隱藏)
[ASP.NET] RDLC 子報表運行模式
RDLC原生導出Excel之功能, 發生「忽略資料表/矩陣資料格中的子報表」
ReportViewer Drill-Through Reports 鑽研報表 (大陸譯:钻取报表)
[RDLC] 使用子報表(SubReport)從既有RDLC建立多筆報表

Difficulty setting ReportParameter

其它:

沒有留言:

張貼留言