vb.net 打印数据网格视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37784788/
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
Printing a datagridview
提问by
I am trying to print a datagridviewwhich will get values from the database, I was able to do that but I don't know why the columns name are not showing up after I print it. Below is the code that I am currently using:
我正在尝试打印一个datagridview将从数据库中获取值的值,我能够做到这一点,但我不知道为什么打印后列名没有显示出来。下面是我目前使用的代码:
This one is for the printdocument:
这是用于打印文档的:
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
With DataGridView3
Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
Dim text As String = ("TrashCollection - Estatísticas")
fmt.LineAlignment = StringAlignment.Center
fmt.Trimming = StringTrimming.EllipsisCharacter
e.Graphics.DrawString(text, New Font("Verdana", 10, FontStyle.Bold), Brushes.Black, 350, 30)
Dim y As Single = e.MarginBounds.Top
Do While mRow < DataGridView3.RowCount
Dim row As DataGridViewRow = .Rows(mRow)
Dim x As Single = e.MarginBounds.Left
Dim h As Single = 0
For Each cell As DataGridViewCell In row.Cells
Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
If (newpage) Then
e.Graphics.DrawString(DataGridView3.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
Else
e.Graphics.DrawString(DataGridView3.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
End If
x += rc.Width
h = Math.Max(h, rc.Height)
Next
newpage = False
y += h
mRow += 1
If y + h > e.MarginBounds.Bottom Then
e.HasMorePages = True
mRow -= 0
newpage = True
Exit Sub
End If
Loop
mRow = 0
End With
End Sub
And then into the print button I have this lines of code:
然后进入打印按钮,我有这几行代码:
Private Sub cmdPrintDetailed_Click(sender As Object, e As EventArgs) Handles cmdPrintDetailed.Click
PrintDocument1.DefaultPageSettings.Landscape = True
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub
So, basically I would like to be able to print the columns name too instead of just the rows. Almost forget, in the print preview I am able to see all the the datagridviewwith the columns.
所以,基本上我也希望能够打印列名而不仅仅是行。几乎忘记了,在打印预览中我可以看到所有datagridview的列。
回答by minimalist
This is the best printing example I found and I use it modified for my datagrid printing. May be this will help you asit addesses also column headers. https://code.msdn.microsoft.com/VBNet-Printing-Example-bc3b0176
这是我找到的最好的打印示例,我使用它修改了我的数据网格打印。可能这会帮助您添加列标题。 https://code.msdn.microsoft.com/VBNet-Printing-Example-bc3b0176

