how to print datagrid in vb6?

Asked By gl0omy sun
02-Feb-10 07:40 PM
Earn up to 0 extra points for answering this tough question.
can i know the code to print datagrid in vb6? i'm out of idea.........help me...

  Does this snippet help?

[)ia6l0 iii replied to gl0omy sun
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 here

  re: how to print datagrid in vb6?

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

  re: how to print datagrid in vb6?

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,
www.CodeCollege.NET
www.InterviewsGuru.info

  re: how to print datagrid in vb6?
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

  re: re: how to print datagrid in vb6?
gl0omy sun replied to Sandra Jain
03-Feb-10 08:57 PM
aren't this code for vb.net?
Create New Account