Hi to all,
I have to preview dataset data using printdoc and my code is below.
Upon clicking the Preview button the preview dialog displays fine with data.
when I close the preview dialog then click the preview button the data is not diplayed.
weird behavior to me.
what should be the cause? Am I missing something?
Private Sub PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument.PrintPage
Dim PrintAreaHeight, PrintAreaWidth, marginLeft, marginRight, marginTop As Int32
With PrintDocument.DefaultPageSettings
' initializing local variables that contain the bounds of the printing area rectangle
PrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
PrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
' initializing local variables to hold margin values that will serve
' as the X and Y coordinates for the upper left corner of the printing
' area rectangle.
marginLeft = .Margins.Left
marginRight = .Margins.Right
marginTop = .Margins.Top
' X and Y coordinate
'.Landscape = True
End With
If PrintDocument.DefaultPageSettings.Landscape Then
Dim intTemp As Int32
intTemp = PrintAreaHeight
PrintAreaHeight = PrintAreaWidth
PrintAreaWidth = intTemp
' if the user selects landscape mode, swap the printing area height and width
End If
Dim TitleFont As Font = New Font("courier new", 24, FontStyle.Bold Or FontStyle.Underline)
Dim SubTitleFont As Font = New Font("courier new", 18, FontStyle.Bold)
Dim SubTitleFont1 As Font = New Font("courier new", 18, FontStyle.Bold Or FontStyle.Underline)
Dim BodyFont As Font = New Font("courier new", 12, FontStyle.Regular)
Dim BodyFont1 As Font = New Font("courier new", 14, FontStyle.Bold)
Dim str As String = "PAYMENTS BY CLASS - " & lblAcadyr.Text
' Get Invoice Data:
Dim InvSql As String
InvSql = "SELECT StudentID StudID,RollNo No,StudentNames Student ,Old_New, "
InvSql += "RegFee Reg,PTAFee PTA,OtherFee Other,TotalFee Total "
InvSql += "FROM tblStudents WHERE ClassID =" & cboClasses.Items(cboClasses.SelectedIndex)(0) & " ORDER BY RollNo"
Dim da As SqlDataAdapter = New SqlDataAdapter(InvSql, Conn)
Dim ds As New DataSet
da.Fill(ds, "tblStudents")
Dim LinesPerPage As Integer = Convert.ToInt32((e.MarginBounds.Height) / BodyFont.GetHeight(e.Graphics)) - 8
Dim LinesPrinted As Integer = 0
Static CentresPrinted As Integer = 0
Static StudentsPrinted As Integer = 0
Dim X, Y As Integer
Static pageno As Integer
X = e.MarginBounds.Left - 50
Y = e.MarginBounds.Top
If pageno = 0 Then 'put heading on first page
'centre the headertext and draw
Dim Xmid As Integer = Convert.ToSingle(e.PageBounds.Width / 2 - e.Graphics.MeasureString(str, TitleFont).Width / 2)
e.Graphics.DrawString(str, TitleFont, Brushes.Blue, Xmid, Y)
Y = Y + 50
'Get Classname
Dim Xmid1 As Integer = Convert.ToSingle(e.PageBounds.Width / 2 - e.Graphics.MeasureString("Class - " & cboClasses.Text, SubTitleFont).Width / 2)
e.Graphics.DrawString("Class - " & cboClasses.Text, SubTitleFont1, Brushes.Blue, Xmid1, Y)
Y = Y + 50
'column headers on each page
e.Graphics.DrawString("StudID", SubTitleFont, Brushes.Blue, X, Y)
X = X + 120
e.Graphics.DrawString("No", SubTitleFont, Brushes.Blue, X, Y)
X = X + 40
e.Graphics.DrawString("Student", SubTitleFont, Brushes.Blue, X, Y)
X = X + 190
e.Graphics.DrawString(" ", SubTitleFont, Brushes.Blue, X, Y)
X = X + 70
e.Graphics.DrawString("Reg.", SubTitleFont, Brushes.Blue, X, Y)
X = X + 80
e.Graphics.DrawString("PTA", SubTitleFont, Brushes.Blue, X, Y)
X = X + 70
e.Graphics.DrawString("Other", SubTitleFont, Brushes.Blue, X, Y)
X = X + 100
e.Graphics.DrawString("Total", SubTitleFont, Brushes.Blue, X, Y)
LinesPrinted = 3
Y = Y + 20
Else
Y = Y - FontHeight - 40 'shift startpoint upwards
'column headers on each page
e.Graphics.DrawString("StudID", SubTitleFont, Brushes.Blue, X, Y)
X = X + 120
e.Graphics.DrawString("No", SubTitleFont, Brushes.Blue, X, Y)
X = X + 40
e.Graphics.DrawString("Student", SubTitleFont, Brushes.Blue, X, Y)
X = X + 190
e.Graphics.DrawString(" ", SubTitleFont, Brushes.Blue, X, Y)
X = X + 70
e.Graphics.DrawString("Reg.", SubTitleFont, Brushes.Blue, X, Y)
X = X + 80
e.Graphics.DrawString("PTA", SubTitleFont, Brushes.Blue, X, Y)
X = X + 70
e.Graphics.DrawString("Other", SubTitleFont, Brushes.Blue, X, Y)
X = X + 100
e.Graphics.DrawString("Total", SubTitleFont, Brushes.Blue, X, Y)
LinesPrinted = 3
Y = Y + 20
End If
Dim Xleft As Integer
Dim FieldValue As String
Do While (LinesPrinted < LinesPerPage) And (StudentsPrinted <= ds.Tables(0).Rows.Count - 1)
'body
Y = Y + FontHeight + 8
Xleft = e.MarginBounds.Left - 50
FieldValue = ds.Tables(0).Rows(StudentsPrinted)("StudID").ToString
e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
Xleft = Xleft + 120
FieldValue = ds.Tables(0).Rows(StudentsPrinted)("No").ToString
e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
Xleft = Xleft + 40
FieldValue = ds.Tables(0).Rows(StudentsPrinted)("Student").ToString
If (FieldValue.Length > 17) Then
FieldValue = FieldValue.Remove(17, FieldValue.Length - 17)
End If
e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
Xleft = Xleft + 190
FieldValue = ds.Tables(0).Rows(StudentsPrinted)("Old_New").ToString
If FieldValue = String.Empty Then
e.Graphics.DrawString(" ", BodyFont, Brushes.Blue, Xleft, Y)
Else
e.Graphics.DrawString("[" & FieldValue & "]", BodyFont, Brushes.Blue, Xleft, Y)
End If
Xleft = Xleft + 70
FieldValue = ds.Tables(0).Rows(StudentsPrinted)("Reg").ToString
If FieldValue = String.Empty Then
FieldValue = " "
Else
FieldValue = CInt(FieldValue).ToString("n0")
End If
e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
Xleft = Xleft + 80
FieldValue = ds.Tables(0).Rows(StudentsPrinted)("PTA").ToString
If FieldValue = String.Empty Then
FieldValue = " "
Else
FieldValue = CInt(FieldValue).ToString("n0")
End If
e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
Xleft = Xleft + 70
FieldValue = ds.Tables(0).Rows(StudentsPrinted)("Other").ToString
If FieldValue = String.Empty Then
FieldValue = " "
Else
FieldValue = CInt(FieldValue).ToString("n0")
End If
e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
Xleft = Xleft + 100
FieldValue = ds.Tables(0).Rows(StudentsPrinted)("Total").ToString
If FieldValue = String.Empty Then
FieldValue = " "
Else
FieldValue = CInt(FieldValue).ToString("n0")
End If
e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
LinesPrinted = LinesPrinted + 1
StudentsPrinted = StudentsPrinted + 1
Y = Y
Loop
End Sub
Private Sub btnPreview_Click(sender As System.Object, e As System.EventArgs) Handles btnPreview.Click
If cboClasses.Text = String.Empty Then
MsgBox("Select Class.", MsgBoxStyle.Critical, "")
Exit Sub
Else
With PrintPreviewDialog
.Document = PrintDocument
.StartPosition = FormStartPosition.CenterScreen
.WindowState = FormWindowState.Maximized
.ShowDialog()
End With
End If
End Sub