Monday, March 5, 2007

Crystal Reports in widows applications (VS 2005, CR X)

Crystal Reports in Windows application using Visual Studio 2005 and CR X:
References required:
Interop.CrystalActiveXReportViewerLib102
CrystalDecisions.CrystalReports.Engine
CrystalDecisions.Enterprise.Framework
CrystalDecisions.Enterprise.InfoStore
CrystalDecisions.ReportSource
CrystalDecisions.Shared
CrystalDecisions.Windows.Forms
------------------------------------------------------------------------------------------------
Create crdocument.rpt file. With the help of wizard we can create the graph (GUI) or text based report very easily.
------------------------------------------------------------------------------------------------
Create a form in windows applications. Drag and Drop "Crystal Report Viewer" control on to the form. (CrystalDecisions.Windows.Forms.CrystalReportViewer)

Some of the Properties of CrystalReportsVIewer we need to modify:
ShowCloseButton : True
ShowExportButton : True
ShowGotoPageButton : True
ShowGroupTreeButton : False
ShowPageNaviageButtons : True
ShowPrintButton : True
ShowRefreshButton : True
ShowTextSearchButton:True
ShowZoomButton : True
------------------------------------------------------------------------------------------------
Now bind the dataset (or datatable or any data source) to Crystalreportviewer control.
------------------------------------------------------------------------------------------------
Code in form1.vb page:
Imports CrystalDecisions.CrystalReports
Imports CrystalDecisions.Enterprise.EnterpriseService
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
------------------------------------------------------------------------------------------------
Dim crData As New ReportDocument
crData.Load(Application.StartupPath() & "\Files\InventoryData.rpt")
///This path can be anything. This is the place where we have .rpt file. When deploying the application to client machine we should deploy .rpts also to the path mentioned here.
------------------------------------------------------------------------------------------------
crData.SetDataSource(Dataset/Datatable instance)
CrystalReportViewer1.ReportSource = crData
------------------------------------------------------------------------------------------------
If there is a parameter to .rpt file then we need to pass value from front end.
Private Sub SetCurrentValuesForParameterField(ByVal myReportDocument As ReportDocument)
Dim Parameter1Val As Object = ParameterValue
Dim parameterFields As New ParameterFields
Dim ParameterField As New ParameterField
ParameterField.ParameterFieldName = "@Parametername"

Dim ParamValue As New ParameterDiscreteValue
ParamValue.Value = Parameter1Val
ParameterField.CurrentValues.Add(ParamValue)
parameterFields.Add(ParameterField)

(This if statement is because sometimes there may not be any data binded to crystal report viewer. In that case it throws 'object reference not set to an instance' type of error.)
If Not CrystalReportViewer1.ParameterFieldInfo Is Nothing Then
CrystalReportViewer1.ParameterFieldInfo = parameterFields
End If

----------------------------------------------------------
To Change the colors of crystal report viewer:
  • *Very important piece of code

Private Sub FormatReportViewer()
Dim thisObj As Object
Dim MyPageView As CrystalDecisions.Windows.Forms.PageView
Dim tcontrol As Windows.Forms.TabControl

For Each thisObj In CrystalReportViewer1.Controls
'yes, of course the names are case sensitive!
'(they are StatusBar and PageView and none other!)
Select Case UCase(thisObj.GetType.Name)
Case "STATUSBAR"
CType(thisObj, StatusBar).Visible = False 'This hides the status bar
Case "PAGEVIEW"
MyPageView = CType(thisObj, CrystalDecisions.Windows.Forms.PageView)
If MyPageView.Controls.Count > 0 Then
tcontrol = CType(MyPageView.Controls(0), TabControl)
With tcontrol
'*****************************************
'these three lines together effectively make tab invisible.
'comment out if you want to show the tab's text.
.ItemSize = New Size(0, 1)
.SizeMode = TabSizeMode.Fixed
.Appearance = TabAppearance.Buttons
'*****************************************
'the crystalreportviewer must be totally drawn before you
'have a tabpage. So you have to call formatreportviewer
'after it has been drawn for the following to work.
If .TabPages.Count > 0 Then
'set color, this is good even if you have chosen
'to make the the tab invisible, does the border.
'the other properties are useless if invisible!
With .TabPages(0)
.BackColor = Color.FromArgb(216, 228, 248)
.Text = "TABTEXT"
End With
End If
End With
End If
End Select
Next

End Sub

1 comment:

Bharathi said...

For any doubt in Crystal Reports for Windows applications feel free to contact me.