2 DGVs auf einem DIN A4 Blatt drucken

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von INOPIAE.

    2 DGVs auf einem DIN A4 Blatt drucken

    Hallo ;)

    Ich habe auf meinem Form 2DGV´s welche ich beide auf einer DIN A4 Seite (Querformat) ausdrucken möchte.
    Bitte schaut mal auf das Bild, ich komme nicht weiter, damit die DGVs füllend und sauber auf dem Blatt platziert und gedruckt werden.
    So wie jetzt ist das ja kein schöner Ausdruck. ;(

    Wenn das auch anders geht...

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private Sub PrintDataGridViews(ByVal sender As Object, ByVal e As PrintPageEventArgs)
    2. Dim titleFont As New Font("Arial", 14, FontStyle.Bold)
    3. Dim headerFont As New Font("Arial", 8, FontStyle.Regular)
    4. Dim normalFont As New Font("Arial", 11, FontStyle.Regular)
    5. Dim dgv1Width As Integer = 0
    6. Dim dgv2Width As Integer = 0
    7. Dim dgv1Height As Integer = 0
    8. Dim dgv2Height As Integer = 0
    9. Dim startX As Integer = e.MarginBounds.Left
    10. Dim startY As Integer = e.MarginBounds.Top
    11. Dim titleHeight As Integer = CInt(titleFont.GetHeight())
    12. Dim headerHeight As Integer = CInt(headerFont.GetHeight())
    13. Dim rowHeight As Integer = CInt(normalFont.GetHeight())
    14. Dim currentY As Integer = startY
    15. Dim cellBounds As Rectangle
    16. Dim headerBounds As Rectangle
    17. Dim dgv1Bounds As Rectangle
    18. Dim dgv2Bounds As Rectangle
    19. Dim dgv1ColumnCount As Integer = DGV_1.ColumnCount
    20. Dim dgv2ColumnCount As Integer = DGV_2.ColumnCount
    21. Dim totalRows1 As Integer
    22. Dim totalRows2 As Integer
    23. Dim currentRow1 As Integer = 0
    24. Dim currentRow2 As Integer = 0
    25. Dim horizontalSpacing As Integer = 50
    26. ' Calculate the width and height of the DataGridViews
    27. For i As Integer = 0 To dgv1ColumnCount - 1
    28. dgv1Width += DGV_1.Columns(i).Width
    29. Next
    30. For i As Integer = 0 To dgv2ColumnCount - 1
    31. dgv2Width += DGV_2.Columns(i).Width
    32. Next
    33. totalRows1 = DGV_1.RowCount - 1
    34. totalRows2 = DGV_2.RowCount - 1
    35. dgv1Height = (totalRows1 + 1) * rowHeight
    36. dgv2Height = (totalRows2 + 1) * rowHeight
    37. ' Draw the title
    38. Dim title As String = "Wetterdaten Jahresabschluss"
    39. e.Graphics.DrawString(title, titleFont, Brushes.Blue, startX, startY - 40)
    40. currentY += titleHeight
    41. ' Draw the first DataGridView
    42. If dgv1Height > e.MarginBounds.Height - titleHeight Then
    43. dgv1Height = e.MarginBounds.Height - titleHeight
    44. End If
    45. dgv1Bounds = New Rectangle(startX, currentY, dgv1Width, dgv1Height)
    46. headerBounds = dgv1Bounds
    47. headerBounds.Height = headerHeight
    48. e.Graphics.FillRectangle(Brushes.LightGray, headerBounds)
    49. e.Graphics.DrawRectangle(Pens.Black, dgv1Bounds)
    50. currentY += headerHeight
    51. For i As Integer = 0 To dgv1ColumnCount - 1
    52. cellBounds = New Rectangle(startX, currentY, DGV_1.Columns(i).Width, headerHeight)
    53. e.Graphics.DrawString(DGV_1.Columns(i).HeaderText, headerFont, Brushes.Black, cellBounds)
    54. startX += DGV_1.Columns(i).Width
    55. Next
    56. currentY += rowHeight
    57. For i As Integer = currentRow1 To totalRows1
    58. If currentY + rowHeight > e.MarginBounds.Height + e.MarginBounds.Top Then
    59. e.HasMorePages = True
    60. currentRow1 = i
    61. startX = e.MarginBounds.Left
    62. currentY = e.MarginBounds.Top
    63. Exit Sub
    64. End If
    65. startX = dgv1Bounds.Left
    66. For j As Integer = 0 To dgv1ColumnCount - 1
    67. cellBounds = New Rectangle(startX, currentY, DGV_1.Columns(j).Width, rowHeight)
    68. e.Graphics.DrawString(DGV_1.Rows(i).Cells(j).FormattedValue.ToString(), normalFont, Brushes.Black, cellBounds)
    69. startX += DGV_1.Columns(j).Width
    70. Next
    71. startX = e.MarginBounds.Left
    72. currentY += rowHeight
    73. Next
    74. ' Draw the second DataGridView
    75. startY += dgv1Height + headerHeight + horizontalSpacing
    76. If dgv2Height > e.MarginBounds.Height - titleHeight - dgv1Height - headerHeight Then
    77. dgv2Height = e.MarginBounds.Height - titleHeight - dgv1Height - headerHeight
    78. End If
    79. dgv2Bounds = New Rectangle(startX, startY, dgv2Width, dgv2Height)
    80. headerBounds = dgv2Bounds
    81. headerBounds.Height = headerHeight
    82. e.Graphics.FillRectangle(Brushes.LightGray, headerBounds)
    83. e.Graphics.DrawRectangle(Pens.Black, dgv2Bounds)
    84. currentY += headerHeight
    85. For i As Integer = 0 To dgv2ColumnCount - 1
    86. cellBounds = New Rectangle(startX, startY, DGV_2.Columns(i).Width, headerHeight)
    87. e.Graphics.DrawString(DGV_2.Columns(i).HeaderText, headerFont, Brushes.Black, cellBounds)
    88. startX += DGV_2.Columns(i).Width
    89. Next
    90. currentY += rowHeight
    91. For i As Integer = currentRow2 To totalRows2
    92. If currentY + rowHeight > e.MarginBounds.Height + e.MarginBounds.Top Then
    93. e.HasMorePages = True
    94. currentRow2 = i
    95. startX = e.MarginBounds.Left
    96. currentY = e.MarginBounds.Top
    97. Exit Sub
    98. End If
    99. startX = dgv2Bounds.Left
    100. For j As Integer = 0 To dgv2ColumnCount - 1
    101. cellBounds = New Rectangle(startX, currentY, DGV_2.Columns(j).Width, rowHeight)
    102. e.Graphics.DrawString(DGV_2.Rows(i).Cells(j).FormattedValue.ToString(), normalFont, Brushes.Black, cellBounds)
    103. startX += DGV_2.Columns(j).Width
    104. Next
    105. startX = e.MarginBounds.Left
    106. currentY += rowHeight
    107. Next
    108. End Sub

    VB.NET-Quellcode

    1. Private Sub btn_Charts_Click(sender As Object, e As EventArgs) Handles btn_Charts.Click
    2. Dim pd As New PrintDocument()
    3. Dim ps As New PaperSize("A4 Landscape", 827, 1169 + pd.DefaultPageSettings.Margins.Top + pd.DefaultPageSettings.Margins.Bottom)
    4. ps.PaperName = CStr(PaperKind.A4)
    5. pd.DefaultPageSettings.Landscape = True
    6. pd.DefaultPageSettings.PaperSize = ps
    7. AddHandler pd.PrintPage, AddressOf PrintDataGridViews
    8. pd.Print()
    9. End Sub

    Bilder
    • 2dgvs-1.jpg

      149,08 kB, 800×510, 82 mal angesehen
    Asperger Autistin. Brauche immer etwas um gewisse Sachen zu verstehen. :huh:
    Hallo Amelie

    Ich hätte da einen Vorschlag: Drucke doch statt das 2. DGV einfach eine Zeile mehr beim 1. DGV. Die Abmessungen nimmst du vom 1. DGV die Werte vom 2.DGV. In der 1. Spalte schreibst du z.B. 01 - 12.2023 oder Jahreswerte. Evtl. könntest Du ja die Zeile noch Fett drucken.

    Ich denke, das wäre relativ einfach umzusetzen.

    LG Panter

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private Sub PrintDataGridViews(ByVal sender As Object, ByVal e As PrintPageEventArgs)
    2. Dim titleFont As New Font("Arial", 14, FontStyle.Bold)
    3. Dim headerFont As New Font("Arial", 8, FontStyle.Regular)
    4. Dim normalFont As New Font("Arial", 11, FontStyle.Regular)
    5. Dim dgv1Width As Integer = 0
    6. Dim dgv1Height As Integer = 0
    7. Dim startX As Integer = e.MarginBounds.Left
    8. Dim startY As Integer = e.MarginBounds.Top
    9. Dim titleHeight As Integer = CInt(titleFont.GetHeight())
    10. Dim headerHeight As Integer = CInt(headerFont.GetHeight())
    11. Dim rowHeight As Integer = CInt(normalFont.GetHeight())
    12. Dim currentY As Integer = startY
    13. Dim cellBounds As Rectangle
    14. Dim headerBounds As Rectangle
    15. Dim dgv1Bounds As Rectangle
    16. Dim dgv1ColumnCount As Integer = DGV_1.ColumnCount
    17. Dim totalRows1 As Integer
    18. Dim currentRow1 As Integer = 0
    19. Dim horizontalSpacing As Integer = 50
    20. ' Calculate the width and height of the DataGridViews
    21. For i As Integer = 0 To dgv1ColumnCount - 1
    22. dgv1Width += DGV_1.Columns(i).Width
    23. Next
    24. totalRows1 = DGV_1.RowCount - 1
    25. dgv1Height = (totalRows1 + 3) * rowHeight
    26. ' Draw the title
    27. Dim title As String = "Wetterdaten Jahresabschluss"
    28. e.Graphics.DrawString(title, titleFont, Brushes.Blue, startX, startY - 40)
    29. currentY += titleHeight
    30. ' Draw the first DataGridView
    31. If dgv1Height > e.MarginBounds.Height - titleHeight Then
    32. dgv1Height = e.MarginBounds.Height - titleHeight
    33. End If
    34. dgv1Bounds = New Rectangle(startX, currentY, dgv1Width, dgv1Height)
    35. headerBounds = dgv1Bounds
    36. headerBounds.Height = headerHeight
    37. e.Graphics.FillRectangle(Brushes.LightGray, headerBounds)
    38. e.Graphics.DrawRectangle(Pens.Black, dgv1Bounds)
    39. 'currentY += headerHeight
    40. For i As Integer = 0 To dgv1ColumnCount - 1
    41. cellBounds = New Rectangle(startX, currentY, DGV_1.Columns(i).Width, headerHeight)
    42. e.Graphics.DrawString(DGV_1.Columns(i).HeaderText, headerFont, Brushes.Black, cellBounds)
    43. startX += DGV_1.Columns(i).Width
    44. Next
    45. currentY += rowHeight
    46. For i As Integer = currentRow1 To totalRows1
    47. If currentY + rowHeight > e.MarginBounds.Height + e.MarginBounds.Top Then
    48. e.HasMorePages = True
    49. currentRow1 = i
    50. startX = e.MarginBounds.Left
    51. currentY = e.MarginBounds.Top
    52. Exit Sub
    53. End If
    54. startX = dgv1Bounds.Left
    55. For j As Integer = 0 To dgv1ColumnCount - 1
    56. cellBounds = New Rectangle(startX, currentY, DGV_1.Columns(j).Width, rowHeight)
    57. e.Graphics.DrawString(DGV_1.Rows(i).Cells(j).FormattedValue.ToString(), normalFont, Brushes.Black, cellBounds)
    58. startX += DGV_1.Columns(j).Width
    59. Next
    60. startX = e.MarginBounds.Left
    61. currentY += rowHeight
    62. Next
    63. ' Draw the second DataGridView
    64. startX = dgv1Bounds.Left
    65. For j As Integer = 0 To dgv1ColumnCount - 1
    66. cellBounds = New Rectangle(startX, currentY, DGV_1.Columns(j).Width, rowHeight)
    67. e.Graphics.DrawString(DGV_2.Rows(0).Cells(j).FormattedValue.ToString(), normalFont, Brushes.Black, cellBounds)
    68. startX += DGV_1.Columns(j).Width
    69. Next
    70. End Sub


    Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von „Panter“ ()

    @Panter

    Ich habe es nun soweit hinbekommen siehe BILD und sehe erst jetzt das Du Code gepostet hast.
    Werde dein Code mal ausprobieren.
    DANKE



    @Panter

    Ich habe deinen Code nun einmal etwas angepasst und nun schaut es doch recht gut aus.
    Siehe Bildanhang

    Insgesamt ist dein Code dann noch etwas kürzer als meiner mit den 2DGV.
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private Sub PrintDataGridViews(ByVal sender As Object, ByVal e As PrintPageEventArgs)
    2. Dim titleFont As New Font("Arial", 14, FontStyle.Bold)
    3. Dim titleFont2 As New Font("Arial", 12, FontStyle.Bold)
    4. Dim headerFont As New Font("Arial", 10.5, FontStyle.Regular)
    5. Dim normalFont As New Font("Arial", 12, FontStyle.Regular)
    6. Dim normalFont2 As New Font("Arial", 12, FontStyle.Bold)
    7. Dim dgv1Width As Integer = 0
    8. Dim dgv1Height As Integer = 0
    9. Dim startX As Integer = e.MarginBounds.Left
    10. Dim startY As Integer = e.MarginBounds.Top
    11. Dim titleHeight As Integer = CInt(titleFont.GetHeight())
    12. Dim headerHeight As Integer = CInt(headerFont.GetHeight()) + 4
    13. Dim rowHeight As Integer = CInt(normalFont.GetHeight()) + 4
    14. Dim currentY As Integer = startY
    15. Dim cellBounds As Rectangle
    16. Dim headerBounds As Rectangle
    17. Dim dgv1Bounds As Rectangle
    18. Dim dgv1ColumnCount As Integer = DGV_1.ColumnCount
    19. Dim totalRows1 As Integer
    20. Dim currentRow1 As Integer = 0
    21. ' Calculate the width and height of the DataGridViews
    22. For i As Integer = 0 To dgv1ColumnCount - 1
    23. dgv1Width += DGV_1.Columns(i).Width + 18
    24. Next
    25. totalRows1 = DGV_1.RowCount - 1
    26. dgv1Height = (totalRows1 + 3) * rowHeight
    27. ' Draw the title
    28. Dim title As String = "Wetterdaten Jahresabschluss"
    29. e.Graphics.DrawString(title, titleFont, Brushes.Maroon, startX, startY - 40)
    30. currentY += titleHeight
    31. ' Draw the first DataGridView
    32. If dgv1Height > e.MarginBounds.Height - titleHeight Then
    33. dgv1Height = e.MarginBounds.Height - titleHeight
    34. End If
    35. dgv1Bounds = New Rectangle(startX, currentY, dgv1Width + 18, dgv1Height + 40)
    36. headerBounds = dgv1Bounds
    37. headerBounds.Height = headerHeight
    38. e.Graphics.FillRectangle(Brushes.LightGray, headerBounds)
    39. e.Graphics.DrawRectangle(Pens.DarkGray, dgv1Bounds)
    40. startY += headerHeight
    41. For i As Integer = 0 To dgv1ColumnCount - 1
    42. cellBounds = New Rectangle(startX, startY + 2, CInt(DGV_1.Columns(i).Width + 19.5), headerHeight)
    43. e.Graphics.DrawString(DGV_1.Columns(i).HeaderText, headerFont, Brushes.Black, cellBounds)
    44. startX += CInt(DGV_1.Columns(i).Width + 19.5)
    45. Next
    46. For i As Integer = currentRow1 To totalRows1
    47. If currentY + rowHeight > e.MarginBounds.Height + e.MarginBounds.Top Then
    48. e.HasMorePages = False
    49. currentRow1 = i
    50. startX = e.MarginBounds.Left
    51. currentY = e.MarginBounds.Top
    52. Exit Sub
    53. End If
    54. startX = dgv1Bounds.Left + 1
    55. Dim backgroundColor As Brush = If(i Mod 2 = 0, Brushes.White, Brushes.LightGray)
    56. For j As Integer = 0 To dgv1ColumnCount - 1
    57. cellBounds = New Rectangle(startX, currentY + 24, CInt(DGV_1.Columns(j).Width + 19.5), rowHeight)
    58. e.Graphics.FillRectangle(backgroundColor, cellBounds)
    59. e.Graphics.DrawString(DGV_1.Rows(i).Cells(j).FormattedValue.ToString(), normalFont, Brushes.Black, cellBounds)
    60. e.Graphics.DrawRectangle(Pens.Black, cellBounds)
    61. startX += CInt(DGV_1.Columns(j).Width + 19.5)
    62. Next
    63. startX = e.MarginBounds.Left
    64. currentY += rowHeight
    65. Next
    66. ' Draw the title2
    67. Dim title2 As String = "Gesamter Jahresdurchschnitt"
    68. e.Graphics.DrawString(title2, titleFont2, Brushes.Maroon, startX, startY + 300)
    69. currentY += titleHeight
    70. ' Draw the second DataGridView
    71. startX = dgv1Bounds.Left
    72. For j As Integer = 0 To dgv1ColumnCount - 1
    73. cellBounds = New Rectangle(startX + 1, currentY + 40, CInt(DGV_1.Columns(j).Width + 19.5), rowHeight)
    74. e.Graphics.FillRectangle(Brushes.Beige, cellBounds)
    75. e.Graphics.DrawString(DGV_2.Rows(0).Cells(j).FormattedValue.ToString(), normalFont2, Brushes.Black, cellBounds)
    76. startX += CInt(DGV_1.Columns(j).Width + 19.5)
    77. Next
    78. End Sub



    Beiträge zusammengefügt. ~Thunderbolt
    Bilder
    • 2dgvs-1.jpg

      226,64 kB, 956×601, 76 mal angesehen
    • 2dgvs-2.jpg

      222,25 kB, 941×592, 66 mal angesehen
    Asperger Autistin. Brauche immer etwas um gewisse Sachen zu verstehen. :huh:

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Thunderbolt“ ()

    Hallo Amelie

    Danke für dein Lob. Du hast den Code aber recht verbessert. Vielleicht wäre es noch schön den Text rechts auszurichten - also so:

    VB.NET-Quellcode

    1. Dim _right As New StringFormat With {.Alignment = StringAlignment.Far, .LineAlignment = StringAlignment.Center}
    2. ....
    3. e.Graphics.DrawString(DGV_1.Rows(i).Cells(j).FormattedValue.ToString(), normalFont, Brushes.Black, cellBounds, _right)


    LG Panter

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Panter“ ()