Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
if the pages is more than 1 and i press a button the print preview don't stop generating print preview it will only stop if press the cancel button, then it will display the table with more than 1 pages but the data inside the tables is just the same. how can i solve this print preview problem that keeps generating print preview and the repetition of data?? thank you in advance :D

C#
private void button3_Click(object sender, EventArgs e)
 {  printPreviewDialog1.ShowDialog();  }


here is the code in my PrintDocument:

C#
else if (radioButton2.Checked)
            {

                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);



                    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);


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

                }

            }
Posted

1 solution

I saw that problem appear when I attempted to correct a printpreview issue with page numbering that continued vs. restarting when printing the hardcopy. In case this helps you, in DrawRows function, I needed to reset PageNumber and mColumnPoints.
   CurrentRow = 0
   mColumnPoint += 1
 'Continue to print the next group of columns
If mColumnPoint = mColumnPoints.Count Then
  'Which means all columns are printed
    mColumnPoint = 0

    PageNumber = 0
    mColumnPoints.Clear()

    Return False
Else
    Return True
End If


Good luck!
 
Share this answer
 
v3

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