PrintDgv.cs

Description du code

PrintDgv.cs est un fichier du projet BrolDev.
Ce fichier est situé dans /var/www/bin/sniplets/bibliobrol/broldev/src/.

Projet BrolDev : Librairie de composants réutilisables pour les applications BrolDev en CSharp.

Code source ou contenu du fichier

  1. using System;
  2. using System.Collections;
  3. using System.Drawing;
  4. using System.Drawing.Printing;
  5. using System.Windows.Forms;
  6. using be.gaudry.model.enums;
  7.  
  8. namespace be.gaudry.view.utils.dgvFactory
  9. {
  10. public class PrintDgv : AbstractDgv
  11. {
  12. #region declarations and constructors
  13. private PrintDocument printDoc = new PrintDocument(); // PrintDocumnet Object used for printing
  14.  
  15. private StringFormat StrFormat; // Holds content of a TextBox Cell to write by DrawString
  16. private StringFormat StrFormatComboBox; // Holds content of a Boolean Cell to write by DrawImage
  17. private Button CellButton; // Holds the Contents of Button Cell
  18. private CheckBox CellCheckBox; // Holds the Contents of CheckBox Cell
  19. private ComboBox CellComboBox; // Holds the Contents of ComboBox Cell
  20.  
  21. private int TotalWidth; // Summation of Columns widths
  22. private int RowPos; // Position of currently printing row
  23. private bool NewPage; // Indicates if a new page reached
  24. private int PageNo; // Number of pages to print
  25. private ArrayList ColumnLefts = new ArrayList(); // Left Coordinate of Columns
  26. private ArrayList ColumnWidths = new ArrayList(); // Width of Columns
  27. private ArrayList ColumnTypes = new ArrayList(); // DataType of Columns
  28. private int CellHeight; // Height of DataGrid Cell
  29. private int RowsPerPage; // Number of Rows per Page
  30. private int HeaderHeight = 0;
  31.  
  32.  
  33. public PrintDgv()
  34. {
  35. //LayoutOptionsDialog.Text = "Mise en page pour l'impression";
  36. //LayoutOptionsDialog.layoutControl.enableImageDocumentOptions(true);
  37. }
  38. #endregion
  39.  
  40. #region method to override
  41. public override void print(DataGridView dgv)
  42. {
  43. this.dgv = dgv;
  44. //setLayoutOptions(outputTitle);
  45. printDoc.DefaultPageSettings.Landscape = (this.pageOrientation == PAGE_ORIENTATION.landscape);
  46.  
  47. RowsPerPage = 0;
  48. PrintPreviewDialog ppvw;
  49. ppvw = new PrintPreviewDialog();
  50. ppvw.Document = printDoc;
  51. ppvw.PrintPreviewControl.AutoZoom = true;
  52.  
  53. // Showing the Print Preview Page
  54. printDoc.BeginPrint += new PrintEventHandler(PrintDoc_BeginPrint);
  55. printDoc.PrintPage += new PrintPageEventHandler(PrintDoc_PrintPage);
  56.  
  57.  
  58. if (ppvw.ShowDialog() != DialogResult.OK)
  59. {
  60. printDoc.BeginPrint -= new PrintEventHandler(PrintDoc_BeginPrint);
  61. printDoc.PrintPage -= new PrintPageEventHandler(PrintDoc_PrintPage);
  62. return;
  63. }
  64.  
  65. // Printing the Document
  66.  
  67. printDoc.Print();
  68. printDoc.BeginPrint -= new PrintEventHandler(PrintDoc_BeginPrint);
  69. printDoc.PrintPage -= new PrintPageEventHandler(PrintDoc_PrintPage);
  70. }
  71. #endregion
  72.  
  73. #region private methods
  74.  
  75. private void PrintDoc_BeginPrint(object sender,
  76. System.Drawing.Printing.PrintEventArgs e)
  77. {
  78. //try
  79. //{
  80. // Formatting the Content of Text Cell to print
  81. StrFormat = new StringFormat();
  82. StrFormat.Alignment = StringAlignment.Near;
  83. StrFormat.LineAlignment = StringAlignment.Center;
  84. StrFormat.Trimming = StringTrimming.EllipsisCharacter;
  85.  
  86. // Formatting the Content of Combo Cells to print
  87. StrFormatComboBox = new StringFormat();
  88. StrFormatComboBox.LineAlignment = StringAlignment.Center;
  89. StrFormatComboBox.FormatFlags = StringFormatFlags.NoWrap;
  90. StrFormatComboBox.Trimming = StringTrimming.EllipsisCharacter;
  91.  
  92. ColumnLefts.Clear();
  93. ColumnWidths.Clear();
  94. ColumnTypes.Clear();
  95. CellHeight = 0;
  96. RowsPerPage = 0;
  97.  
  98. // For various column types
  99. CellButton = new Button();
  100. CellCheckBox = new CheckBox();
  101. CellComboBox = new ComboBox();
  102.  
  103. // Calculating Total Widths
  104. TotalWidth = 0;
  105. foreach (DataGridViewColumn GridCol in SelectedColumns)
  106. {
  107. TotalWidth += GridCol.Width;
  108. }
  109. PageNo = 1;
  110. NewPage = true;
  111. RowPos = 0;
  112. /*}
  113. catch (Exception ex)
  114. {
  115.   MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  116. }*/
  117. }
  118.  
  119. private void PrintDoc_PrintPage(object sender,
  120. System.Drawing.Printing.PrintPageEventArgs e)
  121. {
  122. int tmpWidth, i;
  123. int tmpTop = e.MarginBounds.Top;
  124. int tmpLeft = e.MarginBounds.Left;
  125.  
  126. //try
  127. //{
  128. // Before starting first page, it saves Width & Height of Headers and CoulmnType
  129. if (PageNo == 1)
  130. {
  131. foreach (DataGridViewColumn GridCol in SelectedColumns)
  132. {
  133. // Detemining whether the columns are fitted to page or not.
  134. if (FitToPageWidth)
  135. tmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
  136. (double)TotalWidth * (double)TotalWidth *
  137. ((double)e.MarginBounds.Width / (double)TotalWidth))));
  138. else
  139. tmpWidth = GridCol.Width;
  140.  
  141. HeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
  142. GridCol.InheritedStyle.Font, tmpWidth).Height) + 11;
  143.  
  144. // Save width & height of headres and ColumnType
  145. ColumnLefts.Add(tmpLeft);
  146. ColumnWidths.Add(tmpWidth);
  147. ColumnTypes.Add(GridCol.GetType());
  148. tmpLeft += tmpWidth;
  149. }
  150. }
  151.  
  152. // Printing Current Page, Row by Row
  153. while (RowPos <= dgv.Rows.Count - 1)
  154. {
  155. DataGridViewRow GridRow = dgv.Rows[RowPos];
  156. if (GridRow.IsNewRow || (!PrintAllRows && !GridRow.Selected))
  157. {
  158. RowPos++;
  159. continue;
  160. }
  161.  
  162. //height +10 avoids wrapped text taking too mutch place on the cell
  163. //seems not work
  164. CellHeight = GridRow.Height+10;
  165.  
  166. if (tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
  167. {
  168. DrawFooter(e, RowsPerPage);
  169. NewPage = true;
  170. PageNo++;
  171. e.HasMorePages = true;
  172. return;
  173. }
  174. else
  175. {
  176. if (NewPage)
  177. {
  178. // Draw Header
  179. e.Graphics.DrawString(PrintTitle, new Font(dgv.Font, FontStyle.Bold),
  180. Brushes.Black,
  181. e.MarginBounds.Left,
  182. e.MarginBounds.Top - e.Graphics.MeasureString(
  183. PrintTitle,
  184. new Font(dgv.Font, FontStyle.Bold),
  185. e.MarginBounds.Width).Height - 13
  186. );
  187. if (DisplayDate)
  188. {
  189. String s = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();
  190.  
  191. e.Graphics.DrawString(s, new Font(dgv.Font, FontStyle.Bold),
  192. Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width -
  193. e.Graphics.MeasureString(s, new Font(dgv.Font,
  194. FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top -
  195. e.Graphics.MeasureString(PrintTitle, new Font(new Font(dgv.Font,
  196. FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13);
  197. }
  198.  
  199. // Draw Columns
  200. tmpTop = e.MarginBounds.Top;
  201. i = 0;
  202. foreach (DataGridViewColumn GridCol in SelectedColumns)
  203. {
  204. e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
  205. new Rectangle((int) ColumnLefts[i], tmpTop,
  206. (int)ColumnWidths[i], HeaderHeight));
  207.  
  208. e.Graphics.DrawRectangle(Pens.Black,
  209. new Rectangle((int) ColumnLefts[i], tmpTop,
  210. (int)ColumnWidths[i], HeaderHeight));
  211.  
  212. e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,
  213. new SolidBrush(GridCol.InheritedStyle.ForeColor),
  214. new RectangleF((int)ColumnLefts[i], tmpTop,
  215. (int)ColumnWidths[i], HeaderHeight), StrFormat);
  216. i++;
  217. }
  218. NewPage = false;
  219. tmpTop += HeaderHeight;
  220. }
  221.  
  222. // Draw Columns Contents
  223. i = 0;
  224. foreach (DataGridViewCell Cel in GridRow.Cells)
  225. {
  226. if (!SelectedColumns.Contains(Cel.OwningColumn))
  227. continue;
  228.  
  229. // For the TextBox Column
  230. if (((Type) ColumnTypes[i]).Name == "DataGridViewTextBoxColumn" ||
  231. ((Type) ColumnTypes[i]).Name == "DataGridViewLinkColumn")
  232. {
  233. string valueStr;
  234. if (Cel.Value == null)
  235. {
  236. valueStr = "";
  237. }
  238. else
  239. {
  240. //todo : call format delegate mehod if provided
  241. if (Cel.EditedFormattedValue is string)
  242. {
  243. valueStr = (string)Cel.EditedFormattedValue;
  244. }
  245. else
  246. {
  247. valueStr = Cel.Value.ToString();
  248. }
  249. }
  250. e.Graphics.DrawString(valueStr, Cel.InheritedStyle.Font,
  251. new SolidBrush(Cel.InheritedStyle.ForeColor),
  252. new RectangleF((int)ColumnLefts[i], (float)tmpTop,
  253. (int)ColumnWidths[i], (float)CellHeight), StrFormat);
  254. }
  255. // For the Button Column
  256. else if (((Type) ColumnTypes[i]).Name == "DataGridViewButtonColumn")
  257. {
  258. CellButton.Text = Cel.Value.ToString();
  259. CellButton.Size = new Size((int)ColumnWidths[i], CellHeight);
  260. Bitmap bmp =new Bitmap(CellButton.Width, CellButton.Height);
  261. CellButton.DrawToBitmap(bmp, new Rectangle(0, 0,
  262. bmp.Width, bmp.Height));
  263. e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
  264. }
  265. // For the CheckBox Column
  266. else if (((Type) ColumnTypes[i]).Name == "DataGridViewCheckBoxColumn")
  267. {
  268. CellCheckBox.Size = new Size(14, 14);
  269. CellCheckBox.Checked = (bool)Cel.Value;
  270. Bitmap bmp = new Bitmap((int)ColumnWidths[i], CellHeight);
  271. Graphics tmpGraphics = Graphics.FromImage(bmp);
  272. tmpGraphics.FillRectangle(Brushes.White, new Rectangle(0, 0,
  273. bmp.Width, bmp.Height));
  274. CellCheckBox.DrawToBitmap(bmp,
  275. new Rectangle((int)((bmp.Width - CellCheckBox.Width) / 2),
  276. (int)((bmp.Height - CellCheckBox.Height) / 2),
  277. CellCheckBox.Width, CellCheckBox.Height));
  278. e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
  279. }
  280. // For the ComboBox Column
  281. else if (((Type) ColumnTypes[i]).Name == "DataGridViewComboBoxColumn")
  282. {
  283. CellComboBox.Size = new Size((int)ColumnWidths[i], CellHeight);
  284. Bitmap bmp = new Bitmap(CellComboBox.Width, CellComboBox.Height);
  285. CellComboBox.DrawToBitmap(bmp, new Rectangle(0, 0,
  286. bmp.Width, bmp.Height));
  287. e.Graphics.DrawImage(bmp, new Point((int)ColumnLefts[i], tmpTop));
  288. e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
  289. new SolidBrush(Cel.InheritedStyle.ForeColor),
  290. new RectangleF((int)ColumnLefts[i] + 1, tmpTop, (int)ColumnWidths[i]
  291. - 16, CellHeight), StrFormatComboBox);
  292. }
  293. // For the Image Column
  294. else if (((Type) ColumnTypes[i]).Name == "DataGridViewImageColumn")
  295. {
  296. Rectangle CelSize = new Rectangle((int)ColumnLefts[i],
  297. tmpTop, (int)ColumnWidths[i], CellHeight);
  298. Size ImgSize = ((Image)(Cel.FormattedValue)).Size;
  299. e.Graphics.DrawImage((Image)Cel.FormattedValue,
  300. new Rectangle((int)ColumnLefts[i] + (int)((CelSize.Width - ImgSize.Width) / 2),
  301. tmpTop + (int)((CelSize.Height - ImgSize.Height) / 2),
  302. ((Image)(Cel.FormattedValue)).Width, ((Image)(Cel.FormattedValue)).Height));
  303.  
  304. }
  305.  
  306. // Drawing Cells Borders
  307. e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)ColumnLefts[i],
  308. tmpTop, (int)ColumnWidths[i], CellHeight));
  309.  
  310. i++;
  311.  
  312. }
  313. tmpTop += CellHeight;
  314. }
  315.  
  316. RowPos++;
  317. // For the first page it calculates Rows per Page
  318. if (PageNo == 1) RowsPerPage++;
  319. }
  320.  
  321. if (RowsPerPage == 0) return;
  322.  
  323. // Write Footer (Page Number)
  324. DrawFooter(e, RowsPerPage);
  325.  
  326. e.HasMorePages = false;
  327. /*}
  328. catch (Exception ex)
  329. {
  330.   MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  331. }*/
  332. }
  333.  
  334. private void DrawFooter(System.Drawing.Printing.PrintPageEventArgs e,
  335. int RowsPerPage)
  336. {
  337. double cnt = 0;
  338.  
  339. // Detemining rows number to print
  340. if (PrintAllRows)
  341. {
  342. if (dgv.Rows[dgv.Rows.Count - 1].IsNewRow)
  343. cnt = dgv.Rows.Count - 2; // When the DataGridView doesn't allow adding rows
  344. else
  345. cnt = dgv.Rows.Count - 1; // When the DataGridView allows adding rows
  346. }
  347. else
  348. cnt = dgv.SelectedRows.Count;
  349.  
  350. // Writing the Page Number on the Bottom of Page
  351. string footerPageCounter;
  352. switch (PageCounterFormat)
  353. {
  354. case PAGE_COUNTERFORMAT.pageXdeX: footerPageCounter = "Page {0} de {1}"; break;
  355. case PAGE_COUNTERFORMAT.xMinusX: footerPageCounter = "{0} - {1}"; break;
  356. case PAGE_COUNTERFORMAT.xSlashX: footerPageCounter = "{0} / {1}"; break;
  357. default: footerPageCounter = "Page {0} de {1}"; break;
  358. }
  359. string PageNum = string.Format(
  360. footerPageCounter,
  361. PageNo.ToString(),
  362. Math.Ceiling((double)(cnt / RowsPerPage)).ToString()
  363. );
  364.  
  365. e.Graphics.DrawString(PageNum, dgv.Font, Brushes.Black,
  366. e.MarginBounds.Left + (e.MarginBounds.Width -
  367. e.Graphics.MeasureString(PageNum, dgv.Font,
  368. e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top +
  369. e.MarginBounds.Height + 31);
  370. }
  371. #endregion
  372. }
  373. }

