How to do a Print Preview in C# crystal reports visual studio 2010

eric_13 picture eric_13 · Aug 17, 2012 · Viewed 6.9k times · Source

I have an application which has two options, to print the report, or to preview the report. I have the following code to print the report-

FCPMS.Reports.rptPanelStudy crtrptPanelStudy = new FCPMS.Reports.rptPanelStudy();
crtrptPanelStudy.FileName = tReportFileName; -(refers to the file location on the  computer)
crtrptPanelStudy.DataDefinition.FormulaFields["PANELNUM"].Text = "'5'";
foreach (Table tblTable in crtrptPanelStudy.Database.Tables)
{
    tiInfo = tblTable.LogOnInfo;
    tiInfo.ConnectionInfo = ciConnection;
    tblTable.ApplyLogOnInfo(tiInfo);
}

crtrptPanelStudy.PrintToPrinter(1, false, 0, 0);

This works properly and prints the report. But I am having trouble finding similar code to simply get the printer preview to open with this report. Does anyone know how I could do this? All I need is the printer preview to pop up without the report actually printing. Any help would be appreciated.

I also tried to use Print Dialog -

PrintPreviewDialog printpreview = new PrintPreviewDialog();
printpreview.Document = crtrptFlowRangeSummary;
printpreview.ShowDialog(); 

But got this error on the second line

Error 35 Cannot implicitly convert type FCPMS.Reports.rptFlowRangeSummary to System.Drawing.Printing.PrintDocument

Answer

MethodMan picture MethodMan · Aug 17, 2012

I am not sure how you are implementing or declaring crtrptFlowRangeSummary but you can try this code below.

this is also assuming you have the event declared like this

private void CrystalReportPrintPreviewForm_Load(object sender, EventArgs e)
{
    if(crtrptFlowRangeSummary !=null)
    crystalReportViewer1.ReportSource = crtrptFlowRangeSummary;
}


CrystalReportPrintPreviewForm crystalReportPrintPreview = 
       new CrystalReportPrintPreviewForm(crtrptFlowRangeSummary);
crystalReportPrintPreview.ShowDialog();