Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to print datagridview with bindingnavigator on each page using windows application

[EDIT - below code has been moved from OP's comment]

C#
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
       {
int iLeftMargin = e.MarginBounds.Left;
               //Set the top margin
               int iTopMargin = e.MarginBounds.Top;
               //Whether more pages have to print or not
               bool bMorePagesToPrint = false;
               int iTmpWidth = 0;

               //For the first page to print set the cell width and header height
               if (bFirstPage)
               {
                   foreach (DataGridViewColumn GridCol in dataGridView1.Columns)
                   {
                       iTmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
                           (double)iTotalWidth * (double)iTotalWidth *
                           ((double)e.MarginBounds.Width / (double)iTotalWidth))));

                       iHeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
                           GridCol.InheritedStyle.Font, iTmpWidth).Height) + 11;

                       // Save width and height of headers
                       arrColumnLefts.Add(iLeftMargin);
                       arrColumnWidths.Add(iTmpWidth);
                       iLeftMargin += iTmpWidth;
                   }
               }
               //Loop till all the grid rows not get printed
               while (iRow <= dataGridView1.Rows.Count - 1)
               {
                   DataGridViewRow GridRow = dataGridView1.Rows[iRow];
                   //Set the cell height
                   iCellHeight = GridRow.Height + 5;
                   int iCount = 0;
                   //Check whether the current page settings allows more rows to print
                   if (iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                   {
                       bNewPage = true;
                       bFirstPage = false;
                       bMorePagesToPrint = true;
                       break;
                   }
                   else
                   {
                       if (bNewPage)
                       {
                           //Draw Header
                           e.Graphics.DrawString("Summary",
                               new Font(dataGridView1.Font, FontStyle.Bold),
                               Brushes.Black, e.MarginBounds.Left,
                               e.MarginBounds.Top - e.Graphics.MeasureString("Summary",
                               new Font(dataGridView1.Font, FontStyle.Bold),
                               e.MarginBounds.Width).Height - 13);

                           String strDate = DateTime.Now.ToLongDateString() + " " +
                               DateTime.Now.ToShortTimeString();
                           //Draw Date
                           e.Graphics.DrawString(strDate,
                               new Font(dataGridView1.Font, FontStyle.Bold), Brushes.Black,
                               e.MarginBounds.Left +
                               (e.MarginBounds.Width - e.Graphics.MeasureString(strDate,
                               new Font(dataGridView1.Font, FontStyle.Bold),
                               e.MarginBounds.Width).Width),
                               e.MarginBounds.Top - e.Graphics.MeasureString("Customer Summary",
                               new Font(new Font(dataGridView1.Font, FontStyle.Bold),
                               FontStyle.Bold), e.MarginBounds.Width).Height - 13);

                           //Draw Columns
                           iTopMargin = e.MarginBounds.Top;
                           foreach (DataGridViewColumn GridCol in dataGridView1.Columns)
                           {
                               e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
                                   new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
                                   (int)arrCo
Posted
Updated 28-Aug-15 3:34am
v2
Comments
Maciej Los 28-Aug-15 9:21am    
What have you tried? Where are you stuck?
Sathish km 28-Aug-15 9:30am    
--- deleted ---
Sathish km 28-Aug-15 9:30am    
--- moved to the question ---
Maciej Los 28-Aug-15 9:35am    
Use "Improve question" widget to update your question!

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