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
| Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Try
Dim iLefMargin As Integer = e.MarginBounds.Left
Dim itopMargin As Integer = e.MarginBounds.Top
Dim bMorePagesToPrint As Boolean = False
Dim iTempwidth As Integer = 0
If bFirstPage Then
For Each GridCol As DataGridViewColumn In DataGridView1.Columns
iTempwidth = CInt(Math.Truncate(Math.Floor(CDbl(CDbl(GridCol.Width) * CDbl(iTotalWidth) * CDbl(e.MarginBounds.Width) / CDbl(iTotalWidth)))))
iHeaderMeight = CInt(e.Graphics.MeasureString(GridCol.HeaderText, GridCol.InheritedStyle.Font, iTempwidth).Height) + 11
arrColumnLefts.Add(iLefMargin)
arrColumnWidths.Add(iTempwidth)
iLefMargin = +iTempwidth
Next GridCol
End If
Do While iRow <= DataGridView1.Rows.Count - 1
Dim GridRow As DataGridViewRow = DataGridView1.Rows(iRow)
iCellMeight = GridRow.Height + 5
Dim iCount As Integer = 0
If itopMargin + iCellMeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
bNewPage = True
bFirstPage = False
bMorePagesToPrint = True
Exit Do
Else
If bNewPage Then
e.Graphics.DrawString("datagridview details ", New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top)
Dim strDate As String = Date.Now.ToLongDateString & " " & Date.Now.ToShortTimeString
e.Graphics.DrawString(strDate, New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(strDate, New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top - e.Graphics.MeasureString("datagridviewdetails", New Font(New Font(DataGridView1.Font, FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13)
' ''''' tracer les colonnes
itopMargin = e.MarginBounds.Top
For Each GridCol As DataGridViewColumn In DataGridView1.Columns
e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New Rectangle(DirectCast(arrColumnLefts(iCount), Integer), itopMargin, DirectCast(arrColumnWidths(iCount), Integer), iHeaderMeight))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(DirectCast(arrColumnLefts(iCount), Integer), itopMargin, DirectCast(arrColumnWidths(iCount), Integer), iHeaderMeight))
e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, New SolidBrush(GridCol.InheritedStyle.ForeColor), New RectangleF(DirectCast(arrColumnLefts(iCount), Integer), itopMargin, DirectCast(arrColumnWidths(iCount), Integer), iHeaderMeight), strFormat)
iCount += 1
Next GridCol
bNewPage = False
itopMargin += iHeaderMeight
End If
iCount = 0
For Each cel As DataGridViewCell In GridRow.Cells
If cel.Value IsNot Nothing Then
e.Graphics.DrawString(cel.Value.ToString, cel.InheritedStyle.Font, New SolidBrush(cel.InheritedStyle.ForeColor), New RectangleF(DirectCast(arrColumnLefts(iCount), Integer), CSng(itopMargin), DirectCast(arrColumnWidths(iCount), Integer), iHeaderMeight), strFormat)
End If
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(DirectCast(arrColumnLefts(iCount), Integer), itopMargin, DirectCast(arrColumnWidths(iCount), Integer), iCellMeight))
iCount += 1
Next cel
End If
iRow += 1
itopMargin += iCellMeight
Loop
If bMorePagesToPrint Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try |
Partager