Advertisement
Guest User

Untitled

a guest
Dec 16th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 KB | None | 0 0
  1. private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
  2. {
  3. try
  4. {
  5. strFormat = new StringFormat();
  6. strFormat.Alignment = StringAlignment.Near;
  7. strFormat.LineAlignment = StringAlignment.Center;
  8. strFormat.Trimming = StringTrimming.EllipsisCharacter;
  9.  
  10. arrColumnLefts.Clear();
  11. arrColumnWidths.Clear();
  12. iCellHeight = 0;
  13. iRow = 0;
  14. bFirstPage = true;
  15. bNewPage = true;
  16.  
  17. // Calculating Total Widths
  18. iTotalWidth = 0;
  19. foreach (DataGridViewColumn dgvGridCol in dataGridView1.Columns)
  20. {
  21. iTotalWidth += dgvGridCol.Width;
  22. }
  23. }
  24. catch (Exception ex)
  25. {
  26. MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  27. }
  28. }
  29.  
  30. private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  31. {
  32. try
  33. {
  34. //Set the left margin
  35. int iLeftMargin = e.MarginBounds.Left;
  36. //Set the top margin
  37. int iTopMargin = e.MarginBounds.Top;
  38. //Whether more pages have to print or not
  39. bool bMorePagesToPrint = false;
  40. int iTmpWidth = 0;
  41.  
  42. //For the first page to print set the cell width and header height
  43. if (bFirstPage)
  44. {
  45. foreach (DataGridViewColumn GridCol in dataGridView1.Columns)
  46. {
  47. iTmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
  48. (double)iTotalWidth * (double)iTotalWidth *
  49. ((double)e.MarginBounds.Width / (double)iTotalWidth))));
  50.  
  51. iHeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
  52. GridCol.InheritedStyle.Font, iTmpWidth).Height) + 11;
  53.  
  54. // Save width and height of headres
  55. arrColumnLefts.Add(iLeftMargin);
  56. arrColumnWidths.Add(iTmpWidth);
  57. iLeftMargin += iTmpWidth;
  58. }
  59. }
  60. //Loop till all the grid rows not get printed
  61. while (iRow <= dataGridView1.Rows.Count - 1)
  62. {
  63. DataGridViewRow GridRow = dataGridView1.Rows[iRow];
  64. //Set the cell height
  65. iCellHeight = GridRow.Height + 5;
  66. int iCount = 0;
  67. //Check whether the current page settings allo more rows to print
  68. if (iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
  69. {
  70. bNewPage = true;
  71. bFirstPage = false;
  72. bMorePagesToPrint = true;
  73. break;
  74. }
  75. else
  76. {
  77. if (bNewPage)
  78. {
  79. //Draw Header
  80. e.Graphics.DrawString("Reports", new Font(dataGridView1.Font, FontStyle.Bold),
  81. Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top -
  82. e.Graphics.MeasureString("Reports", new Font(dataGridView1.Font,
  83. FontStyle.Bold), e.MarginBounds.Width).Height - 13);
  84.  
  85. String strDate = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();
  86. //Draw Date
  87. e.Graphics.DrawString(strDate, new Font(dataGridView1.Font, FontStyle.Bold),
  88. Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width -
  89. e.Graphics.MeasureString(strDate, new Font(dataGridView1.Font,
  90. FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top -
  91. e.Graphics.MeasureString("Reports", new Font(new Font(dataGridView1.Font,
  92. FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13);
  93.  
  94. //Draw Columns
  95. iTopMargin = e.MarginBounds.Top;
  96. foreach (DataGridViewColumn GridCol in dataGridView1.Columns)
  97. {
  98. e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
  99. new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
  100. (int)arrColumnWidths[iCount], iHeaderHeight));
  101.  
  102. e.Graphics.DrawRectangle(Pens.Black,
  103. new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
  104. (int)arrColumnWidths[iCount], iHeaderHeight));
  105.  
  106. e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,
  107. new SolidBrush(GridCol.InheritedStyle.ForeColor),
  108. new RectangleF((int)arrColumnLefts[iCount], iTopMargin,
  109. (int)arrColumnWidths[iCount], iHeaderHeight), strFormat);
  110. iCount++;
  111.  
  112. }
  113. bNewPage = false;
  114. iTopMargin += iHeaderHeight;
  115. }
  116. iCount = 0;
  117. //Draw Columns Contents
  118. foreach (DataGridViewCell Cel in GridRow.Cells)
  119. {
  120. if (Cel.Value != null)
  121. {
  122.  
  123. e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
  124. new SolidBrush(Cel.InheritedStyle.ForeColor),
  125. new RectangleF((int)arrColumnLefts[iCount], (float)iTopMargin,
  126. (int)arrColumnWidths[iCount], (float)iCellHeight), strFormat);
  127.  
  128. }
  129. //Drawing Cells Borders
  130. e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)arrColumnLefts[iCount],
  131. iTopMargin, (int)arrColumnWidths[iCount], iCellHeight));
  132.  
  133. iCount++;
  134. }
  135. }
  136. iRow++;
  137. iTopMargin += iCellHeight;
  138. }
  139.  
  140. //If more lines exist, print another page.
  141. if (bMorePagesToPrint)
  142. e.HasMorePages = true;
  143. else
  144. e.HasMorePages = false;
  145. }
  146. catch (Exception exc)
  147. {
  148. MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement