Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys i have this code that works perfectly but when radiobutton2 is checked and print the data in the datagridview the printPreviewDialog shows up and start generating Print previews continuously and not stopping. anyone knows why??? thanks in advance.


C#
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            int i = 0;


            int width = 0;
            int height = 0;
            int x = 0;
            int y = 0;


            StringFormat str = new StringFormat();
            str.Alignment = StringAlignment.Near;
            str.LineAlignment = StringAlignment.Center;
            str.Trimming = StringTrimming.EllipsisCharacter;
            Pen p = new Pen(Color.Black, 2.5f);

            e.HasMorePages = true;
            e.HasMorePages = false;


            if (radioButton1.Checked)
            {
                while (i < dataGridView1.Rows.Count)
                {
                    if (height > e.MarginBounds.Height)
                    {
                        height = 100;
                        width = 100;
                        e.HasMorePages = true;
                        return;
                    }
                    //Draws a rectangle with same width and height of first column of datagridview. 
                    e.Graphics.DrawRectangle(Pens.Black, 100, 0,
                        dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height);

                    //Fills the above drawn rectangle with a light gray colour just to distinguish the header 
                    e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle
                        (100, 0, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));

                    //Draws a text inside the above drawn rectangle whose value is same of the first column. 

                    e.Graphics.DrawString(dataGridView1.Columns[0].HeaderText,
                        dataGridView1.Font, Brushes.Black, new RectangleF(100, 0,
                        dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);



                    //Draws a rectangle with same width and height of first column of datagridview. 
                    e.Graphics.DrawRectangle(Pens.Black, 200, 0,
                        dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height);

                    //Fills the above drawn rectangle with a light gray colour just to distinguish the header 
                    e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle
                        (200, 0, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));

                    //Draws a text inside the above drawn rectangle whose value is same of the first column. 

                    e.Graphics.DrawString(dataGridView1.Columns[1].HeaderText,
                        dataGridView1.Font, Brushes.Black, new RectangleF(200, 0,
                        dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);




                    //Draws a rectangle with same width and height of first column of datagridview. 
                    e.Graphics.DrawRectangle(Pens.Black, 300, 0,
                        dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height);

                    //Fills the above drawn rectangle with a light gray colour just to distinguish the header 
                    e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle
                        (300, 0, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));

                    //Draws a text inside the above drawn rectangle whose value is same of the first column. 

                    e.Graphics.DrawString(dataGridView1.Columns[2].HeaderText,
                        dataGridView1.Font, Brushes.Black, new RectangleF(300, 0,
                        dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);



                    //Draws a rectangle with same width and height of first column of datagridview. 
                    e.Graphics.DrawRectangle(Pens.Black, 400, 0,
                        dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height);

                    //Fills the above drawn rectangle with a light gray colour just to distinguish the header 
                    e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle
                        (400, 0, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));

                    //Draws a text inside the above drawn rectangle whose value is same of the first column. 

                    e.Graphics.DrawString(dataGridView1.Columns[3].HeaderText,
                        dataGridView1.Font, Brushes.Black, new RectangleF(400, 0,
                        dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);





                    //Draws a rectangle with same width and height of first column of datagridview. 
                    e.Graphics.DrawRectangle(Pens.Black, 500, 0,
                        125, dataGridView1.Rows[0].Height);

                    //Fills the above drawn rectangle with a light gray colour just to distinguish the header 
                    e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle
                        (500, 0, 125, dataGridView1.Rows[0].Height));

                    //Draws a text inside the above drawn rectangle whose value is same of the first column. 

                    e.Graphics.DrawString(dataGridView1.Columns[4].HeaderText,
                        dataGridView1.Font, Brushes.Black, new RectangleF(500, 0,
                        125, dataGridView1.Rows[0].Height), str);





                    height += dataGridView1.Rows[i].Height;


                    e.Graphics.DrawRectangle(Pens.Black, 100, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height);
                    e.Graphics.DrawString(dataGridView1.Rows[i].Cells[0].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new RectangleF(100, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);

                    e.Graphics.DrawRectangle(Pens.Black, 100 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height);
                    e.Graphics.DrawString(dataGridView1.Rows[i].Cells[1].Value.ToString(), dataGridView1.Font, Brushes.Black, new RectangleF(100 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);


                    e.Graphics.DrawRectangle(Pens.Black, 200 + dataGridView1.Columns[1].Width, height, dataGridView1.Columns[1].Width, dataGridView1.Rows[1].Height);
                    e.Graphics.DrawString(dataGridView1.Rows[i].Cells[2].Value.ToString(), dataGridView1.Font, Brushes.Black, new RectangleF(200 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);





                    e.Graphics.DrawRectangle(Pens.Black, 300 + dataGridView1.Columns[2].Width, height, dataGridView1.Columns[2].Width, dataGridView1.Rows[2].Height);
                    e.Graphics.DrawString(dataGridView1.Rows[i].Cells[3].Value.ToString(), dataGridView1.Font, Brushes.Black, new RectangleF(300 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);

                    e.Graphics.DrawRectangle(Pens.Black, 400 + dataGridView1.Columns[3].Width, height, 125, dataGridView1.Rows[3].Height);
                    e.Graphics.DrawString(dataGridView1.Rows[i].Cells[4].Value.ToString(), dataGridView1.Font, Brushes.Black, new RectangleF(400 + dataGridView1.Columns[0].Width, height, 400, dataGridView1.Rows[0].Height), str);


                    width += dataGridView1.Columns[0].Width;
                    i++;


                  

                }
        
            
            
            
            }

            else   if (radioButton2.Checked)
            {






                while (i < dataGridView1.Rows.Count)
                {




                    if (height > e.MarginBounds.Height)
                    {
                        height = 130;
                        width = 100;
                        e.HasMorePages = true;
                        return;
                    }







                    //Draws a rectangle with same width and height of first column of datagridview. 
                    e.Graphics.DrawRectangle(Pens.Black, 100, 0,
                        dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height);

                    //Fills the above drawn rectangle with a light gray colour just to distinguish the header 
                    e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle
                        (100, 0, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));

                    //Draws a text inside the above drawn rectangle whose value is same of the first column. 

                    e.Graphics.DrawString(dataGridView1.Columns[0].HeaderText,
                        dataGridView1.Font, Brushes.Black, new RectangleF(100, 0,
                        dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);





                    //Draws a rectangle with same width and height of first column of datagridview. 
                    e.Graphics.DrawRectangle(Pens.Black, 200, 0,
                        200, dataGridView1.Rows[0].Height);

                    //Fills the above drawn rectangle with a light gray colour just to distinguish the header 
                    e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle
                        (200, 0, 200, dataGridView1.Rows[0].Height));

                    //Draws a text inside the above drawn rectangle whose value is same of the first column. 

                    e.Graphics.DrawString(dataGridView1.Columns[1].HeaderText,
                        dataGridView1.Font, Brushes.Black, new RectangleF(200, 0,
                        200, dataGridView1.Rows[0].Height), str);




                    //Draws a rectangle with same width and height of first column of datagridview. 
                    e.Graphics.DrawRectangle(Pens.Black, 400, 0,
                        dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height);

                    //Fills the above drawn rectangle with a light gray colour just to distinguish the header 
                    e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle
                        (400, 0, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));

                    //Draws a text inside the above drawn rectangle whose value is same of the first column. 

                    e.Graphics.DrawString(dataGridView1.Columns[2].HeaderText,
                        dataGridView1.Font, Brushes.Black, new RectangleF(400, 0,
                        dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);




                    //Draws a rectangle with same width and height of first column of datagridview. 
                    e.Graphics.DrawRectangle(Pens.Black, 500, 0,
                        dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height);

                    //Fills the above drawn rectangle with a light gray colour just to distinguish the header 
                    e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle
                        (500, 0, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));

                    //Draws a text inside the above drawn rectangle whose value is same of the first column. 

                    e.Graphics.DrawString(dataGridView1.Columns[3].HeaderText,
                        dataGridView1.Font, Brushes.Black, new RectangleF(500, 0,
                        dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);



                    //Draws a rectangle with same width and height of first column of datagridview. 
                    e.Graphics.DrawRectangle(Pens.Black, 600, 0,
                        200, dataGridView1.Rows[0].Height);

                    //Fills the above drawn rectangle with a light gray colour just to distinguish the header 
                    e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle
                        (600, 0, 200, dataGridView1.Rows[0].Height));

                    //Draws a text inside the above drawn rectangle whose value is same of the first column. 

                    e.Graphics.DrawString(dataGridView1.Columns[4].HeaderText,
                        dataGridView1.Font, Brushes.Black, new RectangleF(600, 0,
                        dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);




                    height += dataGridView1.Rows[i].Height;


                    e.Graphics.DrawRectangle(Pens.Black, 100, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height);
                    e.Graphics.DrawString(dataGridView1.Rows[i].Cells[0].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new RectangleF(100, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);



                    e.Graphics.DrawRectangle(Pens.Black, 100 + dataGridView1.Columns[1].Width, height, 200, dataGridView1.Rows[1].Height);
                    e.Graphics.DrawString(dataGridView1.Rows[i].Cells[1].Value.ToString(), dataGridView1.Font, Brushes.Black, new RectangleF(100 + dataGridView1.Columns[0].Width, height, 200, dataGridView1.Rows[0].Height), str);

                    e.Graphics.DrawRectangle(Pens.Black, 100 + dataGridView1.Columns[2].Width, height, 300, dataGridView1.Rows[2].Height);
                    e.Graphics.DrawString(dataGridView1.Rows[i].Cells[2].Value.ToString(), dataGridView1.Font, Brushes.Black, new RectangleF(300 + dataGridView1.Columns[0].Width, height, 300, dataGridView1.Rows[0].Height), str);

                    e.Graphics.DrawRectangle(Pens.Black, 100 + dataGridView1.Columns[3].Width, height, 400, dataGridView1.Rows[3].Height);
                    e.Graphics.DrawString(dataGridView1.Rows[i].Cells[3].Value.ToString(), dataGridView1.Font, Brushes.Black, new RectangleF(400 + dataGridView1.Columns[0].Width, height, 400, dataGridView1.Rows[0].Height), str);


                    e.Graphics.DrawRectangle(Pens.Black, 100 + 300, height, 400, dataGridView1.Rows[4].Height);

                    e.Graphics.DrawString(dataGridView1.Rows[i].Cells[4].Value.ToString(), dataGridView1.Font, Brushes.Black, new RectangleF(500 + dataGridView1.Columns[0].Width, height, 500, dataGridView1.Rows[0].Height), str);



                    width += dataGridView1.Columns[0].Width;
                    i++;




                }
            }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900