Thursday, February 1, 2018

Print Datagridview





çi dessous le code complet :

Imports System
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing

Public Class Form1

Private strFormat As StringFormat
Private arrColumnLefts As New ArrayList
Private arrColumnWidths As New ArrayList
Private iCellHeight As Integer = 0
Private iTotalWidth As Integer = 0
Private iRow As Integer = 0
Private bFirstPage As Boolean = False
Private bNewPage As Boolean = False
Private iHeaderHeight As Integer = 0

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.Rows.Add("0", "Iheb", "32", "IT Manager", "Tunisia")
DataGridView1.Rows.Add("1", "Sam", "32", "Teacher", "USA")
DataGridView1.Rows.Add("2", "Jhon", "22", "Student", "Philippine")
DataGridView1.Rows.Add("3", "David", "25", "Student", "UK")
End Sub

Private Sub printDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint

Try

strFormat = New StringFormat
strFormat.Alignment = StringAlignment.Near
strFormat.LineAlignment = StringAlignment.Center
strFormat.Trimming = StringTrimming.EllipsisCharacter

arrColumnLefts.Clear()
arrColumnWidths.Clear()
iCellHeight = 0
iRow = 0
bNewPage = True
bFirstPage = True

iTotalWidth = 0

For Each dgGridCol As DataGridViewColumn In DataGridView1.Columns

iTotalWidth += dgGridCol.

Next dgGridCol

Catch ex As Exception
MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

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 iTmpWidth As Integer = 0

If bFirstPage Then
For Each Gridcol As DataGridViewColumn In DataGridView1.Columns

iTmpWidth = CInt(Math.Truncate(Math.Floor(CDbl(CDbl(Gridcol.Width) / CDbl(iTotalWidth) * CDbl(iTotalWidth) * CDbl(e.MarginBounds.Width) / CDbl(iTotalWidth)))))
iHeaderHeight = CInt(e.Graphics.MeasureString(Gridcol.HeaderText, Gridcol.InheritedStyle.Font, iTmpWidth).Height) + 11


arrColumnLefts.Add(iLefmargin)
arrColumnWidths.Add(iTmpWidth)
iLefmargin += iTmpWidth

Next Gridcol
End If


Do While iRow <= DataGridView1.Rows.Count - 1
Dim GridRow As DataGridViewRow = DataGridView1.Rows(iRow)

iCellHeight = GridRow.Height + 5
Dim iCount As Integer = 0

If iTopmargin + iCellHeight >= 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 - e.Graphics.MeasureString("Datagridview Details", New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)

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("Datagridview Details", New Font(New Font(DataGridView1.Font, FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13)

'draw column

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), iHeaderHeight))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(DirectCast(arrColumnLefts(iCount), Integer), iTopmargin, DirectCast(arrColumnWidths(iCount), Integer), iHeaderHeight))
e.Graphics.DrawString(Gridcol.HeaderText, Gridcol.InheritedStyle.Font, New SolidBrush(Gridcol.InheritedStyle.ForeColor), New RectangleF(DirectCast(arrColumnLefts(iCount), Integer), iTopmargin, DirectCast(arrColumnWidths(iCount), Integer), iHeaderHeight), strFormat)

iCount += 1

Next Gridcol

bNewPage = False
iTopmargin += iHeaderHeight

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), iHeaderHeight), strFormat)

End If
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(DirectCast(arrColumnLefts(iCount), Integer), iTopmargin, DirectCast(arrColumnWidths(iCount), Integer), iCellHeight))

iCount += 1
Next cel
End If

iRow += 1
iTopmargin += iCellHeight
Loop

If bMorePagesToPrint Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If

Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim Print_Preview As New PrintPreviewDialog
Print_Preview.Document = PrintDocument1
Print_Preview.ShowDialog()

End Sub
End Class

No comments:

Post a Comment