L. Levine
L. Levine

Reputation: 155

Printing w/o displaying Print Option Dialog Box Crystal Report Viewer in my C#

I have a C# application that has a Crystal Report Viewer in a form. I call that form and pass to it a value that I use to update a parameter field associated with the Crystal Report so only a specific record gets displayed.

That all works fine, and I can call the Viewers PrintReport method to print the report without operator intervention.

CrystalForm fs = new CrystalForm(); 
fs.SetCrystalOrderNumParameter(ItemID);

 public partial class CrystalForm : Form
    {
        public CrystalForm()
        {
            InitializeComponent();
        }

        public void SetCrystalOrderNumParameter(string ItemID)
        {
            ParameterFields paramFields = new ParameterFields();

            ParameterField paramField = new ParameterField();
            ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();

            paramField.Name = "ItemID";
            paramDiscreteValue.Value = ItemID;
            paramField.CurrentValues.Add(paramDiscreteValue);
            paramFields.Add(paramField);


            crystalReportViewer1.ParameterFieldInfo = paramFields;

            crystalReportViewer1.PrintReport();

        }
    }

The problem I am having is that I want to be able to pass a value to the Crystal Report so that it uses this # to determine how many copies of the report should be printed.

Is there a way to do this using a Crystal Report Viewer ?

Thank you in advance for your assistance.

Upvotes: 1

Views: 4695

Answers (2)

Brandon Kramer
Brandon Kramer

Reputation: 1118

The crystal report viewer itself does not provide this functionality.

To control the number of pages without a dialog popup, you will have to use the CrystalDecisions.CrystalReports.Engine.ReportDocument class. This class is what the CrystalReports API uses to represent an actual Crystal Report, and it is usually assigned to the ReportSource property of the viewer to tell the viewer what report to display. You may already be using this object, but I cannot see where you are assigning the report source from the code you have shared.

The ReportDocument class has a PrintToPrinter method, and the second overload looks like this: void PrintToPrinter(int nCopies, bool collated, int startPageN, int endPageN)

The nCopies parameter allows you to specify how many copies of the report to print. The print settings for the report will default to whatever the report's printer settings are, though they may be changed via the PrintOptions property of the ReportDocument instance.

Here is a simple code example, where rptPath is the path to your crystal report:

var rpt = new ReportDocument();
rpt.Load(rptPath);
rpt.PrintOptions.PrinterName = "MyPrinterName";
//This will print 2 copies of the crystal report.
//You can use the nCopies (first) parameter to specify whatever #
//of copies you wish.
rpt.PrintToPrinter(2, false, 0, 0);

Additionally, when a ReportDocument is used to load a Crystal Report via the Load() method, it automatically populates its ParameterFields collection with all of the parameters that the report expects. You can then set a parameter's value like reds displayed:

rpt.SetParameterValue("ParameterName", value);

Finally, if you want to show this report with the viewer, all you have to do is the following:

viewer.ReportSource = rpt;

Where rpt is the ReportDocument object representing the report, and viewer is the CrystalDecisions.Windows.Forms.CrystalReportViewer you wish to use to display the report.

Upvotes: 1

Vijunav Vastivch
Vijunav Vastivch

Reputation: 4191

By passing a variable from code behind to cr report parameter:

It could be be like this:

CRPT.SetParameterValue("syear", Servercls.year);
CRPT.SetParameterValue("smonth", Servercls.month);
CRPT.SetParameterValue("sday", Servercls.day);

See this link for more info

Upvotes: 0

Related Questions