[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Printing from a Datagrid

I am trying to allow a user to print out information from a database query that is appearing to him in a datagrid.  What is the easiest way to print out this information since a datagrid doesn't have a print method?
0
sladej
Asked:
sladej
1 Solution
 
tkuppinenCommented:
Add a data report to your project and bind the datasource to the smae datasource to which the grid is bound.
0
 
Éric MoreauSenior .Net ConsultantCommented:
I have this code that I never tried.

Public Sub MSFlexGridPrint(gridQuery As Control)
   ' *** Print MSFlexGrid

   Dim I          As Integer
   Dim J          As Integer
   Dim PTab       As Integer

   For I = 1 To gridQuery.Rows - 1
      PTab = 20 'Set the first tab value
      'This checks to see if a page break is needed

      If Printer.CurrentY + Printer.TextHeight(gridQuery.Text) > Printer.ScaleHeight - 600 Then
         Printer.NewPage
      End If

      gridQuery.Row = I 'Set the active row

      For J = 0 To gridQuery.Cols - 1
         gridQuery.Col = J 'Set the current column
           
         'Send the field to the print line and add the tab.
         'Notice the semicolon at the end of the line.

         Printer.Print Trim$(gridQuery.Text); Tab(PTab);

         PTab = PTab + 20 'Increment the tab value
      Next

      'After each column has printed to the print line

      'a Printer.Print statement without a semicolon will
      'will send the line to the printer and start a new
      'one.
      Printer.Print
   Next

End Sub    
 
Please tell me your result!
0
 
brandontCommented:
The easiest way is definitely NOT to create a data report.  If you don't need a lot of formatting options, just use the printer object and send all the info from the grid (similar to above comment).  I will generally declare a variable for each column that I want printed, and set them to the horizontal location on the page where I want it to print (i.e. COL1 = 1.5: COL2 = COL1 + 1, etc).  Below is an example of how I generally handle it.

Sub PrintHline(lmar As Double, rmar As Double)
    Printer.Line (lmar, Printer.CurrentY)-(rmar, Printer.CurrentY)
    Printer.CurrentX = lmar
End Sub
Sub LineFeed(xPosition As Double)
    Dim LF As String: LF = Chr(13)
    Printer.Print LF
    Printer.CurrentX = xPosition
End Sub
Sub PrintText(dblXpos As Double, dblYpos As Double, strText As String)
    Printer.CurrentX = dblXpos: Printer.CurrentY = dblYpos
    Printer.Print strText
End Sub
Sub PrintDoc(lmar As Double, rmar As Double, tmar As Double, bmar As Double, Optional SelectOnly As Boolean)
    Dim Y As Double, lngRow As Integer, lngCol As Integer ', lngNumItems As Integer
    Dim COL1 As Double, COL2 As Double, COL3 As Double, COL4 As Double, COL5 As Double
    Dim intStart As Integer, intEnd As Integer
    Printer.FontName = "Arial"
    Printer.FontSize = 8
   
    COL1 = lmar: COL2 = COL1 + 2.25: COL3 = COL2 + 0.75: COL4 = COL3 + 0.5: COL5 = COL4 + 0.5
    Printer.FontBold = True: Printer.FontSize = 10
    PrintText lmar, tmar, "Laser Library Tracker - Printed on: " & Date & " at " & Time
    Printer.FontSize = 8
    PrintHline lmar, rmar
    LineFeed lmar
    Y = Printer.CurrentY
    PrintText COL1, Y, "Form": PrintText COL2, Y, "Status": PrintText COL3, Y, "Date"
    PrintText COL4, Y, "Tstr": PrintText COL5, Y, "Problem"
    PrintHline lmar, rmar
    Printer.FontBold = False
    Y = Printer.CurrentY + 0.05
    'Print items in grid
    With frmMain.MSFlexGrid1
        If SelectOnly Then
            intStart = .Row
            intEnd = .RowSel
        Else
            intStart = 1
            intEnd = .Rows - 1
        End If
        'lngNumItems = .Rows
        'For lngRow = 1 To lngNumItems - 1
        For lngRow = intStart To intEnd
            .Row = lngRow
            .Col = GRD_FORM: PrintText COL1, Y, .Text
            .Col = GRD_STATUS: PrintText COL2, Y, .Text
            .Col = GRD_DATE: PrintText COL3, Y, .Text
            .Col = GRD_INITIALS: PrintText COL4, Y, .Text
            .Col = GRD_PROBLEM: PrintText COL5, Y, .Text
            If lngRow Mod 10 = 0 Then
                Y = Printer.CurrentY + 0.12
                Else
                Y = Printer.CurrentY + 0.04
            End If
            If Y >= bmar Then Printer.NewPage: Y = tmar
        Next
    End With
    Printer.EndDoc
    Exit Sub
End Sub
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now