mysql - 如何在VB中打印Datagridview有表

标签 mysql vb.net printing datagrid

我有一个数据网格,其中包含从数据库中检索到的信息,我希望打印输出采用包含行和列的表格格式。我的实际方法很简单,但输出非常困惑。有什么想法吗?

  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

最佳答案

添加到表单(设计)Button1PrintDocument1PrintPreviewDialog1,您的 -> 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) 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

关于mysql - 如何在VB中打印Datagridview有表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24654790/

相关文章:

graphics - 打印和图形[]

Python 2.7.8 : Printing a sub-dictionary value?

mysql - SQL 密集索引和稀疏索引

mysql - 检查 select 语句是否返回带有 IF 的内容

VB.NET:有没有办法让Nothing默认为数字类型的0?

c# - 从 JavaScript 调用 ASP 函数

c# - .NET 中命名空间可以包含的类的数量是否有任何限制?

java - 仅打印 JTable 中带有 Logo 的填充数据

php - 将 MySQL 查询放在 try{ } 之外是否安全

mysql - 是否可以将这 4 个查询合并为一个更高效的查询?