Results 1 to 3 of 3

Thread: Print a Datagrid Control

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Posts
    95

    Print a Datagrid Control

    Hi All,
    How can I send the contents of a Datagrid to a printer on LPT1 with the table names in order and the rows and columns tabulated correctly.
    Cheers
    R

  2. #2
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    Printing is the most annoying part of Windows programming. You can use a 3rd party control or u can code it. Here is some sample code for a FlexGrid... am sure u can adjust for DataGrid.
    Regards
    Stuart

    VB Code:
    1. 'Print Grid for any page
    2. Sub priPrintGrid()
    3.     Dim lintPageNum As Integer
    4.     Dim lintGridWidth As Integer
    5.     Dim lintScaleLeft As Integer
    6.     Dim lintScaleWidth As Integer
    7.    
    8.     Dim llngRows As Long
    9.     Dim llngCols As Long
    10.    
    11.     Dim lstrText As String
    12.     Dim lintLeftPosn As Integer
    13.     Dim lintColWidth As Integer
    14.     Dim lintColAlign As Integer
    15.     Dim lintTextWidth As Integer
    16.     Dim lintPrintSpace As Integer
    17.    
    18.     On Error Resume Next
    19.     lintPageNum = 1
    20.     lintGridWidth = Grid1.Width
    21.     lintScaleLeft = Printer.ScaleLeft
    22.     lintScaleWidth = Printer.ScaleWidth
    23.  
    24.     'Print Grid rows
    25.     With Grid1
    26.         For llngRows = 0 To .Rows -1
    27.             For llngCols = 0 To .Cols - 1
    28.                 lstrText = Trim$(.TextMatrix(llngRows, llngCols))
    29.                 lintTextWidth = Printer.TextWidth(lstrText)
    30.                 lintLeftPosn = .ColPos(llngCols) / lintGridWidth * lintScaleWidth
    31.                 lintColWidth = .ColWidth(llngCols) / lintGridWidth * lintScaleWidth
    32.                 lintColAlign = .ColAlignment(llngCols)
    33.                
    34.                 'Print cell
    35.                 priPrintCell lstrText, lintTextWidth, lintLeftPosn, lintColWidth, lintColAlign
    36.             Next
    37.             Printer.Print " "
    38.        
    39.             'Test for new page
    40.             With Printer
    41.                 lintPrintSpace = .ScaleHeight - .ScaleTop - .CurrentY
    42.             End With
    43.             If lintPrintSpace < 3 * Printer.TextHeight("A") Then
    44.                 lintPageNum = lintPageNum + 1
    45.                 If lintPageNum \ 5 = lintPageNum / 5 Then Printer.EndDoc
    46.             End If
    47.         Next
    48.     End With    
    49. End Sub
    50.  
    51. 'Print grid cells
    52. Sub priPrintCell(lstrText As String, lintTextWidth As Integer, _
    53.     lintLeftPosn As Integer, lintColWidth As Integer, _
    54.     lintColAlign As Integer)
    55.            
    56.     Dim lintMaxText As Integer
    57.     On Error Resume Next
    58.     If lintColAlign = flexAlignLeftCenter Then  'Grid / total
    59.         Printer.CurrentX = lintLeftPosn + 80 'Left align
    60.     Else
    61.         Printer.CurrentX = lintLeftPosn + (lintColWidth - lintTextWidth) 'Right align
    62.     End If
    63.    
    64.     'In case text is wider than column
    65.     If lintColWidth > 0 And lintColWidth < lintTextWidth Then 'Reduce column width
    66.         lintMaxText = Round(lintColWidth / lintTextWidth * Len(lstrText), 0) - 1
    67.         lstrText = Left$(lstrText, lintMaxText)
    68.     End If
    69.     If lintColWidth > 0 Then Printer.Print lstrText;
    70. End Sub

    This is basic and u can add stuff to print lines, bolding etc etc... also i have cut and pasted stuff here and removed bits and pieces so if u get an error pls let me know specific type and i will fix
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  3. #3
    Addicted Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    160

    MSFLEXGRID PRINTING

    Is there something very very very simple and alot smaller to use that
    will work with alot of columns?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width