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
Code c# (PrintDgv.cs) (373 lignes)
using System; using System.Collections; using System.Drawing; using System.Drawing.Printing; using System.Windows.Forms; using be.gaudry.model.enums; namespace be.gaudry.view.utils.dgvFactory { public class PrintDgv : AbstractDgv { #region declarations and constructors private StringFormat StrFormat; // Holds content of a TextBox Cell to write by DrawString private StringFormat StrFormatComboBox; // Holds content of a Boolean Cell to write by DrawImage private Button CellButton; // Holds the Contents of Button Cell private CheckBox CellCheckBox; // Holds the Contents of CheckBox Cell private ComboBox CellComboBox; // Holds the Contents of ComboBox Cell private int TotalWidth; // Summation of Columns widths private int RowPos; // Position of currently printing row private bool NewPage; // Indicates if a new page reached private int PageNo; // Number of pages to print private int CellHeight; // Height of DataGrid Cell private int RowsPerPage; // Number of Rows per Page private int HeaderHeight = 0; public PrintDgv() { //LayoutOptionsDialog.Text = "Mise en page pour l'impression"; //LayoutOptionsDialog.layoutControl.enableImageDocumentOptions(true); } #endregion #region method to override public override void print(DataGridView dgv) { this.dgv = dgv; //setLayoutOptions(outputTitle); printDoc.DefaultPageSettings.Landscape = (this.pageOrientation == PAGE_ORIENTATION.landscape); RowsPerPage = 0; PrintPreviewDialog ppvw; ppvw.Document = printDoc; ppvw.PrintPreviewControl.AutoZoom = true; // Showing the Print Preview Page if (ppvw.ShowDialog() != DialogResult.OK) { return; } // Printing the Document printDoc.Print(); } #endregion #region private methods private void PrintDoc_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e) { //try //{ // Formatting the Content of Text Cell to print StrFormat.Alignment = StringAlignment.Near; StrFormat.LineAlignment = StringAlignment.Center; StrFormat.Trimming = StringTrimming.EllipsisCharacter; // Formatting the Content of Combo Cells to print StrFormatComboBox.LineAlignment = StringAlignment.Center; StrFormatComboBox.FormatFlags = StringFormatFlags.NoWrap; StrFormatComboBox.Trimming = StringTrimming.EllipsisCharacter; ColumnLefts.Clear(); ColumnWidths.Clear(); ColumnTypes.Clear(); CellHeight = 0; RowsPerPage = 0; // For various column types // Calculating Total Widths TotalWidth = 0; foreach (DataGridViewColumn GridCol in SelectedColumns) { TotalWidth += GridCol.Width; } PageNo = 1; NewPage = true; RowPos = 0; /*} catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }*/ } private void PrintDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { int tmpWidth, i; int tmpTop = e.MarginBounds.Top; int tmpLeft = e.MarginBounds.Left; //try //{ // Before starting first page, it saves Width & Height of Headers and CoulmnType if (PageNo == 1) { foreach (DataGridViewColumn GridCol in SelectedColumns) { // Detemining whether the columns are fitted to page or not. if (FitToPageWidth) tmpWidth = (int)(Math.Floor((double)((double)GridCol.Width / (double)TotalWidth * (double)TotalWidth * ((double)e.MarginBounds.Width / (double)TotalWidth)))); else tmpWidth = GridCol.Width; HeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText, GridCol.InheritedStyle.Font, tmpWidth).Height) + 11; // Save width & height of headres and ColumnType ColumnLefts.Add(tmpLeft); ColumnWidths.Add(tmpWidth); ColumnTypes.Add(GridCol.GetType()); tmpLeft += tmpWidth; } } // Printing Current Page, Row by Row while (RowPos <= dgv.Rows.Count - 1) { DataGridViewRow GridRow = dgv.Rows[RowPos]; if (GridRow.IsNewRow || (!PrintAllRows && !GridRow.Selected)) { RowPos++; continue; } //height +10 avoids wrapped text taking too mutch place on the cell //seems not work CellHeight = GridRow.Height+10; if (tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top) { DrawFooter(e, RowsPerPage); NewPage = true; PageNo++; e.HasMorePages = true; return; } else { if (NewPage) { // Draw Header Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString( PrintTitle, e.MarginBounds.Width).Height - 13 ); if (DisplayDate) { String s = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString(); Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top - FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13); } // Draw Columns tmpTop = e.MarginBounds.Top; i = 0; foreach (DataGridViewColumn GridCol in SelectedColumns) { (int)ColumnWidths[i], HeaderHeight)); e.Graphics.DrawRectangle(Pens.Black, (int)ColumnWidths[i], HeaderHeight)); e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, (int)ColumnWidths[i], HeaderHeight), StrFormat); i++; } NewPage = false; tmpTop += HeaderHeight; } // Draw Columns Contents i = 0; foreach (DataGridViewCell Cel in GridRow.Cells) { if (!SelectedColumns.Contains(Cel.OwningColumn)) continue; // For the TextBox Column if (((Type) ColumnTypes[i]).Name == "DataGridViewTextBoxColumn" || ((Type) ColumnTypes[i]).Name == "DataGridViewLinkColumn") { string valueStr; if (Cel.Value == null) { valueStr = ""; } else { //todo : call format delegate mehod if provided { valueStr = (string)Cel.EditedFormattedValue; } else { valueStr = Cel.Value.ToString(); } } e.Graphics.DrawString(valueStr, Cel.InheritedStyle.Font, (int)ColumnWidths[i], (float)CellHeight), StrFormat); } // For the Button Column else if (((Type) ColumnTypes[i]).Name == "DataGridViewButtonColumn") { CellButton.Text = Cel.Value.ToString(); bmp.Width, bmp.Height)); } // For the CheckBox Column else if (((Type) ColumnTypes[i]).Name == "DataGridViewCheckBoxColumn") { Graphics tmpGraphics = Graphics.FromImage(bmp); bmp.Width, bmp.Height)); CellCheckBox.DrawToBitmap(bmp, (int)((bmp.Height - CellCheckBox.Height) / 2), CellCheckBox.Width, CellCheckBox.Height)); } // For the ComboBox Column else if (((Type) ColumnTypes[i]).Name == "DataGridViewComboBoxColumn") { bmp.Width, bmp.Height)); e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font, - 16, CellHeight), StrFormatComboBox); } // For the Image Column else if (((Type) ColumnTypes[i]).Name == "DataGridViewImageColumn") { tmpTop, (int)ColumnWidths[i], CellHeight); Size ImgSize = ((Image)(Cel.FormattedValue)).Size; e.Graphics.DrawImage((Image)Cel.FormattedValue, tmpTop + (int)((CelSize.Height - ImgSize.Height) / 2), ((Image)(Cel.FormattedValue)).Width, ((Image)(Cel.FormattedValue)).Height)); } // Drawing Cells Borders tmpTop, (int)ColumnWidths[i], CellHeight)); i++; } tmpTop += CellHeight; } RowPos++; // For the first page it calculates Rows per Page if (PageNo == 1) RowsPerPage++; } if (RowsPerPage == 0) return; // Write Footer (Page Number) DrawFooter(e, RowsPerPage); e.HasMorePages = false; /*} catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }*/ } private void DrawFooter(System.Drawing.Printing.PrintPageEventArgs e, int RowsPerPage) { double cnt = 0; // Detemining rows number to print if (PrintAllRows) { if (dgv.Rows[dgv.Rows.Count - 1].IsNewRow) cnt = dgv.Rows.Count - 2; // When the DataGridView doesn't allow adding rows else cnt = dgv.Rows.Count - 1; // When the DataGridView allows adding rows } else cnt = dgv.SelectedRows.Count; // Writing the Page Number on the Bottom of Page string footerPageCounter; switch (PageCounterFormat) { case PAGE_COUNTERFORMAT.pageXdeX: footerPageCounter = "Page {0} de {1}"; break; case PAGE_COUNTERFORMAT.xMinusX: footerPageCounter = "{0} - {1}"; break; case PAGE_COUNTERFORMAT.xSlashX: footerPageCounter = "{0} / {1}"; break; default: footerPageCounter = "Page {0} de {1}"; break; } string PageNum = string.Format( footerPageCounter, PageNo.ToString(), Math.Ceiling((double)(cnt / RowsPerPage)).ToString() ); e.Graphics.DrawString(PageNum, dgv.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(PageNum, dgv.Font, e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top + e.MarginBounds.Height + 31); } #endregion } }
Structure et Fichiers du projet
Afficher/masquer...Icône | Nom | Taille | Modification |
Pas de sous-répertoires. | |||
Icône | Nom | Taille | Modification |
| _ | Répertoire parent | 0 octets | 1732284788 22/11/2024 15:13:08 |
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.
Version en cache
22/11/2024 15:13:08 Cette version de la page est en cache (à la date du 22/11/2024 15:13:08) afin d'accélérer le traitement. Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la dernère version de la page.Document créé le 16/10/2009, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/cs-broldev-source-rf-view/utils/dgvFactory//PrintDgv.cs.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.