vb.net 以横向格式打印和预览 DataGridView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15786955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print and Preview DataGridView in Landscape format
提问by Rara Arar
Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click
preview.PrintPreviewControl.Zoom = 1
preview.Document = print
print.PrinterSettings.DefaultPageSettings.Landscape = True
preview.Show()
AddHandler print.PrintPage, AddressOf print_PrintPage
End Sub
Protected Sub print_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
Dim ColumnCount As Integer = DataGridView1.ColumnCount
Dim RowCount As Integer = DataGridView1.RowCount
Dim CellTopPos As Integer = print.PrinterSettings.DefaultPageSettings.Margins.Top
For Row = 0 To RowCount - 1
Dim CellLeftPos As Integer = print.PrinterSettings.DefaultPageSettings.Margins.Left
For Cell = 0 To ColumnCount - 1
Dim CellValue As String = DataGridView1.Rows(Row).Cells(Cell).Value.ToString()
Dim CellWidth = DataGridView1.Rows(Row).Cells(Cell).Size.Width + 50
Dim CellHeight = DataGridView1.Rows(Row).Cells(Cell).Size.Height
Dim Brush As New SolidBrush(Color.Black)
e.Graphics.DrawString(CellValue, New Font("Century Gothic", 10), Brush, CellLeftPos, CellTopPos)
e.Graphics.DrawRectangle(Pens.Black, CellLeftPos, CellTopPos, CellWidth, CellHeight)
CellLeftPos += CellWidth
Next
CellTopPos += DataGridView1.Rows(Row).Cells(0).Size.Height
Next
End Sub
This is my code for previewing and printing the contents of my datagridview. I tried using the DefaultPageSettings code but it doesn't work. Im also trying to print it in centered format
这是我用于预览和打印 datagridview 内容的代码。我尝试使用 DefaultPageSettings 代码,但它不起作用。我也试图以居中的格式打印它
采纳答案by LarsTech
Try setting the DefaultPageSettings property of the PrintDocument, not PrinterSettings:
尝试设置 PrintDocument 的 DefaultPageSettings 属性,而不是 PrinterSettings:
'PrintDocument1.PrinterSettings.DefaultPageSettings.Landscape = True
PrintDocument1.DefaultPageSettings.Landscape = True
回答by zidane
Can be used like this CODE
可以像这样使用代码
Dim preview As PrintPreviewDialog
Dim PageSetup As PageSetupDialog
PageSetup = New PageSetupDialog
preview = New PrintPreviewDialog
PageSetup.PageSettings = PrintDocument1.DefaultPageSettings
PageSetup.ShowDialog()
preview.PrintPreviewControl.Zoom = 1
preview.Document = PrintDocument1
preview.ShowDialog()