Redraw PrintDocument with different data

  • Thread starter Thread starter JamesBuss
  • Start date Start date
J

JamesBuss

Guest
This seems like it should be simple, yet the answer eludes me.

I am using PrintPage event to create a report on a PrintDocument. The PrintDocument is displayed in a PrintPreviewDialog. I have added some filter options to the ToolStrip of the PrintPreviewDialog. When the user selects one of the filter options, the report needs to be recreated using the selected filter in the same PrintPreviewDialog. I can't seem to figure out how to get it to recreate the PrintDocument.

I've tried PrintPreviewDialog.Refresh which doesn't cause the PrintDocument to get redrawn with the newly sorted data. I've tried PrintDialog.Print which only causes the newly sorted data report to get sent to the printer. I've even tried calling the PrintReport() subroutine but that just results in an exception that I'm trying to call the ShowModal() function on a window that is already shown modal.

Sample code:

Public Sub PrintReport()
Dim sortNameToolStripButton = New System.Windows.Forms.ToolStripButton
sortNameToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text
sortNameToolStripButton.Name = "sortNameToolStripButton"
sortNameToolStripButton.Text = "Name"
Dim sortMonthToolStripButton = New System.Windows.Forms.ToolStripButton
sortMonthToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text
sortMonthToolStripButton.Name = "sortMonthToolStripButton"
sortMonthToolStripButton.Text = "Month"
CType(PrintPreviewDialog.Controls(1), ToolStrip).Items.Add(CType(sortNameToolStripButton, ToolStripButton))
CType(PrintPreviewDialog.Controls(1), ToolStrip).Items.Add(CType(sortMonthToolStripButton, ToolStripButton))
AddHandler pd.PrintPage, AddressOf pd_PrintPage
AddHandler sortNameToolStripButton.Click, AddressOf sortNameToolStripButton_Click
AddHandler sortMonthToolStripButton.Click, AddressOf sortMonthToolStripButton_Click
GetData() 'SQL query to retrieve report data and put it into a DataView
SortData() 'Apply the Sort property to the DataView
Sort = SortEnum.Name
PrintPreviewDialog.Document = pd
PrintPreviewDialog.ShowDialog()
pd.Dispose()
PrintPreviewDialog.Dispose()
End Sub

Private Sub sortNameToolStripButton_Click(sender As Object, e As EventArgs)
Sort = SortEnum.Name
SortData() 'Apply the Sort property to the DataView
PrintPreviewDialog.Refresh 'Doesn't cause the PrintDocument to get reprinted with the newly sorted data
End Sub

Private Sub sortMonthToolStripButton_Click(sender As Object, e As EventArgs)
Sort = SortEnum.Month
SortData() 'Apply the Sort property to the DataView
pd.Print() 'Causes the newly sorted report to be output on the printer
End Sub

Private Sub pd_PrintPage(sender As Object, e As PrintPageEventArgs)
PreparePage(e, sender, xPos, yPos)
xPos = LeftMargin
yPos = TopMargin
While x < View.Count
PrintText(View(x)("Name"), xPos, yPos, hw, LeftJustified, n11, e)
PrintText(FormatDateTime(View(x)("Date"), DateFormat.LongDate), xPos + hw + 10, yPos, qw, LeftJustified, n11, e)
x += 1
End While
End Sub

Continue reading...
 
Back
Top