Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleEasily print grid-like data in Visual Basic .NET
DescriptionThis example shows how to easily print grid-like data in Visual Basic .NET.
Keywordsgrid, print, print grid, data, VB.NET
CategoriesGraphics, VB.NET
 
When the program starts, it makes some test data.
 
' The index of the next row we need to print.
Private m_NextRow As Integer

Private Const ROW_HGT As Integer = 20
Private Const COL_WID As Integer = 100
Private Const NUM_ROWS As Integer = 100
Private Const NUM_COLS As Integer = 6

Private X_INDENT As Integer = 5
Private Y_INDENT As Integer = 5

' The sample data.
Private m_Data(,) As Integer

' Make some test data.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    Dim rand As New Random

    ReDim m_Data(NUM_ROWS, NUM_COLS)
    For i As Integer = 0 To m_Data.GetUpperBound(0) - 1
        For j As Integer = 0 To m_Data.GetUpperBound(1) - 1
            m_Data(i, j) = i * 1000 + j
        Next j
    Next i

    For i As Integer = 0 To m_Data.GetUpperBound(0) - 1
        m_Data(i, 0) = i
    Next i
    For j As Integer = 0 To m_Data.GetUpperBound(1) - 1
        m_Data(0, j) = j
    Next j
End Sub
 
The program's form contains a PrintPreviewDialog named ppdGrid. It's Document property is set to a PrintDocument object named pdocGrid.

When you click the Print Preview button, the following code executes. It sets m_NextRow to indicate that the next row to print is row number 0. It then calls the preview dialog's ShowDialog method.

 
Private Sub btnPrintPreview_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnPrintPreview.Click
    ' Start with the first row and page.
    m_NextRow = 0

    ' Start the preview process.
    ppdGrid.ShowDialog()
End Sub
 
The preview dialog uses the PrintDocument to generate the print out. When it needs data to print, the PrintDocument raises its PrintPage event.

The following event handler prints the next page of data. It sets y to the page's top margin. It then loops through the data starting at the next row to print.

For each row, the code sets x to the page's left margin. It then loops through the data's columns, printing the data values.

After each row, the program moves y down a bit and then checks whether there is room for the next row. If there is no more room, the program calls subroutine DrawGrid to draw a grid around the data, saves the index of the next row to print in m_NextRow, sets e.HasMorePages to True if there are more rows to print, and exits the event handler.

If the loop ends without reaching the Exit Sub statement, that means the program has printed all of the data. The code calls DrawGrid and sets e.HasMorePages to False so the PrintDocument doesn't try to print any more.

 
' Print the next page of the grid.
Private Sub pdocGrid_PrintPage(ByVal sender As _
    System.Object, ByVal e As _
    System.Drawing.Printing.PrintPageEventArgs) Handles _
    pdocGrid.PrintPage
    ' Debugging: Draw the page margins.
    'Dim the_pen As New Pen(Color.Blue, 5)
    'the_pen.DashStyle = Drawing2D.DashStyle.Dash
    'e.Graphics.DrawRectangle(the_pen, e.MarginBounds)
    'the_pen.Dispose()

    ' Start at the upper margin.
    Dim y As Integer = e.MarginBounds.Top

    ' Print rows.
    For i As Integer = m_NextRow To m_Data.GetUpperBound(0) _
        - 1
        ' Start at the left margin.
        Dim x As Integer = e.MarginBounds.Left

        ' Print columns
        For j As Integer = 0 To m_Data.GetUpperBound(1) - 1
            e.Graphics.DrawString( _
                m_Data(i, j), _
                Me.Font, Brushes.Black, _
                x + X_INDENT, _
                y + Y_INDENT)
            x += COL_WID
        Next j

        ' Move down a bit.
        y += ROW_HGT

        ' See if we're at the bottom of the page.
        If y + ROW_HGT > e.MarginBounds.Bottom Then
            ' End of the page.
            ' Draw a grid.
            DrawGrid(e, y)

            ' Move to the next row of data.
            m_NextRow = i + 1
            e.HasMorePages = (m_NextRow <= _
                m_Data.GetUpperBound(0) - 1)
            Exit Sub
        End If
    Next i

    DrawGrid(e, y)
    e.HasMorePages = False
End Sub
 
Subroutine DrawGrid draws a grid around the rows and columns indicated by its parameters.
 
' Draw a grid around the page's data.
Private Sub DrawGrid(ByVal e As _
    System.Drawing.Printing.PrintPageEventArgs, ByVal y_max _
    As Integer)
    ' Draw column lines.
    Dim x As Integer = e.MarginBounds.Left
    Dim y As Integer = e.MarginBounds.Top
    For i As Integer = 0 To m_Data.GetUpperBound(1)
        e.Graphics.DrawLine(Pens.Green, x, y, x, y_max)
        x += COL_WID
    Next i

    ' Draw row lines.
    x = e.MarginBounds.Left
    Dim xmax As Integer = x + m_Data.GetUpperBound(1) * _
        COL_WID
    y = e.MarginBounds.Top
    Do While y <= y_max
        e.Graphics.DrawLine(Pens.Green, x, y, xmax, y)
        y += ROW_HGT
    Loop
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated