Dim i As Integer = 0
Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim w As Integer = e.MarginBounds.Width
Dim h As Integer = e.MarginBounds.Height
Dim t As Integer = e.MarginBounds.Top
Dim l As Integer = e.MarginBounds.Left
Dim pen As New Pen(Brushes.Black, 0.1)
Dim formatcenter As New StringFormat
formatcenter.Alignment = StringAlignment.Center
Dim formatnear As New StringFormat
formatnear.Alignment = StringAlignment.Near
Dim fontBold As New Font("arial", 12, FontStyle.Bold)
Dim fontRegular As New Font("arial", 12, FontStyle.Regular)
Dim rect As New RectangleF(l, t, w, h)
e.Graphics.DrawString("Başlık", fontBold, Brushes.Black, rect, formatcenter)
rect.Y += fontBold.Height
rect.Y += fontBold.Height
' If rect.Y > h + 3 * fontBold.Height Then
rect.Width = e.MarginBounds.Width / 5
rect.Height = fontBold.Height
rect.X = l
e.Graphics.DrawString(DataGridView1.Columns(0).HeaderText, fontBold, Brushes.Black, rect, formatnear)
e.Graphics.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height)
rect.X += rect.Width
e.Graphics.DrawString(DataGridView1.Columns(1).HeaderText, fontBold, Brushes.Black, rect, formatnear)
e.Graphics.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height)
rect.X += rect.Width
e.Graphics.DrawString(DataGridView1.Columns(2).HeaderText, fontBold, Brushes.Black, rect, formatnear)
e.Graphics.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height)
rect.X += rect.Width
e.Graphics.DrawString(DataGridView1.Columns(3).HeaderText, fontBold, Brushes.Black, rect, formatnear)
e.Graphics.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height)
rect.X += rect.Width
e.Graphics.DrawString(DataGridView1.Columns(4).HeaderText, fontBold, Brushes.Black, rect, formatnear)
e.Graphics.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height)
Dim state As Boolean = True
While i < DataGridView1.Rows.Count
rect.Y += fontBold.Height
rect.X = l
If state Then e.Graphics.FillRectangle(Brushes.LightGray, rect.X, rect.Y, rect.Width, rect.Height)
e.Graphics.DrawString(DataGridView1.Item(0, i).Value.ToString, fontRegular, Brushes.Black, rect, formatnear)
e.Graphics.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height)
rect.X += rect.Width
If state Then e.Graphics.FillRectangle(Brushes.LightGray, rect.X, rect.Y, rect.Width, rect.Height)
e.Graphics.DrawString(DataGridView1.Item(1, i).Value.ToString, fontRegular, Brushes.Black, rect, formatnear)
e.Graphics.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height)
rect.X += rect.Width
If state Then e.Graphics.FillRectangle(Brushes.LightGray, rect.X, rect.Y, rect.Width, rect.Height)
e.Graphics.DrawString(DataGridView1.Item(2, i).Value.ToString, fontRegular, Brushes.Black, rect, formatnear)
e.Graphics.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height)
rect.X += rect.Width
If state Then e.Graphics.FillRectangle(Brushes.LightGray, rect.X, rect.Y, rect.Width, rect.Height)
e.Graphics.DrawString(DataGridView1.Item(3, i).Value.ToString, fontRegular, Brushes.Black, rect, formatnear)
e.Graphics.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height)
rect.X += rect.Width
If state Then e.Graphics.FillRectangle(Brushes.LightGray, rect.X, rect.Y, rect.Width, rect.Height)
e.Graphics.DrawString(DataGridView1.Item(4, i).Value.ToString, fontRegular, Brushes.Black, rect, formatnear)
e.Graphics.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height)
i += 1
state = Not state
If rect.Y > h + 3 * fontBold.Height Then
rect.Y = t
rect.X = l
Exit While
End If
End While
If i < DataGridView1.Rows.Count Then e.HasMorePages = True Else e.HasMorePages = False : i = 0
End Sub