本文介绍了如何打印Datagridview在VB中有一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Datagrid,其中包含从数据库检索的信息,我希望打印输出采用行和列的表格格式。我的实际方法很简单,但输出却非常混乱。有什么想法吗? Private Sub Imprimir_Click(发件人作为对象,作为EventArgs)处理Imprimir.Click
PrintPreviewDialog1.PrintPreviewControl.Zoom = 1.0
PrintPreviewDialog1.FindForm.WindowState = FormWindowState.Maximized
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object,ByVal e作为
System.Drawing.Printing.PrintPageEventArgs)处理PrintDocument1.PrintPage
Dim y As Integer = 70
PrintDocument1.DefaultPageSettings.Landscape = True
e.Graphics.DrawString(TransporGest - Registo deOperações,
新字体(Verdana,10,FontStyle.Bold),Brushes.Black,30,30)
对于每个dr作为DataGridViewRow在dg.Rows
e .Graphics.DrawString(dr.Cells(0).Value&|& dr.Cells(2).Value&
|& dr.Cells(3).Value& |& dr.Cells(4).Value&|&
dr.Cells(6).Value& |& dr.Cells(7).Value& |&
dr.Cells(9).Value& |& dr.Cells(11).Value& |&
dr.Cells(12).Value,New Font(Verdana,10),Brushes.Black,30,y)
y + = 20
下一个
End Sub
结束类
解决方案
添加到表单(设计) Button1
, PrintDocument1
, PrintPreviewDialog1
,您的 - > DataGridView1
并粘贴代码:
Dim mRow As Integer = 0
Dim newpage As Boolean = True
Private Sub PrintDocument1_PrintPage(sender As System.Object,e As System.Drawing.Printing.PrintPageEventArgs)处理PrintDocument1.PrintPage
使用DataGridView1
Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
fmt.LineAlignment = StringAlignment.Center
fmt.Trimming = StringTrimming.EllipsisCharacter
Dim y As Single = e.MarginBounds.Top
Do While mRow< .RowCount
Dim row As DataGridViewRow = .Rows(mRow)
Dim x As Single = e.MarginBounds.Left
Dim h As Single = 0
对于每个单元格作为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然后
e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText,.Font,Brushes.Black,rc, fmt)
Else
e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(),.Font,Brushes.Black,rc,fmt)
End If
x + = rc.Width
h = Math.Max(h,rc.Height)
下一个
newpage = False
y + = h
mRow + = 1
如果y + h> e.MarginBounds.Bottom然后
e.HasMorePages = True
mRow - = 1
newpage = True
退出Sub
结束如果
循环
mRow = 0
结束
End Sub
私有子按钮1_Click(发件人as System.Object,e As System.EventArgs)处理Button3.Click
PrintPreviewDialog1。 Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub
I have a Datagrid with information retrieved from a database and i would like the print output to be in table format with lines and columns. My actual methode is simple but the output is very confusing. Any thoughts?
Private Sub Imprimir_Click(sender As Object, e As EventArgs) Handles Imprimir.Click
PrintPreviewDialog1.PrintPreviewControl.Zoom = 1.0
PrintPreviewDialog1.FindForm.WindowState = FormWindowState.Maximized
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim y As Integer = 70
PrintDocument1.DefaultPageSettings.Landscape = True
e.Graphics.DrawString("TransporGest - Registo de Operações",
New Font("Verdana", 10, FontStyle.Bold), Brushes.Black, 30, 30)
For Each dr As DataGridViewRow In dg.Rows
e.Graphics.DrawString(dr.Cells(0).Value & " | " & dr.Cells(2).Value &
" | " & dr.Cells(3).Value & " | " & dr.Cells(4).Value & " | " &
dr.Cells(6).Value & " | " & dr.Cells(7).Value & " | " &
dr.Cells(9).Value & " | " & dr.Cells(11).Value & " | " &
dr.Cells(12).Value, New Font("Verdana", 10), Brushes.Black, 30, y)
y += 20
Next
End Sub
End Class
解决方案
Add to Form(Design) Button1
, PrintDocument1
,PrintPreviewDialog1
, your -> DataGridView1
and paste the code:
Dim mRow As Integer = 0
Dim newpage As Boolean = True
Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
With DataGridView1
Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
fmt.LineAlignment = StringAlignment.Center
fmt.Trimming = StringTrimming.EllipsisCharacter
Dim y As Single = e.MarginBounds.Top
Do While mRow < .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(DataGridView1.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
Else
e.Graphics.DrawString(DataGridView1.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 -= 1
newpage = True
Exit Sub
End If
Loop
mRow = 0
End With
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub
这篇关于如何打印Datagridview在VB中有一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!