I have a small problem with printing reports (made by crystal reports). In Form1 I have a list of customers in a ListView (with checkboxes).
If the users selects one customer (checkes ONE checkBox) the report is shown - and the report it self has on option to print.

If the user selects more then one, I do not want to show both reports, but only directly to print what is on both reports.

How to go into the Form2 class where the report is created, without calling form2.Show(); ?
From Form1 all the required values are gathered into DataSet, which is then passed to Form2 - for every single selected customer.

This is my code I have :

Form1:

private void buttonPrintSelected(object sender, EventArgs e)
{
   for (int j = 0; j < listView1.Items.Count; j++)
   {
      if (listView1.Items[j].Checked == true)
      {
         string CustomerName = null;
         CustomerName = listView1.Items[j].SubItems[1].Text;
         DataSet ds = new DataSet();
         //then I fill the dataSet with all the values I need in the report

	Form2 form2 = new Form2();
	form2.ds_form2 = ds; // I send the dataSet to Form2
	form2.Show();
        form2.Close(); //ADDED:  IF I USE THIS LINE, THE REPORT IS NOT SHOWN WHEN CODE COMES BACK FROM THE form2
     }
  }
}

Form2:

public partial class Form2 : Form
{
     public DataSet ds_Form2 { private get; set; }
     CrystalReport3 cr3 = new CrystalReport3();
     
     private void Form2_Load(object sender, EventArgs e)
     {
           foreach (DataRow dr in ds_Form2.Tables[0].Rows)
          {
                cr3.SetParameterValue("NameSurname", dr[0]);
                //and all the other values bind to their parameters - this works ok
                cr3.PrintOptions.PaperSize = PaperSize.PaperA4;
                crystalReportViewer3.ReportSource = cr3;
                crystalReportViewer3.Refresh();
                 cr3.PrintToPrinter(1, true, 0, 0); //THIS IS HOW I HAVE SET NOW - BUT IT IS NOT OK!
                //Print the report. Set the no. of copies, collate (false or true), startPageN and endPageN . set startPageN and endPageN parameters to 0 to print all pages.
                
          }
     }
}

This code above is now showing every single customer selected from a listView. And is showing the print Dialog - cr3.PrintToPrinter shows that.

But I would like that PrintDialog is shown on the begining (only ones) and then the code prints out all the selected customers. Is there necessary to call the PrintToPrinter every loop? Or is there any other way how to make this work correctly?

Do not use report viewer.

..
TestReport rep = new TestReport();
rep.SetDataSource(ds);
rep.PrintToPrinter(0, true, 0, 0);
..

u can also try print report without preview.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.