vb.net 如何在VB中打印Datagridview有一个表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24654790/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:49:50  来源:igfitidea点击:

How to print Datagridview has a table in VB

mysqlvb.netprintingdatagrid

提问by A.Barbosa

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?

我有一个包含从数据库中检索到的信息的 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

回答by ooopsoft

Add to Form(Design) Button1, PrintDocument1,PrintPreviewDialog1, your -> DataGridView1

添加到 Form(Design) Button1, PrintDocument1, PrintPreviewDialog1, 你的 ->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

回答by ooopsoft

For pecific Columns (example column 1,3,4)

对于特定列(例如第 1、3、4 列)

Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim custCells As Integer() = {1, 3, 4}
    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 Integer In custCells
                Dim rc As RectangleF = New RectangleF(x, y, row.Cells(cell).Size.Width, row.Cells(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).HeaderText, .Font, Brushes.Black, rc, fmt)
                Else
                    e.Graphics.DrawString(DataGridView1.Rows(row.Cells(cell).RowIndex).Cells(cell).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

回答by rheitzman

You may find that moving the data to Excel/Word would be as useful:

您可能会发现将数据移动到 Excel/Word 也同样有用:

    Private Sub tsbtnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnCopy.Click
    dgv01.SuspendLayout()
    dgv01.RowHeadersVisible = False
    If dgv01.SelectedRows.Count = 0 Then dgv01.SelectAll()
    Clipboard.SetDataObject(dgv01.GetClipboardContent())
    dgv01.ClearSelection()
    dgv01.RowHeadersVisible = True
    dgv01.ResumeLayout()
End Sub

Or have the user select all (click upper left cell) to copy/Paste.

或者让用户全选(单击左上角单元格)进行复制/粘贴。