2013年3月20日 星期三

[學習] ReportViewer Drill-Through Reports 鑽研報表 (大陸譯:钻取报表)

主體屬性 與 報表屬性
報表規格A4紙 21寬 29.7長
邊界都為0


CODE:使用 自定義事件 DemoDrillthroughEventHandler
Imports Microsoft.Reporting.WinForms
Public Class Printer_Salary
    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 s.*,e.zhFname + e.zhName as 姓名,b.account FROM dbo.salaryM as s left join dbo.employee as e on s.staff_sn = e.staff_sn left join dbo.bank as b on s.staff_sn = b.staff_sn where s.staff_sn = " & a & " and s.ym = " & ComboBox3.Text.Trim & ComboBox2.Text.Trim & " and s.第幾次發薪 = " & TextBox1.Text.Trim & "", Nothing, Nothing)
        salaryM.Dispose()
        Return salaryDT
    End Function

    Private Sub DemoDrillthroughEventHandler(ByVal sender As System.Object, ByVal e As DrillthroughEventArgs)
        Dim myLocalReport As LocalReport
        myLocalReport = e.Report
        orderDetailsData = LoadOrderDetailsData(myLocalReport.GetParameters()("Report_Parameter_3").Values(0).Trim)
        myLocalReport.DataSources.Add(New ReportDataSource("yiTestDataSet_salaryM1", orderDetailsData))
    End Sub

    Private Sub Printer_Salary_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ReportViewer1.RefreshReport()
    End Sub

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

        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

        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

        ComboBox1.SelectedIndex = 0

        Call Button1_Click(Me, Nothing)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim salaryM As New Class_conn(ConnRegNfunction.connMode, _
                            ConnRegNfunction.連線字串Local, _
                            ConnRegNfunction.連線字串WebUri)
        Dim salaryDT = salaryM.DataTable("SELECT s.*,e.zhFname + e.zhName as 姓名,b.account FROM dbo.salaryM as s left join dbo.employee as e on s.staff_sn = e.staff_sn left join dbo.bank as b on s.staff_sn = b.staff_sn where s.ym = " & ComboBox3.Text.Trim & ComboBox2.Text.Trim & " and s.第幾次發薪 = " & TextBox1.Text.Trim & "", Nothing, Nothing)
        Dim rds As ReportDataSource = New ReportDataSource("yiTestDataSet_salaryM", salaryDT) 
        ReportViewer1.LocalReport.DataSources.Clear()
        ReportViewer1.LocalReport.DataSources.Add(rds)

        Dim rp1 As ReportParameter = _
        New ReportParameter("Report_Parameter_0", ComboBox3.Text.Trim)
        Dim rp2 As ReportParameter = _
        New ReportParameter("Report_Parameter_1", ComboBox2.Text.Trim)
        Dim rp3 As ReportParameter = _
        New ReportParameter("Report_Parameter_2", TextBox1.Text.Trim)

        ReportViewer1.LocalReport.SetParameters(New ReportParameter() {rp1})
        ReportViewer1.LocalReport.SetParameters(New ReportParameter() {rp2})
        ReportViewer1.LocalReport.SetParameters(New ReportParameter() {rp3})

  AddHandler ReportViewer1.Drillthrough, AddressOf DemoDrillthroughEventHandler

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

CODE:使用報表事件 ReportViewer1_Drillthrough
Imports Microsoft.Reporting.WinForms
Public Class Printer_Salary
    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 s.*,e.zhFname + e.zhName as 姓名,b.account FROM dbo.salaryM as s left join dbo.employee as e on s.staff_sn = e.staff_sn left join dbo.bank as b on s.staff_sn = b.staff_sn where s.staff_sn = " & a & " and s.ym = " & ComboBox3.Text.Trim & ComboBox2.Text.Trim & " and s.第幾次發薪 = " & TextBox1.Text.Trim & "", Nothing, Nothing)
        salaryM.Dispose()
        Return salaryDT
    End Function

    Private Sub Printer_Salary_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ReportViewer1.RefreshReport()
    End Sub

    Private Sub Printer_Salary_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
        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

        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

        ComboBox1.SelectedIndex = 0
        Call Button1_Click(Me, Nothing)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim salaryM As New Class_conn(ConnRegNfunction.connMode, _
                            ConnRegNfunction.連線字串Local, _
                            ConnRegNfunction.連線字串WebUri)
        Dim salaryDT = salaryM.DataTable("SELECT s.*,e.zhFname + e.zhName as 姓名,b.account FROM dbo.salaryM as s left join dbo.employee as e on s.staff_sn = e.staff_sn left join dbo.bank as b on s.staff_sn = b.staff_sn where s.ym = " & ComboBox3.Text.Trim & ComboBox2.Text.Trim & " and s.第幾次發薪 = " & TextBox1.Text.Trim & "", Nothing, Nothing)
        Dim rds As ReportDataSource = New ReportDataSource("yiTestDataSet_salaryM", salaryDT) 
        ReportViewer1.LocalReport.DataSources.Clear()
        ReportViewer1.LocalReport.DataSources.Add(rds)

        Dim rp1 As ReportParameter = _
        New ReportParameter("Report_Parameter_0", ComboBox3.Text.Trim)
        Dim rp2 As ReportParameter = _
        New ReportParameter("Report_Parameter_1", ComboBox2.Text.Trim)
        Dim rp3 As ReportParameter = _
        New ReportParameter("Report_Parameter_2", TextBox1.Text.Trim)

        ReportViewer1.LocalReport.SetParameters(New ReportParameter() {rp1})
        ReportViewer1.LocalReport.SetParameters(New ReportParameter() {rp2})
        ReportViewer1.LocalReport.SetParameters(New ReportParameter() {rp3})

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

    Private Sub ReportViewer1_Drillthrough(ByVal sender As System.Object, ByVal e As Microsoft.Reporting.WinForms.DrillthroughEventArgs) Handles ReportViewer1.Drillthrough
        Dim myLocalReport As LocalReport
        myLocalReport = e.Report
        'If orderDetailsData Is Nothing Then'

        orderDetailsData = LoadOrderDetailsData(myLocalReport.GetParameters()("Report_Parameter_3").Values(0).Trim)

        'End If'
        myLocalReport.DataSources.Add(New ReportDataSource("yiTestDataSet_salaryM1", orderDetailsData))
    End Sub
