Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing a Datagrid 1

Status
Not open for further replies.

RonRepp

Technical User
Feb 25, 2005
1,031
US
Hi all:

I don't know if this has been covered or not, but I was wondering how you would handle printing a datagrid or flexgrid.

Any suggestions?

Thanks,

Ron

 
Here's a routine I created. There are some global values that are used that will need to be replaced with the corresponding grid properties, etc. (I created a flexgrid class to handle all the complexity), but it will give you an idea of how to handle it:
Code:
Public Sub PrintFlexGrid(Header As String)

'   Prints the data in the flexgrid (visible columns only)

    Dim r As Integer, c As Integer
    Dim maxColSize(100) As Single
    Dim maxGridSize As Single
    Dim startPos As Single
    Dim p As String
    Dim StartCol As Integer
    Dim TotColWidth As Single

    If fg.Rows = 1 Then Exit Sub

 '  Check for printer

    On Error Resume Next
    p = Printer.DeviceName
    If Err.Number = 484 Then
        MsgBox "No printer was found. Please add a printer", vbInformation, "Print
Grid"
        Exit Sub
    End If
    On Error GoTo 0

    TextSize = txtSize

'   Initialize printer - default to portrait - text measurements are in number of
characters

    Printer.FontName = "arial"
    Printer.FontSize = TextSize
    Printer.ScaleMode = vbInches
    Printer.Orientation = vbPRORPortrait

'   Find max width for all columns

    startPos = Printer.TextWidth("W") * 3
    For c = 0 To fg.cols - 1
        maxColSize(c) = 0
        For r = 0 To fg.Rows - 1
            If maxColSize(c) < Printer.TextWidth(fg.TextMatrix(r, c)) Then _
            maxColSize(c) = Printer.TextWidth(fg.TextMatrix(r, c))
        Next

'   Add 2 characters padding between columns

        maxColSize(c) = maxColSize(c) + Printer.TextWidth("W") * 2
        maxGridSize = maxGridSize + maxColSize(c)
    Next

'   Check if fits portrait, if not, switch to landscape

    If maxGridSize + startPos * 2 > Printer.ScaleWidth Then
        Printer.Orientation = vbPRORLandscape
    End If

'   Starting with first column, add widths.  When sum of column widths
'   exceed the page width, print columns.  Columns that don't fit, overflow
'   to a new page.  If any one column exceeds the width of the page,
'   truncate the column to fit on one page.

    c = 0
    Do
        If columnVisible(c) Then TotColWidth = TotColWidth + maxColSize(c)

        If TotColWidth > Printer.ScaleWidth Or c = fg.cols - 1 Then
            If TotColWidth = 0 Then Exit Do
            If StartCol <> c And c <> fg.cols - 1 Then c = c - 1

'   Print page header and column headers

            PrintHeader Header
            PrintRow 0, startPos, StartCol, c, maxColSize, True

'   Print each row, positioning to the print position for each column
'   Allow bottom margin of 3 rows

            For r = 1 To fg.Rows - 1
                PrintRow r, startPos, StartCol, c, maxColSize

                If Printer.CurrentY + Printer.TextHeight("W") * 3 > Printer.ScaleHeight
Then
                    Printer.NewPage
                    PrintHeader Header
                    PrintRow 0, startPos, StartCol, c, maxColSize, True
                End If
            Next

            Printer.NewPage

            TotColWidth = 0
            c = c + 1
            StartCol = c
         Else
            c = c + 1
        End If
    Loop Until c > fg.cols - 1

    Printer.EndDoc

End Sub

Private Sub PrintRow(r As Integer, startPos As Single, StartCol As Integer, EndCol
As Integer, maxColSize() As Single, Optional bold As Boolean = False)

'   Print a row, positioning to the print position for each column

    Dim c As Integer
    Dim pos As Single
    Dim Y As Single

    pos = startPos
    Printer.FontBold = bold
    Y = Printer.CurrentY
    For c = StartCol To EndCol
        If columnVisible(c) Then
            Printer.CurrentX = pos
            Printer.CurrentY = Y
            Printer.Print fg.TextMatrix(r, c)
            pos = pos + maxColSize(c)
        End If
    Next

End Sub

Private Sub PrintHeader(Header As String)

'   Print the header line, reset text size and print page number

    Dim Y As Single

    Printer.FontSize = headerSize
    Printer.FontBold = False
    Printer.CurrentY = Printer.TextHeight("W") * 2
    Printer.CurrentX = (Printer.ScaleWidth - Printer.TextWidth(Header)) / 2
    Printer.Print Header
    Printer.FontSize = TextSize
    Printer.Print
    Y = Printer.CurrentY

    Printer.CurrentX = Printer.ScaleWidth / 2 - Printer.TextWidth("W") * 2
    Printer.CurrentY = Printer.ScaleHeight - Printer.TextHeight("W") * 1.5
    Printer.Print "Page " + CStr(Printer.Page)
    Printer.CurrentY = Y

End Sub

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top