Click here to Skip to main content
15,562,550 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
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
    ' sets it to show '...' for long text
    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
    'print the header text for a new page, use a grey BG just like the control
    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
    'now print the data for each row
    For index As Int32 = i To dgv.RowCount - 1
        'no need to try to print the new row
        If dgv.Rows(index).IsNewRow Then Exit For
        row = dgv.Rows(index)
        x = e.MarginBounds.Left
        h = 0
        'reset X for data
        x = e.MarginBounds.Left
        ' print the data
        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
        'next row to print
        i = index + 1
        If y + h > e.MarginBounds.Bottom Then
            e.HasMorePages = True
            newpage = True
            Return
        End If
    Next
End Sub
Posted
Updated 15-Oct-22 9:58am
v3

1 solution

You're already printing "rows and columns" using existing values. Put the (for loop) logic in separate methods that you can call with parameters. In that way you "add" extra / custom rows and columns (using "blank" parameters for extra empty rows and columns).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900