End Class

步驟:
STEP1.
各自建立報表 report1.rdlc & report2.rdlc 及報表來源

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



STEP2.
在父報表Report2設定
欄位導覽 跳至報表Report1 (所要鑽取的報表)
並設定要給予的參數(Report_Parameter_3 參數值 是此欄位的值)
(此參數在 Report1 及 Report2 參數名稱要相同)


範例畫面:
主(父)報表Report2


鑽取下的報表Report1


延伸:
若有多個欄位 鑽研不同的Report
則...
  Select Case e.ReportPath
   Case "Parent"
    Me.List_Customers_OrderTotalTableAdapter.Fill(Me.MyDataSet1.List_Customers_OrderTotal)
    RptrDS = New ReportDataSource("DataSet_1", Me.MyDataSet1.Tables("List_Customers_OrderTotal"))

   Case "Level1"
    Me.Show_OrderDetailsTableAdapter.Fill(Me.MyDataSet1.Show_OrderDetails, Integer.Parse(sParameter))
    RptrDS = New ReportDataSource("DataSet_2", Me.MyDataSet1.Tables("Show_OrderDetails"))

   Case "Level2"
    Me.Show_ProductsTableAdapter.Fill(Me.MyDataSet1.Show_Products, Integer.Parse(sParameter))
    RptrDS = New ReportDataSource("DataSet_3", Me.MyDataSet1.Tables("Show_Products"))

   Case Else
    Me.List_Customers_OrderTotalTableAdapter.Fill(Me.MyDataSet1.List_Customers_OrderTotal)
    RptrDS = New ReportDataSource("DataSet_1", Me.MyDataSet1.Tables("List_Customers_OrderTotal"))
  End Select


延伸:
若要多筆資料分頁
則...
Step1.將設計的Table(無資料列 只留表頭或表尾) 拉進 List裡


Step2.設定List屬性


顯示畫面:



自己想的報表:(應該滿多人會被這樣要求的, 不然紙張很浪費!!)
這一張報表剛好佔A4紙3分之1頁面
今天我想把三筆資料弄在同一張頁面

解決方式1:(很笨的方式)
查詢的資料結合,將每三筆資料結合成一組。
例如:

原始DataSet From DataBase
SN     名字     地址
1        陳         屏  
2        張         桃  
......
n        n           n             n


自定義DataSet, 每三筆組成一筆, 透過原始 DataSet 來組合
群組      row1     row2     row3     row4     row5     row6     row7     row8     row9
1           1            陳         屏         2           張         桃         3           x           x
2           4            x            x          5            x           x           6           x           x
......
n           n-2         x            x          n-1         x           x           n           x           x


同一個List,同樣的表格畫三個,並分別放入 row1~row9
第一個表格 row1~row3 第二個表格 row4~row6 第三個表格 row7~row9
分頁設定則是依 "群組" 來分頁


若資料剛好不足3筆成一個record時
則沒資料列的地方要補空白
不然格式會跑掉
而且畫出來的格式 如果有顏色
則必須再報表設計時 設定如果沒有值 得border為白色
(1.不知從程式有沒有辦法處理 報表的設定)
(2.不要用hiden 因為格式也會跑掉)
當然如果紙張不是白色的 就依那個顏色
(1.就嘿嘿嘿...囧 自己慢慢選顏色, 缺點...)

解決方法2:結尾的分頁符號不要勾選


顯示畫面:


注意:
A4紙直式輸出 會自動弄三頁
A4紙橫式輸出 會自動弄二頁
若要輸出為中一刀 寬21 長14
則報表上也要設計為中一刀格式
否則輸出依然是A4格式輸出
中一刀設定


參考:
Create Drill-Through Reports using ReportViewer in ASP.NET 2.0
ReportViewer教程(14)-钻取报表和传入参数
ReportViewer.Drillthrough 事件
使用RDLC报表(四)--钻取式报表
RDLC研取子报表
MSDN報表教學 - 加入鑽研報表
MSDN報表教學 - 第 5 課:加入傳遞至鑽研報表的參數
ReportViewer Control 相關的 Sample Code
如何控制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

RDL(C) Report Design Step by Step 2: SubReport  <-- 這不是鑽研式報表
LocalReport.SubreportProcessing 事件 <-- 這不是鑽研式報表

ReportViewer SubReport 子報表

Difficulty setting ReportParameter

使用 PIVOT 和 UNPIVOT 讓資料多列變成一列
SQL - 使用 PIVOT
SQL資料轉行
續:SQL 資料轉行

待解決的設計: 思考一下別人的報表 如果是我要如何去設計...
請問該如何利用Report Viewer製作如內文所述報表?


沒有留言:

張貼留言