Start New Discussion within our Software Development Community

Hello
I want to print some data that appear in Form.
I print some data( image, labels....) then at point y = 415, I will print all rows of datagridview, at here all is right, but when i want to print rows that need more page, i had wrong result, this is my trying:

int y = 415;
 for (int i = 0; i < DataGridView1.Rows.Count; i++)
    {
      e.Graphics.DrawLine( Pen, new Point(818, y), new Point(818, y + 35));
      e.Graphics.DrawString(DataGridView1.Rows[i].Cells[0].Value.ToString(), new Font("Arial", 16, FontStyle.Bold), Brushes.Black, new Point(770, y + 7), format1);
     e.Graphics.DrawLine( Pen, new Point(621, y), new Point(621, y + 35));
     e.Graphics.DrawString(DataGridView1.Rows[i].Cells[1].Value.ToString(), new Font("Arial", 16, FontStyle.Bold), Brushes.Black, new Point(530, y + 7), format1);
      e.Graphics.DrawLine(Pen, new Point(424, y), new Point(424, y + 35));
      e.Graphics.DrawString(DataGridView1.Rows[i].Cells[2].Value.ToString(), new Font("Arial", 16, FontStyle.Bold), Brushes.Black, new Point(360, y + 7), format1);
     e.Graphics.DrawLine(new Pen(Color.Black, 3), new Point(227, y), new Point(227, y + 35));
      e.Graphics.DrawString(DataGridView1.Rows[i].Cells[3].Value.ToString(), new Font("Arial", 16, FontStyle.Bold), Brushes.Black, new Point(150, y + 7), format1);
      e.Graphics.DrawLine(Pen, new Point(30, y), new Point(30, y + 35));
      e.Graphics.DrawLine(new Pen(Color.Black, 3), new Point(30, y + 35), new Point(818, y + 35));
                y += 35;
                if (y > e.PageBounds.Height - 25)
                { y = 50;   e.HasMorePages = true;     return;   }
                else
                {            e.HasMorePages = false;           }
            } 

Now, if result need more than one page, i get one page duplicated infinite of times, or i get all results in one page(above of each) if i remove syntax "return;" in if(condittion).
thank you

hello, i found my error, I must declare (int i=0) befor its method decleration.
thank you