2014-07-09 44 views
1

我有一個Datagrid,其中包含從數據庫中檢索的信息,我希望打印輸出爲帶有行和列的表格格式。我的實際方法很簡單,但輸出很混亂。有什麼想法嗎?如何打印Datagridview在VB中有一個表格

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 
+0

您可以打印ooopsoft的工作方式,但在我看來,reportviewer在datagridview打印方面是一個更容易的選擇,但這只是我的看法。 – user3758070

+0

我已經用水晶報告了我的報告!超級簡單!不過,我需要以某種形式使用我的sql查詢。有什麼辦法可以改變VB上的水晶報表查詢嗎? –

+0

我最近與reportviewer很多,所以讓我知道如果你有任何更多的問題:) – user3758070

回答

5

添加至表單(設計)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 
+0

非常感謝!你不知道你幫了我多少幫助! –

+0

但是,我仍然有一個疑問,我如何選擇特定行來打印而不是全部打印? –

+0

上面的代碼成功地打印整個datagridview,爲一行快速,但不是如此優雅,你可以: a)註釋行'Do While mRow <.RowCount' b)註釋行'循環' c)設置爲行(4)例如:'Dim mRow As Integer = 4' d)設置'Dim newpage As Boolean = False' – ooopsoft

2

對於pecific列(例如列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 
1

您可能會發現移動數據到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 

或有用戶選擇所有(點擊左上角單元格)複製/粘貼。