Introduction
This is a simple and small user control that will help programmers to pass discrete parameters to Crystal Reports without writing lengthy code.
In this control, we are having two properties and one method:
The Code
Before discussing the technical stuff, first we will see the pre-requirements. We need to add the following .NET references:
Crystaldecisions.crystalreports.engine
Crystaldecisions.reportsource
Crystaldecisions.shared
Crystaldecisions.windows.forms
Declaration
Dim pFields As New ParameterFields()
Dim pField As New ParameterField()
Dim disVal As New ParameterDiscreteValue()
Dim rVal As New ParameterRangeValue()
Assigning the Parameter Name and its Values
pField.ParameterFieldName = "MyName"
disVal.Value = "CodeProject"
pField.CurrentValues.Add(disVal)
With the example below, we can pass the range value:
rVal.StartValue = 150
rVal.EndValue = 200
pField.CurrentValues.Add(rVal)
paramFields.Add(paramField)
Set the parameter fields collection into the viewer control:
crystalReportViewer1.ParameterFieldInfo = pFields
crystalReportViewer1.ReportSource = "c:\codeproject\About.rpt"
How to use this control
Step #1
Re-build the control.
Step #2
- Open a new project, add a
crystalreportviewer
control and a Button
.
- Add this control by going to the toolbox control. Select the ‘General’ Tab, then right click on the mouse, then you will find an option in the popup as ‘Customize toolbox’. This will bring a window. Then select the tab called ‘.NET framework components’. Using the Browse button, add the UserControl. Then it will show it on the toolbox.
- Then add the control in our current form.
- Then write the following code for the
Button
as follows:
Note: If necessary, change the path of the report filename.
CryRepControl1.ReportFileName = "d:\VB.Net\AnyTest\MyProfile.rpt"
CryRepControl1.ParameterFields(0) = "MyName;" & "J. Franklin Kennedy"
CryRepControl1.ParameterFields(1) = "MyAge;" & "31"
CryRepControl1.ParameterFields(2) = "DOB;" & "09/01/1973"
CryRepControl1.ParameterFields(3) = "Married;" & "True"
RptViewer1.ParameterFieldInfo = CryRepControl1.ParamFields
RptViewer1.ReportSource = CryRepControl1.CrySet()
- Run the project.
Note: The report ‘MyProfile.rpt’ is also in the ZIP file.