How to print multiple pages with PrintDocument in C # using a cycle?

1

Here what I tried:

 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            try
            {             
                string reporte = "";
                if (checkBox1.Checked == true)
                {
                    reporte = "Por Codigo";
                }
                if (checkBox3.Checked == true)
                {
                    reporte = "Por Factura";
                }
                if (checkBox2.Checked == true)
                {
                    reporte = "Por Proveedor";
                }
                int contador = 0;
                double total = 0;

                ///// Se establece el tipo de Fuente        
                Font Fuente = new Font("Verdana", 8);
                Font FuenteEncabezados = new Font("Verdana", 10);
                Font FuenteF = new Font("Verdana", 8);

                //titulo
                Font fuente_titulo = new Font("Verdana", 16, FontStyle.Bold);

                ///// Se establece el Color de Fuente
                Brush Brocha = Brushes.Black;
                ///// Se establece las cordenadas
                int Y = 35;

                //se genera el cuadro de la fecha y #cotizacion
                e.Graphics.DrawRectangle(Pens.Black, 650, 45, 174, 40);



                e.Graphics.DrawString(Program.GGG.empresa, fuente_titulo, Brocha, 190, 20);
                Y = Convert.ToInt32(Y + Fuente.GetHeight());
                //muestra el logo
                Bitmap logo = Sistema.Properties.Resources.logo;
                e.Graphics.DrawImage(logo, 20, 20, 150, 100);

                e.Graphics.DrawString("Reporte de Compras", fuente_titulo, Brocha, (e.MarginBounds.Width /2), 115);

                e.Graphics.DrawString("Tel: " + Program.GGG.telefono_empresa, Fuente, Brocha, 190, 45);
                Y = Convert.ToInt32(Y + Fuente.GetHeight());

                e.Graphics.DrawString("RTN: " + Program.GGG.RTN, Fuente, Brocha, 190, 65);
                Y = Convert.ToInt32(Y + Fuente.GetHeight());

                e.Graphics.DrawString("Direccion: " + Program.GGG.direccion, Fuente, Brocha, 190, 85);
                Y = Convert.ToInt32(Y + Fuente.GetHeight());

                e.Graphics.DrawString("Fecha" , FuenteEncabezados, Brocha, 20, 146);
                Y = Convert.ToInt32(Y + Fuente.GetHeight());

                e.Graphics.DrawString("Codigo", FuenteEncabezados, Brocha, 130, 146);
                Y = Convert.ToInt32(Y + Fuente.GetHeight());

                e.Graphics.DrawString("Descripcion", FuenteEncabezados, Brocha, 200, 146);
                Y = Convert.ToInt32(Y + Fuente.GetHeight());

                e.Graphics.DrawString("Cantidad", FuenteEncabezados, Brocha, 550, 146);
                Y = Convert.ToInt32(Y + Fuente.GetHeight());

                e.Graphics.DrawString("Precio", FuenteEncabezados, Brocha, 650, 146);
                Y = Convert.ToInt32(Y + Fuente.GetHeight());

                e.Graphics.DrawString("Total", FuenteEncabezados, Brocha, 750, 146);
                Y = Convert.ToInt32(Y + Fuente.GetHeight());

                e.Graphics.DrawString("Reporte:     " + reporte, Fuente, Brocha, 650, 45);
                e.Graphics.DrawString("Fecha:" + DateTime.Now.ToString(), FuenteF, Brocha, 650, 65);

                //se genera el cuadro de los articulos
                foreach (DataGridViewRow row in dataGridView1.Rows)

                {
                    if (row.Cells[1].Value != DBNull.Value || row.Cells[2].Value != DBNull.Value || row.Cells[3].Value != DBNull.Value || row.Cells[4].Value != DBNull.Value || row.Cells[5].Value != DBNull.Value) {
                        e.Graphics.DrawString(Convert.ToDateTime(row.Cells[0].Value).ToShortDateString(), Fuente, Brocha, 23, Y);
                        e.Graphics.DrawString(row.Cells[1].Value.ToString(), Fuente, Brocha, 130, Y);
                        e.Graphics.DrawString(row.Cells[2].Value.ToString(), Fuente, Brocha, 200, Y);
                        e.Graphics.DrawString(row.Cells[3].Value.ToString(), Fuente, Brocha, 550, Y);
                        e.Graphics.DrawString(row.Cells[4].Value.ToString(), Fuente, Brocha, 650, Y);
                        e.Graphics.DrawString(row.Cells[5].Value.ToString(), Fuente, Brocha, 750, Y);
                    }


                    Y = Y + 20;
                    contador++;

                    if (Y >= 500)
                    {
                        e.HasMorePages = true;
                        Y = 30;
                        return;
                    }else
                    {
                        e.HasMorePages = false;
                    }
                }


                //se genera el cuadro de los articulos
                e.Graphics.DrawLine(Pens.Black, 20, 145, 825, 145);
                e.Graphics.DrawLine(Pens.Black, 20, 165, 825, 165);

                e.Graphics.DrawLine(Pens.Black, 20, 145, 20, Y);
                e.Graphics.DrawLine(Pens.Black, 825, 145, 825, Y);


                //se genera el cuadro de los totales
                e.Graphics.DrawRectangle(Pens.Black, 20, Y, 805, 25);

                e.Graphics.DrawString("Total: " + suma.ToString("N2"), Fuente, Brocha, 640, Y+2);
                Y = Convert.ToInt32(Y + Fuente.GetHeight());
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

The idea is to print a datagridview with information, this datagrid I go through with a ForEach, but when it runs it enters a cycle of never acbar. Always keep printing. I can not make it when I shoot the e.HasMorePages = true follow another page. I found this question in SO in English but I can not adapt it to my code . If something needs to be changed, we can do it.

I hope your help. Watch out for any questions.

    
asked by Luis Fernando 20.04.2018 в 13:16
source

0 answers