If you would like to add header and footer, you have to remember this:
1. calculate the distance that you need to reserve for the header and footer
2. print header
3. print data from datagridview (do not forget measuring text the space you need for footer)
4. print footer
5. go to next page if there's still something to print
Dim linesPerPage As Single = 0
Dim line As String = Nothing
Dim count As Long = 0
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
linesPerPage -= 4
ev.Graphics.DrawString("Header's text goes here", printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
ev.Graphics.DrawString("---", printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
While count < linesPerPage
line = DataGridView.Rows(count).Cell(0).Value
ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
count += 1
End While
ev.Graphics.DrawString("---", printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
ev.Graphics.DrawString("Footer's text goes here", printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
If (line IsNot Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
For further details, please see:
PrintDocument.PrintPage Event (System.Drawing.Printing)[
^]
Printing of DataGridView[
^]