VB 6.0 - how to print datagrid in vb6?

Asked By gl0omy sun
02-Feb-10 07:40 PM
can i know the code to print datagrid in vb6? i'm out of idea.........help me...

Does this snippet help?  Does this snippet help?

02-Feb-10 09:31 PM
Private Sub CmdPrint_Click()
Dim RRow As Integer, RecCount as integer
reccount=adodc1.recordset.recordcount
printer.print ""
For RRow = 0 To 13 ' change this to suit your need 
or reccount
DataGrid1.Row = RRow
printerPrint DataGrid1.Columns(0).Text, Space(1), DataGrid1.Columns(1).Text, Space(1), DataGrid1.Columns(2).Text  '
Next
printer.enddoc
End Sub 

Source : Click http://en.allexperts.com/q/Visual-Basic-1048/print-Data-Grid-data.htm
  Web Star replied to gl0omy sun
02-Feb-10 09:34 PM

try this

Private Function PrintDataGrid(ByVal g As Graphics) As Boolean
  Dim sf As StringFormat = New StringFormat
  PageCounter = PageCounter + 1
  'if we want to print the grid right to left
  If (bRightToLeft) Then
    CurrentX = PrintDoc.DefaultPageSettings.PaperSize.Width_
               - PrintDoc.DefaultPageSettings.Margins.Right
    sf.FormatFlags = StringFormatFlags.DirectionRightToLeft
  Else
    CurrentX = PrintDoc.DefaultPageSettings.Margins.Left
  End If

  Dim i As Integer
  For i = CurrentRow To PrintGrid.Rows - 1
    Dim j As Integer
    For j = 0 To PrintGrid.Columns - 1
      'set cell alignment
      Select Case (PrintGrid.Cell(i, j).Alignment)
        'left
        Case HorizontalAlignment.Left
          sf.Alignment = StringAlignment.Near
        Case HorizontalAlignment.Center
          sf.Alignment = StringAlignment.Center
        'right
        Case HorizontalAlignment.Right
          sf.Alignment = StringAlignment.Far
      End Select

      'advance X according to order
      If (bRightToLeft) Then
        'draw the cell bounds (lines) and back color
        g.FillRectangle(New SolidBrush(PrintGrid.BackColor), _
            CurrentX - PrintGrid.Cell(i, j).Width, CurrentY, _
            PrintGrid.Cell(i, j).Width, PrintGrid.Cell(i, j).Height)
        g.DrawRectangle(New Pen(PrintGrid.LineColor), _
            CurrentX - PrintGrid.Cell(i, j).Width, CurrentY, _
            PrintGrid.Cell(i, j).Width, PrintGrid.Cell(i, j).Height)

        'draw the cell text
        g.DrawString(PrintGrid.Cell(i, j).CText, _
            PrintGrid.Cell(i, j).Font, New SolidBrush(PrintGrid.ForeColor),_
            New RectangleF(CurrentX - PrintGrid.Cell(i, j).Width, _
            CurrentY, PrintGrid.Cell(i, j).Width, _
            PrintGrid.Cell(i, j).Height), sf)

        'next cell
        CurrentX -= PrintGrid.Cell(i, j).Width
      Else
        'draw the cell bounds (lines) and back color
        g.FillRectangle(New SolidBrush(PrintGrid.BackColor), _
          CurrentX, CurrentY, PrintGrid.Cell(i, j).Width, _
          PrintGrid.Cell(i, j).Height)
        g.DrawRectangle(New Pen(PrintGrid.LineColor), CurrentX, _
          CurrentY, PrintGrid.Cell(i, j).Width, _
          PrintGrid.Cell(i, j).Height)
        'Draw text by alignment
        g.DrawString(PrintGrid.Cell(i, j).CText, _
          PrintGrid.Cell(i, j).Font, New SolidBrush(PrintGrid.ForeColor),_
          New RectangleF(CurrentX, CurrentY, _
          PrintGrid.Cell(i, j).Width, PrintGrid.Cell(i, j).Height), sf)

        'next cell
        CurrentX += PrintGrid.Cell(i, j).Width
      End If
    Next

    'reset to beginning
    If (bRightToLeft) Then
      'right align
      CurrentX = PrintDoc.DefaultPageSettings.PaperSize.Width_
                 - PrintDoc.DefaultPageSettings.Margins.Right
    Else
      'left align
      CurrentX = PrintDoc.DefaultPageSettings.Margins.Left
    End If

    'advance to next row
    CurrentY += PrintGrid.Cell(i, 0).Height
    CurrentRow += 1
    'if we are beyond the page margin (bottom)
    'then we need another page,
    'return true
    If (CurrentY > PrintDoc.DefaultPageSettings.PaperSize.Height_
              - PrintDoc.DefaultPageSettings.Margins.Bottom) Then
      Return True
    End If
  Next
  Return False
End Function

 

http://www.codeproject.com/KB/printing/DataGrid_Printing_Class.aspx

  Sakshi a replied to gl0omy sun
02-Feb-10 09:57 PM

Private Sub Command2_Click()
Dim i As Integer
For i = 0 To DataGrid1.Columns.Count - 1
DataGrid1.Col = i
printer.Print DataGrid1.Text
Next
End Sub
printer.enddoc


Thanks and Regards,
http://www.CodeCollege.NET
http://www.InterviewsGuru.info

  Sandra Jain replied to gl0omy sun
03-Feb-10 02:27 AM
Try this code:

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Dim stringWrite As New System.IO.StringWriter()
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
Dim w As System.Web.UI.WebControls.Unit
w.Pixel(400)
DataGrid1.Width = w
Dim scr As String = "<script>function window.onafterprint(){history.back(1);}</script>"

htmlWrite.Write(scr)
Dim str As String = "" '"<div align=center id=titl1><h3><Font face=Verdana>Your DataGrid:</font></h3></div>"
htmlWrite.Write(str)
DataGrid1.RenderControl(htmlWrite)
Dim strHTML As String = stringWrite.ToString()
Response.Clear()
' you could send it to Excel, too....
'Response.ContentType = "application/vnd.ms-excel"

Response.ContentType = "text/HTML"
Response.Write(strHTML)
Response.Write("<script>window.print();</script>")
Response.End()
End Sub

  gl0omy sun replied to Sandra Jain
03-Feb-10 08:57 PM
aren't this code for vb.net?
Create New Account
help
excel Excel excel Excel Discussions Excel (1) ExcelSalesAid (1) GuillettMicrosoft (1) SalesAid (1) Dguillett1 (1) I will certainly try to - - Don Guillett Microsoft MVP Excel SalesAid Software dguillett1@austin.rr.com keywords: excel description: excel
How do I Fill down on Excel Excel Excel Miscellaneous Discussions Excel (1) RegardsDave (1) Hawley (1) http: / / www.ozgrid.com / Excel / excel-fill-handle.htm - - Regards Dave Hawley www.ozgrid.com keywords: How, do, I, Fill, down, on, Excel
Why doesn't the snap feature work in Excel? Excel Excel Miscellaneous Discussions Microsoft Excel (1) Excel (1) Word (1) Bliengme (1) Snaps (1) SDI (1) Are you referring to Win7's snap feature? My Excel snaps as expected best wishes - - Bernard Liengme Microsoft Excel MVP http: / / people.stfx.ca / bliengme Taken from a post at Microsoft Answers: The snap feature that you are looking for will not work unless you open two instances of Excel. This is because Excel Unlike Word is not a True SDI Application. Microsoft is aware
excel Excel Excel Discussions Excel (1) You are in the correct news group. Now try a question. Gord Dibben MS Excel MVP keywords: excel