我正在使用VB.Net 2008应用程序。
我正在使用DataGridView。 我有一个打印选项,在这里我需要打印DataGridView。
我将此代码用于“打印”和“打印预览”。
Dim MyDataGridViewPrinter As DataGridViewPrinter
Private Function SetupThePrinting() As Boolean
Dim MyPrintDialog As PrintDialog = New PrintDialog()
MyPrintDialog.AllowCurrentPage = False
MyPrintDialog.AllowPrintToFile = False
MyPrintDialog.AllowSelection = False
MyPrintDialog.AllowSomePages = True
MyPrintDialog.PrintToFile = False
MyPrintDialog.ShowHelp = False
MyPrintDialog.ShowNetwork = False
PrintDocument1.PrinterSettings = MyPrintDialog.PrinterSettings
PrintDocument1.DefaultPageSettings.Margins = New Margins(10, 10, 10, 10)
MyDataGridViewPrinter = New DataGridViewPrinter(DataGridView1, PrintDocument1, False, True, "Price Manager", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
Return True
End Function
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim more As Boolean
Try
more = MyDataGridViewPrinter.DrawDataGridView(e.Graphics)
If more Then e.HasMorePages = True
Catch Ex As Exception
End Try
End Sub
' The Print Preview Button
Private Sub btnPrintPreview_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
If SetupThePrinting() Then
Dim MyPrintPreviewDialog As PrintPreviewDialog = New PrintPreviewDialog()
MyPrintPreviewDialog.Document = PrintDocument1
MyPrintPreviewDialog.ShowDialog()
End If
End Sub
' The Print Button
Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
If SetupThePrinting() Then PrintDocument1.Print()
End Sub
附加了我用于打印的课程。
我有10个字段显示在datagridview中。 由于最后5列的标题很长,因此datagridview不适合放在1页内(不在一个页面中显示所有10个字段)。 但是有没有办法我可以“文本换行”顶部标题列并锁定宽度,然后它应该完美地打印在一页上。
我搜索了很多,但找不到。
如果您知道如何“顶部文字”换行并锁定宽度以在一页上打印,请帮助我。 如果您可以提供示例,那将非常有帮助。
提前致谢。