0

I have datagridview in a form and it has multiple rows in it. And I have a following code

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        float i = 290;
        int count = 1;
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {

            e.Graphics.DrawString(count.ToString(), new Font("Times New Roman", 10), new SolidBrush(Color.Black), 13.0F, i);
            e.Graphics.DrawString(Convert.ToString(row.Cells[0].Value), new Font("Times New Roman", 10), new SolidBrush(Color.Black), 70.0F, i);
            e.Graphics.DrawString(Convert.ToString(row.Cells[1].Value), new Font("Times New Roman", 10), new SolidBrush(Color.Black), 185.0F, i);
            e.Graphics.DrawString(Convert.ToString(row.Cells[2].Value), new Font("Times New Roman", 10), new SolidBrush(Color.Black), 275.0F, i);
            e.Graphics.DrawString(Convert.ToString(row.Cells[3].Value), new Font("Times New Roman", 10), new SolidBrush(Color.Black),650.0F, i);
            e.Graphics.DrawString(Convert.ToString(row.Cells[4].Value), new Font("Times New Roman", 10), new SolidBrush(Color.Black), 730.0F, i);
            i = i + 13;
            count++;
        }
        e.Graphics.DrawString("Total Quantity", new Font("Constantia", 15, FontStyle.Bold), new SolidBrush(Color.Black), 13.0F, i + 15);
        e.Graphics.DrawString("Total Amount", new Font("Constantia", 15, FontStyle.Bold), new SolidBrush(Color.Black), 550.0F, i + 15);
        e.Graphics.DrawString(label10.Text, new Font("Times New Roman", 15, FontStyle.Bold), new SolidBrush(Color.Black), 730.0F, i+15);
        e.Graphics.DrawString(label9.Text, new Font("Times New Roman", 15, FontStyle.Bold), new SolidBrush(Color.Black), 180.0F, i + 15);
    }

This code prints fine for 1 page. But after that, the remaining contents doesn't get print.

So if someone can help me with the code that I can use to print all the content from datagridview I shall be highly grateful.

Thanks in advance.

  • Never done it, but it looks like you need to [calculate the number of lines](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-print-a-multi-page-text-file-in-windows-forms?view=netframeworkdesktop-4.8) that fit on a page and use `HasMorePages` property. – Crowcoder Mar 10 '21 at 18:02
  • 1
    you are making far, far too many unique new Font objects in that code - by not disposing of them, you app is going to leak badly. – Ňɏssa Pøngjǣrdenlarp Mar 10 '21 at 18:11
  • I agree with @ŇɏssaPøngjǣrdenlarp Try putting those fonts in variables and reuse them. – Prashant Tiwari Mar 10 '21 at 18:52
  • https://stackoverflow.com/questions/11544989/what-does-hasmorepages-printpageeventargs-property-do-exactly – dr.null Mar 10 '21 at 22:11

0 Answers0