Ausdrucken DataGridView

  • WPF

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von mmkeys.

    Ausdrucken DataGridView

    Hallo zusammen,

    wie der Titel schon sagt, möchte ich per VB-Code den Inhalt einer Datagridview ausdrucken.
    Natürlich habe ich schon einiges an Internetrecherche hinter mir und bin auf einen Code gestoßen, der wirklich ordentlich funktioniert:

    Aufruf der Klasse:

    VB.NET-Quellcode

    1. Dim PrintDGV As New clsPrintDatagridview(DataGridView1)
    2. PrintDGV.Print()

    Eigentliche Klasse:

    VB.NET-Quellcode

    1. 'Klaus D. Raudszus / VB 2005 / November 2006
    2. Public Class clsDGVPrint
    3. Private m_Pages As Integer
    4. Private m_intTotalWidth As Integer
    5. Private m_intPageHeight As Integer
    6. Private m_intRowsToPrint As Integer
    7. Private m_arColsWidth() As Integer
    8. Private m_arHeaderText() As String
    9. Private m_ftHeaderFont As Font
    10. Private m_DataGridView As DataGridView
    11. Private WithEvents m_PrintDoc As Printing.PrintDocument
    12. Public Sub New()
    13. Me.m_Pages = 0
    14. Me.m_intTotalWidth = 0
    15. Me.m_intPageHeight = 0
    16. Me.m_intRowsToPrint = 0
    17. Me.m_ftHeaderFont = New Font("Arial", 9.75!)
    18. End Sub
    19. Public Sub New(ByVal dgv As DataGridView)
    20. Me.New()
    21. Me.m_DataGridView = dgv
    22. ReDim Me.m_arColsWidth(Me.m_DataGridView.ColumnCount - 1)
    23. ReDim Me.m_arHeaderText(Me.m_DataGridView.ColumnCount - 1)
    24. End Sub
    25. Public Sub Print()
    26. Me.m_PrintDoc = New Printing.PrintDocument
    27. AddHandler m_PrintDoc.EndPrint, AddressOf Me.EndPrint
    28. Dim intCols As Integer = Me.m_DataGridView.ColumnCount - 1
    29. For i As Integer = 0 To intCols
    30. Me.m_arColsWidth(i) = GetColumnsSize(i)
    31. Me.m_arHeaderText(i) = GetHeaderText(i)
    32. Me.m_intTotalWidth = Me.m_intTotalWidth + Me.m_arColsWidth(i)
    33. Next
    34. If Me.m_intTotalWidth > Me.m_PrintDoc.DefaultPageSettings.PaperSize.Width Then
    35. Me.m_PrintDoc.DefaultPageSettings.Landscape = True
    36. Me.m_PrintDoc.DefaultPageSettings.Margins.Top = 35
    37. Me.m_intPageHeight = Me.m_PrintDoc.DefaultPageSettings.PaperSize.Width
    38. Else
    39. Me.m_intPageHeight = Me.m_PrintDoc.DefaultPageSettings.PaperSize.Height
    40. End If
    41. Dim ppd As PrintPreviewDialog = New PrintPreviewDialog()
    42. ppd.Document = Me.m_PrintDoc
    43. ppd.ShowDialog()
    44. End Sub
    45. Private Sub m_PrintDoc_PrintPage(ByVal sender As Object, _
    46. ByVal e As Printing.PrintPageEventArgs) Handles m_PrintDoc.PrintPage
    47. Me.PrintHeader(e.Graphics)
    48. Me.PrintRows(e)
    49. 'Seitenzahl drucken
    50. Dim str As String = "Seite " & Me.m_Pages.ToString
    51. e.Graphics.DrawString(str, _
    52. Me.m_DataGridView.Font, Brushes.Blue, _
    53. Me.m_PrintDoc.DefaultPageSettings.Margins.Left, Me.m_intPageHeight - 75)
    54. End Sub
    55. Private Sub PrintHeader(ByVal g As Graphics)
    56. Dim x As Integer = Me.m_PrintDoc.DefaultPageSettings.Margins.Left
    57. Dim y As Integer = Me.m_PrintDoc.DefaultPageSettings.Margins.Top
    58. For i As Integer = 0 To Me.m_arHeaderText.Length - 1
    59. g.DrawString(Me.m_arHeaderText(i), Me.m_ftHeaderFont, Brushes.Blue, x, y)
    60. x += Me.m_arColsWidth(i)
    61. Next
    62. End Sub
    63. Private Sub PrintRows(ByVal e As Printing.PrintPageEventArgs)
    64. Dim intCols As Integer = Me.m_DataGridView.ColumnCount - 1
    65. Dim intRows As Integer = Me.m_DataGridView.RowCount - 2
    66. Dim x As Integer = Me.m_PrintDoc.DefaultPageSettings.Margins.Left
    67. Dim y As Integer = Me.m_PrintDoc.DefaultPageSettings.Margins.Top + 50
    68. Me.m_Pages += 1
    69. For i As Integer = Me.m_intRowsToPrint To intRows
    70. For ii As Integer = 0 To intCols
    71. Dim str As String = Me.m_DataGridView.Rows(i).Cells(ii).Value.ToString
    72. e.Graphics.DrawString(str, Me.m_DataGridView.Font, Brushes.Black, x, y)
    73. x += Me.m_arColsWidth(ii)
    74. Next
    75. x = Me.m_PrintDoc.DefaultPageSettings.Margins.Left
    76. y += 30
    77. If y > (Me.m_intPageHeight - _
    78. Me.m_PrintDoc.DefaultPageSettings.Margins.Bottom) Then
    79. e.HasMorePages = True
    80. Me.m_intRowsToPrint = i + 1
    81. Exit Sub
    82. Else
    83. e.HasMorePages = False
    84. Me.m_intRowsToPrint = 0
    85. End If
    86. Next
    87. End Sub
    88. Private Function GetHeaderText(ByVal col As Integer) As String
    89. Return Me.m_DataGridView.Columns(col).HeaderText
    90. End Function
    91. Private Function GetColumnsSize(ByVal col As Integer) As Integer
    92. Dim sglWidth As Single = 0
    93. Dim intRows As Integer = Me.m_DataGridView.RowCount - 2
    94. Using g As Graphics = Graphics.FromHwnd(Me.m_DataGridView.Handle)
    95. Dim newSizeF As SizeF = g.MeasureString(GetHeaderText(col), Me.m_ftHeaderFont)
    96. sglWidth = newSizeF.Width + 15
    97. For i As Integer = 0 To intRows
    98. newSizeF = g.MeasureString( _
    99. Me.m_DataGridView.Rows(i).Cells(col).Value.ToString, _
    100. Me.m_DataGridView.Font)
    101. If newSizeF.Width > sglWidth Then sglWidth = newSizeF.Width + 15
    102. Next
    103. End Using
    104. Return CInt(sglWidth)
    105. End Function
    106. Private Sub EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs)
    107. Me.m_Pages = 0
    108. End Sub
    109. End Class

    Quelle: vb-fun.de/cgi-bin/forumarchiv.…eseite&nummer=25377#Atext

    Nun die Fragestellung:
    - Übergebe ich der Klasse zu viele Spalten, wird keine neue Seite für den Ausdruck erzeugt, sondern die Schrift läuft quasi aus dem Blatt heraus.
    - Es werden keine Gitternetzlinien angezeigt
    Wie lässt sich der Code dahingehend optimieren?

    Vielen Dank schonmal!
    mmkeys