Imports System.Drawing.Printing
Public Class frmGridPrint
Dim ds As New DataSet
Dim dtMeld As New DataTable
Dim dtMass As New DataTable
Dim WithEvents pd As New PrintDocument
Dim printlines As New List(Of String)
Dim pdia As New PrintDialog
Private Sub frmGridPrint_Load(sender As System.Object, _
e As System.EventArgs) Handles MyBase.Load
With dtMeld
.Columns.Add("ID", GetType(Integer))
.Columns.Add("Meld", GetType(String))
.PrimaryKey = {.Columns(0)}
For i As Integer = 0 To 20
.Rows.Add(i, "Meld" & CStr(i))
Next i
End With
With dtMass
.Columns.Add("ID", GetType(Integer))
.Columns.Add("MeldID", GetType(Integer))
.Columns.Add("Mass", GetType(String))
.PrimaryKey = {.Columns(0)}
Dim id As Integer, mz As Integer
Dim rndm As New Random(123)
For i As Integer = 0 To dtMeld.Rows.Count - 1
mz = rndm.Next(0, 10)
For k As Integer = 1 To mz
id += 1
.Rows.Add(id, i, "Meld" & CStr(i) & "-Mass" & CStr(k))
Next k
Next i
End With
ds.Tables.Add(dtMeld) : ds.Tables.Add(dtMass)
Dim dr As New DataRelation _
("MeldMass", dtMeld.Columns("ID"), dtMass.Columns("MeldID"), True)
ds.Relations.Add(dr)
Dim cr() As DataRow
With dtMeld
For i As Integer = 0 To .Rows.Count - 1
'printlines.Add(" ")
printlines.Add(.Rows(i).Item("Meld").ToString)
cr = .Rows(i).GetChildRows("MeldMass")
For k As Integer = 0 To cr.Length - 1
printlines.Add(" --> " & cr(k).Item("Mass").ToString)
Next k
Next i
End With
pdia.ShowDialog()
pd.Print()
End Sub
Private Sub pd_PrintPage(sender As Object, _
e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
Static linecounter As Integer = -1
Static pagecounter As Integer = 0
Static lineheight As Integer
pagecounter += 1
Dim linefont As New Font("Arial", 10)
Dim pageheight As Integer = e.MarginBounds.Height
Dim pagewidth As Integer = e.MarginBounds.Width
Dim br As New SolidBrush(Color.Black)
Dim pageend As Boolean = False
Dim posy As Integer = e.MarginBounds.Top
Dim sz As SizeF
Do
linecounter += 1
If linecounter = 0 Then
sz = _
e.Graphics.MeasureString(printlines(linecounter), linefont, _
pagewidth)
lineheight = CInt(Math.Ceiling(sz.Height))
End If
posy += lineheight
If posy + lineheight <= e.MarginBounds.Height Then
sz = _
e.Graphics.MeasureString(printlines(linecounter), linefont, _
pagewidth)
lineheight = CInt(Math.Ceiling(sz.Height))
Dim lr As New Rectangle(e.MarginBounds.Left, posy, CInt( _
sz.Width), lineheight)
e.Graphics.DrawString _
(printlines(linecounter), linefont, br, lr)
Else
linecounter -= 1
pageend = True
End If
Loop While Not pageend AndAlso linecounter < printlines.Count - 1
br.Dispose()
e.HasMorePages = linecounter < printlines.Count - 1
End Sub
End Class |