By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,321 Members | 1,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,321 IT Pros & Developers. It's quick & easy.

Gridview data's is not printing,Getting empty sheet out....

P: 15
Hi everyone,

I'm trying to take print out from C#(winforms) Gridview,when I give print I'm getting empty sheet out not printing data's on the paper and not getting any error too..Can anyone tell me whats wrong with my code??

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. namespace PrintandPrintPreview
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         #region Member Variables
  6.         StringFormat strFormat; //Used to format the grid rows.
  7.         ArrayList arrColumnLefts = new ArrayList();//Used to save left coordinates of columns
  8.         ArrayList arrColumnWidths = new ArrayList();//Used to save column widths
  9.         int iCellHeight = 0; //Used to get/set the datagridview cell height
  10.         int iTotalWidth = 0; //
  11.         int iRow = 0;//Used as counter
  12.         bool bFirstPage = true; //Used to check whether we are printing first page
  13.         bool bNewPage = false;// Used to check whether we are printing a new page
  14.         int iHeaderHeight = 0; //Used for the header height
  15.         #endregion
  16.  
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.             printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
  21.         }
  22.  
  23.  private void Form1_Load(object sender, EventArgs e)
  24.         {
  25.             // TODO: This line of code loads data into the 'rajiDataSet.orders' table. You can move, or remove it, as needed.
  26.             this.ordersTableAdapter.Fill(this.rajiDataSet.orders);
  27.  
  28.  
  29.         }
  30.  
  31.         private void btnprint_Click(object sender, EventArgs e)
  32.         {
  33.             //Open the print dialog
  34.             PrintDialog printDialog = new PrintDialog();
  35.             printDialog.Document = printDocument1; ;
  36.             printDialog.UseEXDialog = true;
  37.             //Get the document
  38.             if (DialogResult.OK == printDialog.ShowDialog())
  39.             {
  40.                 printDocument1.DocumentName = "Document is printing";
  41.                printDocument1.Print();
  42.             }
  43.             /*
  44.             Note: In case you want to show the Print Preview Dialog instead of 
  45.             Print Dialog then comment the above code and uncomment the following code
  46.             */
  47.  
  48.              //Open the print preview dialog
  49.              //PrintPreviewDialog objPPdialog = new PrintPreviewDialog();
  50.              //objPPdialog.Document = printDocument1;
  51.              //objPPdialog.ShowDialog();
  52.         }
  53.  
  54.         private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
  55.         {
  56.             try
  57.             {
  58.                 //Set the left margin
  59.                 int iLeftMargin = 0;
  60.                 if (e.MarginBounds.Left > 0)
  61.                 {
  62.                     iLeftMargin = e.MarginBounds.Left;
  63.                 }
  64.                 //Set the top margin
  65.                 int iTopMargin = 0;
  66.                 if (e.MarginBounds.Top > 0)
  67.                 {
  68.                     iTopMargin = e.MarginBounds.Top;
  69.                 }
  70.                 //Whether more pages have to print or not
  71.                 bool bMorePagesToPrint = false;
  72.                 int iTmpWidth = 0;
  73.  
  74.                 //For the first page to print set the cell width and header height
  75.                 if (bFirstPage)
  76.                 {
  77.                     foreach (DataGridViewColumn GridCol in dataGridView1.Columns)
  78.                     {
  79.                         iTmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
  80.                             (double)iTotalWidth * (double)iTotalWidth *
  81.                             ((double)e.MarginBounds.Width / (double)iTotalWidth))));
  82.  
  83.                         iHeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
  84.                             GridCol.InheritedStyle.Font, iTmpWidth).Height) + 11;
  85.  
  86.                         // Save width and height of headers
  87.                         arrColumnLefts.Add(iLeftMargin);
  88.                         arrColumnWidths.Add(iTmpWidth);
  89.                         iLeftMargin += iTmpWidth;
  90.                     }
  91.                 }
  92.                 //Loop till all the grid rows not get printed
  93.                 while (iRow <= dataGridView1.Rows.Count - 1)
  94.                 {
  95.                     DataGridViewRow GridRow = dataGridView1.Rows[iRow];
  96.                     //Set the cell height
  97.                     iCellHeight = GridRow.Height + 5;
  98.                     int iCount = 0;
  99.                     //Check whether the current page settings allows more rows to print
  100.                     if (iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
  101.                     {
  102.                         bNewPage = true;
  103.                         bFirstPage = false;
  104.                         bMorePagesToPrint = true;
  105.                         break;
  106.                     }
  107.                     else
  108.                     {
  109.                         if (bNewPage)
  110.                         {
  111.                             //Draw Header
  112.                             e.Graphics.DrawString("Customer Summary",
  113.                                 new Font(dataGridView1.Font, FontStyle.Bold),
  114.                                 Brushes.Black, e.MarginBounds.Left,
  115.                                 e.MarginBounds.Top - e.Graphics.MeasureString("Customer Summary",
  116.                                 new Font(dataGridView1.Font, FontStyle.Bold),
  117.                                 e.MarginBounds.Width).Height - 13);
  118.  
  119.                             String strDate = DateTime.Now.ToLongDateString() + " " +
  120.                                 DateTime.Now.ToShortTimeString();
  121.                             //Draw Date
  122.                             e.Graphics.DrawString(strDate,
  123.                                 new Font(dataGridView1.Font, FontStyle.Bold), Brushes.Black,
  124.                                 e.MarginBounds.Left +
  125.                                 (e.MarginBounds.Width - e.Graphics.MeasureString(strDate,
  126.                                 new Font(dataGridView1.Font, FontStyle.Bold),
  127.                                 e.MarginBounds.Width).Width),
  128.                                 e.MarginBounds.Top - e.Graphics.MeasureString("Customer Summary",
  129.                                 new Font(new Font(dataGridView1.Font, FontStyle.Bold),
  130.                                 FontStyle.Bold), e.MarginBounds.Width).Height - 13);
  131.  
  132.                             //Draw Columns                 
  133.                             iTopMargin = e.MarginBounds.Top;
  134.                             foreach (DataGridViewColumn GridCol in dataGridView1.Columns)
  135.                             {
  136.                                 e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
  137.                                     new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
  138.                                     (int)arrColumnWidths[iCount], iHeaderHeight));
  139.  
  140.                                 e.Graphics.DrawRectangle(Pens.Black,
  141.                                     new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
  142.                                     (int)arrColumnWidths[iCount], iHeaderHeight));
  143.  
  144.                                 e.Graphics.DrawString(GridCol.HeaderText,
  145.                                     GridCol.InheritedStyle.Font,
  146.                                     new SolidBrush(GridCol.InheritedStyle.ForeColor),
  147.                                     new RectangleF((int)arrColumnLefts[iCount], iTopMargin,
  148.                                     (int)arrColumnWidths[iCount], iHeaderHeight), strFormat);
  149.                                 iCount++;
  150.                             }
  151.                             bNewPage = false;
  152.                             iTopMargin += iHeaderHeight;
  153.                         }
  154.                         iCount = 0;
  155.                         //Draw Columns Contents                
  156.                         foreach (DataGridViewCell Cel in GridRow.Cells)
  157.                         {
  158.                             if (Cel.Value != null)
  159.                             {
  160.                                 e.Graphics.DrawString(Cel.Value.ToString(),
  161.                                     Cel.InheritedStyle.Font,
  162.                                     new SolidBrush(Cel.InheritedStyle.ForeColor),
  163.                                     new RectangleF((int)arrColumnLefts[iCount],
  164.                                     (float)iTopMargin,
  165.                                     (int)arrColumnWidths[iCount], (float)iCellHeight),
  166.                                     strFormat);
  167.                             }
  168.                             //Drawing Cells Borders 
  169.                             e.Graphics.DrawRectangle(Pens.Black,
  170.                                 new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
  171.                                 (int)arrColumnWidths[iCount], iCellHeight));
  172.                             iCount++;
  173.                         }
  174.                     }
  175.                     iRow++;
  176.                     iTopMargin += iCellHeight;
  177.                 }
  178.                 //If more lines exist, print another page.
  179.                 if (bMorePagesToPrint)
  180.                     e.HasMorePages = true;
  181.                 else
  182.                     e.HasMorePages = false;
  183.             }
  184.             catch (Exception exc)
  185.             {
  186.                 MessageBox.Show(exc.ToString());
  187.             }
  188.         }
  189.  
  190.     }
  191. }
Mar 23 '15 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.