Printing Demo

The PrintGrid method is part of the VSFlexGrid control, and you may use it to print the grid contents directly to the printer.

If you need more sophisticated printing, with print preview and the ability to combine several grids and other elements such as tables and text in a single document, then you should use ComponentOne's VSPrinter control, a separate ComponentOne product that is part of the VSVIEW product.

This sample shows how you can print a grid using either method. Both methods allow you to control page breaks and to designate certain rows as "header" rows, which appear at the top of every page.

The example assumes you have a VSFlexGrid control named fg and a VSPrinter control named vp on your form.

    Sub PrintFlexGridBuiltIn()

        fg.PrintGrid "My Grid"

    End Sub

    Sub PrintFlexGridOnVSPrinter()

        vp.StartDoc

        vp.RenderControl = fg.hWnd

        vp.EndDoc

    End Sub

The routines above are all you need in order to print simple reports. The first uses the built-in PrintGrid method, and the second uses the VSPrinter control. The second method allows you to show the report on a preview window, render it on the printer, or save it to a file.

For printing complex reports, the VSFlexGrid control exposes events that allow you to control page breaks and to supply header rows which get printed at the top of each new page. The code below illustrates the use of these events:

    ' BeforePageBreak: controls page breaks

    ' we assume we have subtotals above details,

    ' and prevent subtotal rows from

    ' being the last on a page

    Private Sub fg_BeforePageBreak(ByVal Row As Long, BreakOK As Boolean)

        ' if this row is a subtotal heading, we can't break here

        If fg.IsSubtotal(Row) Then BreakOK = False

    End Sub

 

    ' GetHeaderRow: supplies header rows for new pages

    ' we assume we have title rows with RowData set to -1 

    ' that we want to show

    ' above the data

    Private Sub fg_GetHeaderRow(ByVal Row As Long, HeaderRow As Long)

        Dim r As Long

   

        ' ignore if the top row is a header already

        If fg.RowData(Row) = -1 Then Exit Sub

   

        ' we need a header, so find one

        For r = fg.FixedRows To fa.Rows - 1

            If fg.RowData(r) = -1 Then

                HeaderRow = r

                Exit Sub

            End If

        Next

    End Sub