摘要:如何動態選擇報表欄位
之前不知道在哪裡看到了這個需求,現在來實作看看。
首先,在畫面中新增一個GridView,並且在其Header中的每個欄位放置一個checkbox。
然後設定四個全域參數,這四個參數會依照接收的值,判斷欄位是否要顯示(這裡預設值為1(顯示)):
接下來在某欄位的Visibility的hiden屬性中,輸入下列運算式,然後存檔:
然後只要在程式碼中在作程式碼設定:
(這段程式碼很醜,應用的話請美化一下,像CheckBox_CheckedChanged事件可以共同使用一個就好)
- Dim P1 As Integer = 0
- Dim P2 As Integer = 0
- Dim P3 As Integer = 0
- Dim P4 As Integer = 0
- Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
- checkAll()
- End Sub
- Protected Sub CheckBox2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
- checkAll()
- End Sub
- Protected Sub CheckBox3_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
- checkAll()
- End Sub
- Protected Sub CheckBox4_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
- checkAll()
- End Sub
- Sub checkAll()
- If CType(GridView1.HeaderRow.Cells(0).FindControl("CheckBox1"), CheckBox).Checked = False Then
- P1 = 0
- Else
- P1 = 1
- End If
- If CType(GridView1.HeaderRow.Cells(1).FindControl("CheckBox2"), CheckBox).Checked = False Then
- P2 = 0
- Else
- P2 = 1
- End If
- If CType(GridView1.HeaderRow.Cells(2).FindControl("CheckBox3"), CheckBox).Checked = False Then
- P3 = 0
- Else
- P3 = 1
- End If
- If CType(GridView1.HeaderRow.Cells(3).FindControl("CheckBox4"), CheckBox).Checked = False Then
- P4 = 0
- Else
- P4 = 1
- End If
- Dim ss(3) As ReportParameter
- ss(0) = New ReportParameter("P1", P1)
- ss(1) = New ReportParameter("P2", P2)
- ss(2) = New ReportParameter("P3", P3)
- ss(3) = New ReportParameter("P4", P4)
- ReportViewer1.LocalReport.SetParameters(ss)
- ReportViewer1.LocalReport.Refresh()
- End Sub
Dim P1 As Integer = 0
Dim P2 As Integer = 0
Dim P3 As Integer = 0
Dim P4 As Integer = 0
Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
checkAll()
End Sub
Protected Sub CheckBox2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
checkAll()
End Sub
Protected Sub CheckBox3_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
checkAll()
End Sub
Protected Sub CheckBox4_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
checkAll()
End Sub
Sub checkAll()
If CType(GridView1.HeaderRow.Cells(0).FindControl("CheckBox1"), CheckBox).Checked = False Then
P1 = 0
Else
P1 = 1
End If
If CType(GridView1.HeaderRow.Cells(1).FindControl("CheckBox2"), CheckBox).Checked = False Then
P2 = 0
Else
P2 = 1
End If
If CType(GridView1.HeaderRow.Cells(2).FindControl("CheckBox3"), CheckBox).Checked = False Then
P3 = 0
Else
P3 = 1
End If
If CType(GridView1.HeaderRow.Cells(3).FindControl("CheckBox4"), CheckBox).Checked = False Then
P4 = 0
Else
P4 = 1
End If
Dim ss(3) As ReportParameter
ss(0) = New ReportParameter("P1", P1)
ss(1) = New ReportParameter("P2", P2)
ss(2) = New ReportParameter("P3", P3)
ss(3) = New ReportParameter("P4", P4)
ReportViewer1.LocalReport.SetParameters(ss)
ReportViewer1.LocalReport.Refresh()
End Sub
這樣就可以根據Header的勾選狀況來決定ReportViewer顯示狀況:
ps:這邊有兩個要注意的地方(我使用VS2008)
1. reportviewer的rdlc樣本設計方式跟rdl樣板設計方式變得一樣了。
2. 更炫的是,終於盼到了本機端報表(rdlc)也能列印的功能了
如果您有微軟技術開發的問題,可以到MSDN Forum發問。
如果您有微軟IT管理的問題,可以到TechNet Forum發問喔。