Imports System.Data.SqlClient
Imports Microsoft.Reporting.WinForms
Public Class Form1
' Connection string definition
Private connString As String = _
"Data Source=.\SQLEXPRESS;AttachDbFilename='|DataDirectory|\myDatabase.mdf';_
Integrated Security=True;User Instance=True"
''' <summary>
''' Form load event
''' </summary>
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Try
' Call the customization method
Call customizeReportViewer(Me.ReportViewer1)
' Add a button to the ReportViewer
Call AddReportViewerButton()
With Me.ReportViewer1.LocalReport
' Report path
.ReportPath = Application.StartupPath & "\..\..\rptProducts.rdlc"
.DataSources.Clear()
' Set the parameters
Dim parameters(1) As ReportParameter
parameters(0) = New ReportParameter("ApplicationUser", "jpaulino")
parameters(1) = New ReportParameter("ApplicationLevel", "Administrator")
.SetParameters(parameters)
End With
' ----------------------------------------------------
' Datasource for the main report (where price > 200)
' ----------------------------------------------------
Dim SQL As String = "SELECT * FROM products WHERE price > @price"
Using da As New SqlDataAdapter(SQL, connString)
da.SelectCommand.Parameters.Add("@price", SqlDbType.Int).Value = 200
Using ds As New DataSet
da.Fill(ds, "products")
' You must use the same name as defined in the report
' Data Source Definition
Dim rptDataSource As New ReportDataSource_
("dsReport_products", ds.Tables("products"))
Me.ReportViewer1.LocalReport.DataSources.Add(rptDataSource)
End Using
End Using
' ----------------------------------------------------
' Datasource for the Chart
' ----------------------------------------------------
Dim SQL_Chart As String = "SELECT [group], SUM(quantity) AS _
Total FROM products GROUP BY [group]"
Using da As New SqlDataAdapter(SQL_Chart, connString)
Using ds As New DataSet
da.Fill(ds, "groupTotal")
Dim rptDataSource As New ReportDataSource("dsReport_groupTotal", _
ds.Tables("groupTotal"))
Me.ReportViewer1.LocalReport.DataSources.Add(rptDataSource)
End Using
End Using
' Refresh the report
ReportViewer1.RefreshReport()
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
AddHandler ReportViewer1.LocalReport.SubreportProcessing, _
AddressOf SubreportProcessingEvent
''' <summary>
''' When the subreport is being processed/loaded, fills the datasource
''' </summary>
Sub SubreportProcessingEvent(ByVal sender As Object, _
ByVal e As SubreportProcessingEventArgs)
Try
Dim SQL As String = "SELECT [group], SUM(quantity) AS _
Total FROM products GROUP BY [group]"
Using da As New SqlDataAdapter(SQL, connString)
Using ds As New DataSet
da.Fill(ds, "groupTotal")
Dim rptDataSource As New ReportDataSource_
("dsReport_groupTotal", ds.Tables("groupTotal"))
e.DataSources.Add(rptDataSource)
End Using
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
''' <summary>
''' Loops through all of ReportViewer controls and customize them
''' </summary>
''' <remarks></remarks>
Sub customizeReportViewer(ByVal ctrl As Control)
For Each c As Control In ctrl.Controls
' ----------------------------------------------------
' Check the text of the available labels
' ----------------------------------------------------
If TypeOf c Is Label Then
Dim lbl As Label = DirectCast(c, Label)
Select Case lbl.Name
Case "LblGeneratingReport"
lbl.Text = "My report is loading now ... "
Case Else
' You can add more customizations
End Select
End If
' ----------------------------------------------------
' Change the text in the ToolStrip to Portuguese
' ----------------------------------------------------
If TypeOf c Is ToolStrip Then
Dim ts As ToolStrip = DirectCast(c, ToolStrip)
For Each item As ToolStripItem In ts.Items
Select Case item.Text
Case "Find"
item.Text = "Pesquisar"
Case "Next"
item.Text = "Próximo"
Case "of"
item.Text = "de"
Case Else
' You can add more customizations
End Select
Next
End If
' If the control has child controls
If c.HasChildren Then
customizeReportViewer(c)
End If
Next
End Sub
' Call the customization method
Call customizeReportViewer(Me.ReportViewer1)
''' <summary>
''' Find the main ToolStrip and add a new button on the right side
''' </summary>
Sub AddReportViewerButton()
Dim ts() As Control = Me.ReportViewer1.Controls.Find("toolStrip1", True)
If ts IsNot Nothing Then
Dim tsItem As ToolStrip = DirectCast(ts(0), ToolStrip)
Dim item As New ToolStripButton
item.Name = "newButton"
item.Text = "New Button"
item.BackColor = Color.Green
item.ForeColor = Color.White
item.Alignment = ToolStripItemAlignment.Right
tsItem.Items.Add(item)
AddHandler item.Click, AddressOf newButtonClick
End If
End Sub
''' <summary>
''' Shows a messagebox when the new button of
''' the Reportviewer is pressed
''' </summary>
Sub newButtonClick()
MessageBox.Show("This is my new button!")
End Sub
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)