Structure et Fichiers du projet

Afficher/masquer...


Répertoires contenus dans /var/www/bin/sniplets/bibliobrol/broldev/src/view/utils/dgvFactory/ 
IcôneNomTailleModification
Pas de sous-répertoires.
IcôneNomTailleModification
| _ Répertoire parent0 octets1732285584 22/11/2024 15:26:24
Fichiers contenus dans /var/www/bin/sniplets/bibliobrol/broldev/src/view/utils/dgvFactory/ 
IcôneNomTailleModificationAction
IcôneNomTailleModificationAction
Afficher le fichier .cs|.csHtmlDgv.cs5.03 Ko31/10/2018 18:33:22-refusé-
Afficher le fichier .cs|.csExcelDgv.cs18.77 Ko31/10/2018 18:33:22-refusé-
Afficher le fichier .cs|.csXMLDgv.cs3.02 Ko31/10/2018 18:33:23-refusé-
Afficher le fichier .cs|.csCSVDgv.cs5.02 Ko31/10/2018 18:33:22-refusé-
Afficher le fichier .cs|.csPrintDgv.cs18.02 Ko31/10/2018 18:33:23-refusé-
Afficher le fichier .cs|.csIDgv.cs271 octets31/10/2018 18:33:23-refusé-
Afficher le fichier .cs|.csTextDgv.cs4.02 Ko31/10/2018 18:33:23-refusé-
Afficher le fichier .cs|.csAbstractDgv.cs3.46 Ko31/10/2018 18:33:22-refusé-

Utilisation de l'explorateur de code

  • Navigation :
    • Un clic sur une icône de répertoire ouvre ce répertoire pour en afficher les fichiers.
    • Lorsque le répertoire en cours ne contient pas de sous-répertoires il est possible de remonter vers le répertoire parent.
    • La structure de répertoires en treetable (tableau en forme d'arborescence) n'est plus possibledans cette version.
    • Un clic sur une icône de fichier ouvre ce fichier pour en afficher le code avec la coloration syntaxique adaptée en fonction du langage principal utilisé dans le fichier.
  • Affichage :
    • Il est possible de trier les répertoires ou les fichiers selon certains critères (nom, taille, date).
  • Actions :
    • Les actions possible sur les fichiers dépendent de vos droits d'utilisateur sur le site. Veuillez activer le mode utilisateur pour activer les actions.

Nederlandse vertaling

U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.

Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.

Bij voorbaat dank.

Document heeft de 16/10/2009 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/cs-broldev-source-rf-view/utils/dgvFactory/PrintDgv.cs.html

De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.