1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
'NB:pour l'exemple j'ai pris les donnees du tableau
'des cultures/pays:CultureInfo.GetCultures pour mettre des donnees
'sur le datagridview "Cobaye"
Imports System.Threading
Imports System.Globalization
Imports System.Windows.Forms
Public Class Form1
Dim myForm As Form2 = New Form2
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
btnPrintDGV.Enabled = False
End Sub
Private Sub btnFillDGV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFillDGV.Click
SetupDataGridView()
btnPrintDGV.Enabled = True
End Sub
Private Sub btnPrintDGV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintDGV.Click
'1/uncomment pour envoyer directement à l'imprimante à l'imprimante
Me.PrintDocument1.Print()
'
'
'
'2/uncomment pour afficher sur le control PrintPreviewDialog de ce Form
'(apercu)
'Me.PrintPreviewDialog1.Document = PrintDocument1
'Me.PrintPreviewDialog1.Show()
'
'
'3/uncomment pour afficher sur un controle PrintPreviewDialog
'place sur le Form2
'Me.myForm.PrintPreviewControl1.AutoZoom = True
'Me.myForm.PrintPreviewControl1.Document = PrintDocument1
'Me.myForm.Show()
End Sub
'Fill du DataGridView
Private Sub SetupDataGridView()
'AJOUT DES VALEURS POMPEES DE CultureInfo(gratis) à MyListCI
'Clear the DataGridViewLanguages control
DataGridView1.DataSource = Nothing
'Presentation Colonnes Lignes:En-tete non visible
Me.DataGridView1.RowHeadersVisible = False
' Disable editing features.
DataGridView1.AllowUserToAddRows = False
DataGridView1.AllowUserToDeleteRows = False
DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically
'Presentation Colonnes
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.WhiteSmoke
DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.Blue
DataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True
'Auto-generation des colonnes moins colonnes NumberFormat & DateTimeFormat
'avec Remove on peut exclure certaines colonnes de la generation
'ici:ces 2 colonnes generes une exception en realtion avec culture -ar-
'c'est une autre facon de personnaliser au passage moins souple.
With Me.DataGridView1
.AutoGenerateColumns = True
.DataSource = CultureInfo.GetCultures(CultureTypes.AllCultures)
.Columns.Remove("NumberFormat")
.Columns.Remove("DateTimeFormat")
End With
Me.btnPrintDGV.Enabled = True
End Sub
'variables pour impression
'ne t'occupe des Combox,CheckBoxes et Images
'tu peux sans essayer sans eux.
'Simplement l'auteur du code qui n'est pas moi a prevu de gerer meme ces cas......
Private oStringFormat As StringFormat
Private oStringFormatComboBox As StringFormat
Private oButton As Button
Private oCheckbox As CheckBox
Private oComboBox As ComboBox
Private nTotalWidth As Int16
Private nRowPos As Int16
Private NewPage As Boolean
Private nPageNo As Int16
Private Header As String = "Header Test"
Private sUserName As String = "Will"
Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
oStringFormat = New StringFormat
oStringFormat.Alignment = StringAlignment.Near
oStringFormat.LineAlignment = StringAlignment.Center
oStringFormat.Trimming = StringTrimming.EllipsisCharacter
oStringFormatComboBox = New StringFormat
oStringFormatComboBox.LineAlignment = StringAlignment.Center
oStringFormatComboBox.FormatFlags = StringFormatFlags.NoWrap
oStringFormatComboBox.Trimming = StringTrimming.EllipsisCharacter
oButton = New Button
oCheckbox = New CheckBox
oComboBox = New ComboBox
nTotalWidth = 0
For Each oColumn As DataGridViewColumn In DataGridView1.Columns
nTotalWidth += oColumn.Width
Next
nPageNo = 1
NewPage = True
nRowPos = 0
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Static oColumnLefts As New ArrayList
Static oColumnWidths As New ArrayList
Static oColumnTypes As New ArrayList
Static nHeight As Int16
Dim nWidth, i, nRowsPerPage As Int16
Dim nTop As Int16 = e.MarginBounds.Top
Dim nLeft As Int16 = e.MarginBounds.Left
If nPageNo = 1 Then
For Each oColumn As DataGridViewColumn In DataGridView1.Columns
nWidth = CType(Math.Floor(oColumn.Width / nTotalWidth * nTotalWidth * (e.MarginBounds.Width / nTotalWidth)), Int16)
nHeight = e.Graphics.MeasureString(oColumn.HeaderText, oColumn.InheritedStyle.Font, nWidth).Height + 11
oColumnLefts.Add(nLeft)
oColumnWidths.Add(nWidth)
oColumnTypes.Add(oColumn.GetType)
nLeft += nWidth
Next
End If
Do While nRowPos < DataGridView1.Rows.Count - 1
Dim oRow As DataGridViewRow = DataGridView1.Rows(nRowPos)
If nTop + nHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
DrawFooter(e, nRowsPerPage)
NewPage = True
nPageNo += 1
e.HasMorePages = True
Exit Sub
Else
If NewPage Then
' Draw Header
e.Graphics.DrawString(Header, New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString(Header, New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)
' Draw Columns
nTop = e.MarginBounds.Top
i = 0
For Each oColumn As DataGridViewColumn In DataGridView1.Columns
e.Graphics.FillRectangle(New SolidBrush(Drawing.Color.LightGray), New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
e.Graphics.DrawString(oColumn.HeaderText, oColumn.InheritedStyle.Font, New SolidBrush(oColumn.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)
i += 1
Next
NewPage = False
End If
nTop += nHeight
i = 0
For Each oCell As DataGridViewCell In oRow.Cells
If oColumnTypes(i) Is GetType(DataGridViewTextBoxColumn) OrElse oColumnTypes(i) Is GetType(DataGridViewLinkColumn) Then
e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)
ElseIf oColumnTypes(i) Is GetType(DataGridViewButtonColumn) Then
oButton.Text = oCell.Value.ToString
oButton.Size = New Size(oColumnWidths(i), nHeight)
Dim oBitmap As New Bitmap(oButton.Width, oButton.Height)
oButton.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
ElseIf oColumnTypes(i) Is GetType(DataGridViewCheckBoxColumn) Then
oCheckbox.Size = New Size(14, 14)
oCheckbox.Checked = CType(oCell.Value, Boolean)
Dim oBitmap As New Bitmap(oColumnWidths(i), nHeight)
Dim oTempGraphics As Graphics = Graphics.FromImage(oBitmap)
oTempGraphics.FillRectangle(Brushes.White, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
oCheckbox.DrawToBitmap(oBitmap, New Rectangle(CType((oBitmap.Width - oCheckbox.Width) / 2, Int32), CType((oBitmap.Height - oCheckbox.Height) / 2, Int32), oCheckbox.Width, oCheckbox.Height))
e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
ElseIf oColumnTypes(i) Is GetType(DataGridViewComboBoxColumn) Then
oComboBox.Size = New Size(oColumnWidths(i), nHeight)
Dim oBitmap As New Bitmap(oComboBox.Width, oComboBox.Height)
oComboBox.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i) + 1, nTop, oColumnWidths(i) - 16, nHeight), oStringFormatComboBox)
ElseIf oColumnTypes(i) Is GetType(DataGridViewImageColumn) Then
Dim oCellSize As Rectangle = New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight)
Dim oImageSize As Size = CType(oCell.Value, Image).Size
e.Graphics.DrawImage(oCell.Value, New Rectangle(oColumnLefts(i) + CType(((oCellSize.Width - oImageSize.Width) / 2), Int32), nTop + CType(((oCellSize.Height - oImageSize.Height) / 2), Int32), CType(oCell.Value, Image).Width, CType(oCell.Value, Image).Height))
End If
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
i += 1
Next
End If
nRowPos += 1
nRowsPerPage += 1
Loop
DrawFooter(e, nRowsPerPage)
e.HasMorePages = False
End Sub
Private Sub DrawFooter(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal RowsPerPage As Int32)
Dim sPageNo As String = nPageNo.ToString + " of " + Math.Ceiling(DataGridView1.Rows.Count / RowsPerPage).ToString
' Right Align - User Name
e.Graphics.DrawString(sUserName, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, DataGridView1.Font, e.MarginBounds.Width).Width), e.MarginBounds.Top + e.MarginBounds.Height + 7)
' Left Align - Date/Time
e.Graphics.DrawString(Now.ToLongDateString + " " + Now.ToShortTimeString, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top + e.MarginBounds.Height + 7)
' Center - Page No. Info
e.Graphics.DrawString(sPageNo, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, DataGridView1.Font, e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top + e.MarginBounds.Height + 31)
End Sub
End Class
Public Class Form2
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
End Sub
End Class |
Partager