1
Reply

printpreviewdialog print preview problem

john paul magat

john paul magat

Oct 14 2013 9:51 AM
1.1k
hello guys i have a program that will print the data displayed in the datagridview my problem is that when the pages is more than 3 or something the print preview just keep generating print previews... 

here is my work : 



   else if (radioButton2.Checked)
                {



                    e.HasMorePages = true;


                    while (i < dataGridView1.Rows.Count)
                    {




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

                            e.HasMorePages = false;
                        }




                        //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[0].Width, height, 200, 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, 200, dataGridView1.Rows[0].Height), str);

                        e.Graphics.DrawRectangle(Pens.Black, 100 + dataGridView1.Columns[0].Width, height, 300, dataGridView1.Rows[0].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[0].Width, height, 400, dataGridView1.Rows[0].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[0].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++;




                    }
                   
                }
                e.HasMorePages = false;

Answers (1)