bobcann
asked on
How to Pass DataTable Content to a StreamReader?
I need to print a report directly from a DataTable to stream to a printer. What I am missing is how to get the table's content into a stream to send to the printer and print each row on a new line.
Dim da As New SqlServerCe.SqlCeDataAdapt er(strQuer y, gc_ConnString)
Dim TableReport As New DataTable
da.Fill(TableReport)
strReport = TableReport.Rows.ToString( )
streamToPrint = New StreamReader(<table content>)
Thank you for any advice or solution.
Dim da As New SqlServerCe.SqlCeDataAdapt
Dim TableReport As New DataTable
da.Fill(TableReport)
strReport = TableReport.Rows.ToString(
streamToPrint = New StreamReader(<table content>)
Thank you for any advice or solution.
Imports System
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Printing
Public Class WriteReport
Dim clsErrLog As New ErrorLog
Private components As System.ComponentModel.Container
Private printFont As Font
Private streamToPrint As StreamReader
Dim strReport As String
Public Sub Print(ByRef strQuery As String)
Try
Dim da As New SqlServerCe.SqlCeDataAdapter(strQuery, gc_ConnString)
Dim TableReport As New DataTable
da.Fill(TableReport)
strReport = TableReport.Rows.ToString()
streamToPrint = New StreamReader(<datatable content>) <<
Try
printFont = New Font("Arial", 10)
Dim pd As New PrintDocument()
AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
pd.Print()
Finally
streamToPrint.Close()
End Try
Catch ex As Exception
clsErrLog.LogError(ex.Message, "WriteReport.Print")
End Try
End Sub
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim linesPerPage As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String = Nothing
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
While count < linesPerPage
line = streamToPrint.ReadLine()
If line Is Nothing Then
Exit While
End If
yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
count += 1
End While
If (line IsNot Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
All I need to do is:
Open in new window