Torna al Thread
Private Sub Print_Document_GRIDVIEW(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Try
Static RigheStampate As Integer
Static PagineStampate As Integer
Static MiePagine As Integer
Dim RigheTotali As Integer = DataGridView_auto_report.Rows.Count
Dim ControllaPagine As Boolean = True
Dim Rect As Rectangle
'Dim RectQuality As Rectangle
Dim ColCorrente As Integer = 0
Dim Top As Integer = 5 ' e.MarginBounds.Top
Dim Left As Integer = 5 ' e.MarginBounds.Left
Dim Height As Integer = DataGridView_auto_report.RowTemplate.Height
Dim MaxRighe As Integer = e.MarginBounds.Height / Height
Dim MaxPagine As Integer = DataGridView_auto_report.Rows.Count / MaxRighe
Dim MPX As Integer = (DataGridView_auto_report.Rows.Count \ MaxRighe) + 1
Dim BrHeader As SolidBrush = New SolidBrush(Color.Gray)
Dim carattere_6R As New System.Drawing.Font("Tahoma", 6, FontStyle.Regular)
Dim Hfont As Font = carattere_6R
Dim Fc As String
Dim Fr As String
For Each col As DataGridViewColumn In DataGridView_auto_report.Columns
Fc = ""
Dim ColText As String = col.HeaderText
Dim Sz As System.Drawing.SizeF = e.Graphics.MeasureString(ColText, Hfont)
While Sz.Width > col.Width
ColText = ColText.Remove(ColText.Length - 1, 1)
Sz = e.Graphics.MeasureString(ColText + "...", Hfont)
Fc = "..."
End While
Rect = New Rectangle(Left, Top, col.Width, Height)
e.Graphics.FillRectangle(BrHeader, Rect)
e.Graphics.DrawString(ColText + Fc, Hfont, Brushes.Black, Left, Top + (e.Graphics.MeasureString(ColText, Hfont).Height / 2))
Left += col.Width
If Left > e.PageBounds.Right - 5 Then
MsgBox("Attenzione i campi da stampare sono troppi e la stampa non sarà effettuata correttamente" + Chr(13) + Chr(13) + "Possibili Soluzioni:" + Chr(13) + " .Impostare la stampa in modo Orizzontale" + Chr(13) + " .Selezionare un foglio di dimensioni maggiori" + Chr(13) + " .Ridurre il numero di campi da stampare", MsgBoxStyle.Information)
Exit For
End If
Next
Dim BrRow As SolidBrush = New SolidBrush(Color.Transparent)
Dim PnRow As Pen = New Pen(Color.Black)
Dim RigaCorrente As Integer = 0
For RigaCorrente = RigheStampate To RigheStampate + MaxRighe
Left = 5 ' e.MarginBounds.Left
Top += Height
For Each col As DataGridViewColumn In DataGridView_auto_report.Columns
Fr = ""
Dim CelText As String = DataGridView_auto_report.Rows(RigaCorrente).Cells(col.Name).Value.ToString
Dim Sz As System.Drawing.SizeF = e.Graphics.MeasureString(CelText, Hfont)
While Sz.Width > col.Width
CelText = CelText.Remove(CelText.Length - 1, 1)
Sz = e.Graphics.MeasureString(CelText + "...", Hfont)
Fr = "..."
End While
Rect = New Rectangle(Left, Top, col.Width, Height)
e.Graphics.FillRectangle(BrRow, Rect)
e.Graphics.DrawRectangle(PnRow, Rect)
e.Graphics.DrawString(CelText + Fr, Hfont, Brushes.Black, Left, Top + (e.Graphics.MeasureString(CelText, Hfont).Height / 2))
Left += col.Width
Next
RigheStampate += 1
MiePagine = PagineStampate + 1
If RigheStampate >= DataGridView_auto_report.Rows.Count Then
ControllaPagine = False
e.HasMorePages = False
RigheStampate = 0
PagineStampate = 0
Exit For
End If
Next RigaCorrente
If ControllaPagine Then
PagineStampate += 1
If PagineStampate < MaxPagine Then
e.HasMorePages = True
Else
e.HasMorePages = False
RigheStampate = 0
PagineStampate = 0
End If
End If
BrHeader.Dispose()
BrRow.Dispose()
PnRow.Dispose()
Catch ex As Exception
MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub