Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am creating windows application using c# 2010 here I am using data grid view for reporting purpose, in my data grid view having above 100 rows, I want print multiple page my data grid view values I am write a code on printDocument1_PrintPage event handler, but data’s cannot shown on second page at print time 

how to print Data grid view values at multiple pages

this is my code any one give me just ideas

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
 
            try
            {
 
                Graphics graphic = e.Graphics;
                e.PageSettings.PaperSize = new PaperSize("A4", 850, 1100);
                float pageWidth = e.PageSettings.PrintableArea.Width;
                float pageHeight = e.PageSettings.PrintableArea.Height;
                Font font = new Font("Times New Roman", 10, FontStyle.Bold);
 
                float fontHeight = font.GetHeight();
                int startX = 40;
                int startY = 30;
                int offsetY = 40;
 

                System.Drawing.Font fntString = new Font("Times New Roman", 10, FontStyle.Bold);
                System.Drawing.Font fnthead = new Font("Arial", 20, FontStyle.Bold);
 

                while (s < gvpurchase.Rows.Count)
                {
 

                    if (s % 2 == 0)
                    { 
 
                        for (; s <= 100; s++)
                        {
 
                            offsetY += (int)fontHeight;
 
                            if (offsetY >= pageHeight)
                            {
                                e.HasMorePages = true;
                                offsetY = 0;
                                return;
 

 
                                e.Graphics.DrawString(gvpurchase.Rows[s].Cells[2].Value.ToString(), fntString, Brushes.Black, 70, (s * 93) + 20);
                                e.Graphics.DrawString(gvpurchase.Rows[s].Cells[3].Value.ToString(), fntString, Brushes.Black, 100, (s * 93) + 20);
                                e.Graphics.DrawString(gvpurchase.Rows[s].Cells[4].Value.ToString(), fntString, Brushes.Black, 70, (s * 93) + 40);
                                e.Graphics.DrawString(gvpurchase.Rows[s].Cells[5].Value.ToString(), fntString, Brushes.Black, 100, (s * 93) + 40);
                                e.Graphics.DrawString(gvpurchase.Rows[s].Cells[6].Value.ToString(), fntString, Brushes.Black, 100, (s * 93) + 60);
                                e.Graphics.DrawString(gvpurchase.Rows[s].Cells[7].Value.ToString(), fntString, Brushes.Black, 100, (s * 93) + 80);
                                e.Graphics.DrawString(gvpurchase.Rows[s].Cells[8].Value.ToString(), fntString, Brushes.Black, 100, (s * 93) + 100);
                                e.Graphics.DrawString(gvpurchase.Rows[s].Cells[9].Value.ToString(), fntString, Brushes.Black, 100, (s * 93) + 120);
                                e.Graphics.DrawString(gvpurchase.Rows[s].Cells[10].Value.ToString(), fntString, Brushes.Black, 100, (s * 93) + 140);
 
                            }
 
                            else
                            {
                                e.HasMorePages = false;
                            }
 

                        }
 
                    }
 

 

 
                    else
                    { 
 

                        e.Graphics.DrawString(gvpurchase.Rows[s].Cells[2].Value.ToString(), fntString, Brushes.Black, 370, ((s - 1) * 93) + 20);
                        e.Graphics.DrawString(gvpurchase.Rows[s].Cells[3].Value.ToString(), fntString, Brushes.Black, 400, ((s - 1) * 93) + 20);
                        e.Graphics.DrawString(gvpurchase.Rows[s].Cells[4].Value.ToString(), fntString, Brushes.Black, 370, ((s - 1) * 93) + 40);
                        e.Graphics.DrawString(gvpurchase.Rows[s].Cells[5].Value.ToString(), fntString, Brushes.Black, 400, ((s - 1) * 93) + 40);
                        e.Graphics.DrawString(gvpurchase.Rows[s].Cells[6].Value.ToString(), fntString, Brushes.Black, 400, ((s - 1) * 93) + 60);
                        e.Graphics.DrawString(gvpurchase.Rows[s].Cells[7].Value.ToString(), fntString, Brushes.Black, 400, ((s - 1) * 93) + 80);
                        e.Graphics.DrawString(gvpurchase.Rows[s].Cells[8].Value.ToString(), fntString, Brushes.Black, 400, ((s - 1) * 93) + 100);
                        e.Graphics.DrawString(gvpurchase.Rows[s].Cells[9].Value.ToString(), fntString, Brushes.Black, 400, ((s - 1) * 93) + 120);
                        e.Graphics.DrawString(gvpurchase.Rows[s].Cells[10].Value.ToString(), fntString, Brushes.Black, 400, ((s - 1) * 93) + 140);
 
                       
                    }
 
                }
 
                s++;
 
            }
 
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
 

        }


What I have tried:

How to print data grid view values at multiple pages
Posted
Updated 3-May-17 7:00am

1 solution

Refer link this may help you.

Printing a GridView with Paging[^]
 
Share this answer
 
Comments
Boopalslm 3-May-17 13:02pm    
this is windows application not a web application

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