I want to print a list of students in a class. My columns are things like
admno,sname,gender and class(
e.g form 1B,1G,2B,2G etc).
The code I use only print the available values from a dgv using the rectangle object. To draw the rectangle and print the dgv I import
System.Drawing.Printing
with the following event handling.
My question is how to add empty rows and empty columns until the page is full, just to allow that printed page to be filled manually with a pen.
What I have tried:
Dim i As Integer = 0
Dim newpage As Boolean = True
Dim WithEvents PrintDoc As New PrintDocument
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Dim psd As New PageSetupDialog
psd.Document = PrintDoc
If psd.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
Dim ppvw As New PrintPreviewDialog
ppvw.Document = PrintDoc
If ppvw.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
PrintDoc.Print()
End Sub
Private Sub PrintDoc_BeginPrint(ByVal sender As Object, ByVal e As PrintEventArgs) Handles PrintDoc.BeginPrint
i = 0
newpage = True
End Sub
Private Sub PrintDoc_PrintPage(ByVal sender As System.Object, ByVal e As PrintPageEventArgs) Handles PrintDoc.PrintPage
Dim format As New StringFormat(StringFormatFlags.LineLimit)
format.Alignment = StringAlignment.Near
format.Trimming = StringTrimming.EllipsisCharacter
Dim rc As Rectangle
Dim x As Int32 = e.MarginBounds.Left
Dim y As Int32 = e.MarginBounds.Top
Dim h As Int32
Dim row As DataGridViewRow
If newpage Then
row = dgv.Rows(i)
For Each cell As DataGridViewCell In row.Cells
If cell.Visible Then
rc = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)
e.Graphics.FillRectangle(Brushes.LightGray, rc)
e.Graphics.DrawRectangle(Pens.Black, rc)
e.Graphics.DrawString(dgv.Columns(cell.ColumnIndex).HeaderText, dgv.Font, Brushes.Black, rc, format)
x += rc.Width
h = Math.Max(h, rc.Height)
End If
Next
y += h
End If
newpage = False
For index As Int32 = i To dgv.RowCount - 1
If dgv.Rows(index).IsNewRow Then Exit For
row = dgv.Rows(index)
x = e.MarginBounds.Left
h = 0
x = e.MarginBounds.Left
For Each cell As DataGridViewCell In row.Cells
If cell.Visible Then
rc = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)
e.Graphics.DrawRectangle(Pens.Black, rc)
e.Graphics.DrawString(cell.FormattedValue.ToString(), dgv.Font, Brushes.Black, rc, format)
x += rc.Width
h = Math.Max(h, rc.Height)
End If
Next
y += h
i = index + 1
If y + h > e.MarginBounds.Bottom Then
e.HasMorePages = True
newpage = True
Return
End If
Next
End Sub