我能够在datagridview上打印数据,并且打印没有问题。现在,我想将标题文本或图像添加到datagridview的打印输出中。我无法弄清楚如何做到这一点。我附上了用于打印datagridview的代码。
私有位图As Bitmap Private Sub btnprintreport_Click(sender As Object,e As EventArgs)处理btnprintreport.Click
Try
Dim height As Integer = reportsellinggridview.Height
reportsellinggridview.Height = reportsellinggridview.RowCount * reportsellinggridview.RowTemplate.Height
'Create a Bitmap and draw the DataGridView on it.
bitmap = New Bitmap(Me.reportsellinggridview.Width, Me.reportsellinggridview.Height)
reportsellinggridview.DrawToBitmap(bitmap, New Rectangle(0, 0, Me.reportsellinggridview.Width, Me.reportsellinggridview.Height))
'Resize DataGridView back to original height.
reportsellinggridview.Height = height
PrintDocument1.DefaultPageSettings.Landscape = True
'Show the Print Preview Dialog.
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.PrintPreviewControl.Zoom = 1
PrintPreviewDialog1.ShowDialog()
Catch ex As Exception
MessageBox.Show("Could not print, please turn on the printer and make sure it is set as default printer", "print error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'Print the contents.
e.Graphics.DrawImage(bitmap, 0, 0)
End Sub