Click here to Skip to main content
15,906,467 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I have an app in which the user can print the content of some datagridviews. One of the grids is fairly narrow, so when I print it doesn't use the full width of the page. It does, however, take up 2 or 3 pages in length. Rather than this I would like to be able to split the table across the page, ie. rather than have 3 pages with one table, I want one page with 3 tables side by side.

If anyone can give me some idea as to what I should be looking for I would be very grateful. Although some code would be nice I am happy to be pointed in the right direction.

Many Thanks,


Probably, you are printing the form or DataGridView object itself, instead of "doing teh job properly".

Instead, use a PrintDocument[^] and you can decide exactly where everyuthign goes (and how large it is) - so if you want two columns printed, you can. If you want three and it printed in landscape, you can do that as well.
It may seem complicated, but it's pretty simple really, and well worth getting to grips with in the long run.

The link includes a basic example.
Share this answer
Hi OriginalGriff,

Thank you for your response....

Here is the code I am using at the moment so you can see yourself if I am "doing the job properly".

This part is the "click event"......

Private Sub DomainsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles DomainsToolStripMenuItem.Click
        Me.PrintPreviewDialog1.Document = New System.Drawing.Printing.PrintDocument
        PrintDocument2.DefaultPageSettings.Landscape = True
        AddHandler PrintPreviewDialog1.Document.PrintPage, _
               AddressOf PrintDocument2_PrintPage

        pageNumber = 1
    End Sub

Here is the Print Document.....

Private Sub PrintDocument2_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument2.PrintPage
        PrintDocument2.DefaultPageSettings.Landscape = True
        Static mRow As Integer = 0
        Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
        Dim hfmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
        Dim tfmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
        Dim drwfont As New Font("Arial", 7.5)
        Dim drawfont As New Font("Arial", 11, FontStyle.Bold)
        fmt.Alignment = StringAlignment.Center
        hfmt.Alignment = StringAlignment.Center
        fmt.LineAlignment = StringAlignment.Center
        hfmt.LineAlignment = StringAlignment.Center
        fmt.Trimming = StringTrimming.EllipsisCharacter
        tfmt.Alignment = StringAlignment.Center
        tfmt.LineAlignment = StringAlignment.Center

        'Print the page number.
        e.Graphics.DrawString(Me.pageNumber, New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, 400, 1050, tfmt)

        If pageNumber = 1 Then
            Dim MyString As String = "Document Title"
            e.Graphics.DrawString(MyString, New Font("Arial", 16, FontStyle.Bold), Brushes.Black, 230, 60)
        End If

        Dim y0 As Single = 150
        Dim y As Single = y0
        With DataGridView1
            While mRow < .RowCount
                Dim row As DataGridViewRow = .Rows(mRow)
                Dim x As Single = 20
                Dim h As Single = 0
                For Each cell As DataGridViewCell In row.Cells
                    Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width + 2, cell.Size.Height)
                    e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
                    If (y = y0) Then
                        e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText, drawfont, Brushes.Black, rc, hfmt)
                        e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), drwfont, Brushes.Black, rc, fmt)
                    End If
                    x += rc.Width
                    h = Math.Max(h, rc.Height)

                y += h
                mRow += 1
                If (y + h) > e.MarginBounds.Bottom Then Exit While
            End While
            If mRow < .RowCount Then e.HasMorePages = True Else mRow = 0
            pageNumber += 1
        End With
    End Sub

Not really sure how this might compare to the link you provided.

At the moment the code works perfectly fine for a larger table with a lot more columns but as I stated in my original question, as a particular grid is fairly narrow, I would prefer it if the table was split across a single page into 2 or 3 tables and then move to a new page rather than one long table over 2 or 3 pages.

I hope all this makes sense!!!?/

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