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
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...
VB.NET-Quellcode
- Private Sub PrintDataGridViews(ByVal sender As Object, ByVal e As PrintPageEventArgs)
- Dim titleFont As New Font("Arial", 14, FontStyle.Bold)
- Dim headerFont As New Font("Arial", 8, FontStyle.Regular)
- Dim normalFont As New Font("Arial", 11, FontStyle.Regular)
- Dim dgv1Width As Integer = 0
- Dim dgv2Width As Integer = 0
- Dim dgv1Height As Integer = 0
- Dim dgv2Height As Integer = 0
- Dim startX As Integer = e.MarginBounds.Left
- Dim startY As Integer = e.MarginBounds.Top
- Dim titleHeight As Integer = CInt(titleFont.GetHeight())
- Dim headerHeight As Integer = CInt(headerFont.GetHeight())
- Dim rowHeight As Integer = CInt(normalFont.GetHeight())
- Dim currentY As Integer = startY
- Dim cellBounds As Rectangle
- Dim headerBounds As Rectangle
- Dim dgv1Bounds As Rectangle
- Dim dgv2Bounds As Rectangle
- Dim dgv1ColumnCount As Integer = DGV_1.ColumnCount
- Dim dgv2ColumnCount As Integer = DGV_2.ColumnCount
- Dim totalRows1 As Integer
- Dim totalRows2 As Integer
- Dim currentRow1 As Integer = 0
- Dim currentRow2 As Integer = 0
- Dim horizontalSpacing As Integer = 50
- ' Calculate the width and height of the DataGridViews
- For i As Integer = 0 To dgv1ColumnCount - 1
- dgv1Width += DGV_1.Columns(i).Width
- Next
- For i As Integer = 0 To dgv2ColumnCount - 1
- dgv2Width += DGV_2.Columns(i).Width
- Next
- totalRows1 = DGV_1.RowCount - 1
- totalRows2 = DGV_2.RowCount - 1
- dgv1Height = (totalRows1 + 1) * rowHeight
- dgv2Height = (totalRows2 + 1) * rowHeight
- ' Draw the title
- Dim title As String = "Wetterdaten Jahresabschluss"
- e.Graphics.DrawString(title, titleFont, Brushes.Blue, startX, startY - 40)
- currentY += titleHeight
- ' Draw the first DataGridView
- If dgv1Height > e.MarginBounds.Height - titleHeight Then
- dgv1Height = e.MarginBounds.Height - titleHeight
- End If
- dgv1Bounds = New Rectangle(startX, currentY, dgv1Width, dgv1Height)
- headerBounds = dgv1Bounds
- headerBounds.Height = headerHeight
- e.Graphics.FillRectangle(Brushes.LightGray, headerBounds)
- e.Graphics.DrawRectangle(Pens.Black, dgv1Bounds)
- currentY += headerHeight
- For i As Integer = 0 To dgv1ColumnCount - 1
- cellBounds = New Rectangle(startX, currentY, DGV_1.Columns(i).Width, headerHeight)
- e.Graphics.DrawString(DGV_1.Columns(i).HeaderText, headerFont, Brushes.Black, cellBounds)
- startX += DGV_1.Columns(i).Width
- Next
- currentY += rowHeight
- For i As Integer = currentRow1 To totalRows1
- If currentY + rowHeight > e.MarginBounds.Height + e.MarginBounds.Top Then
- e.HasMorePages = True
- currentRow1 = i
- startX = e.MarginBounds.Left
- currentY = e.MarginBounds.Top
- Exit Sub
- End If
- startX = dgv1Bounds.Left
- For j As Integer = 0 To dgv1ColumnCount - 1
- cellBounds = New Rectangle(startX, currentY, DGV_1.Columns(j).Width, rowHeight)
- e.Graphics.DrawString(DGV_1.Rows(i).Cells(j).FormattedValue.ToString(), normalFont, Brushes.Black, cellBounds)
- startX += DGV_1.Columns(j).Width
- Next
- startX = e.MarginBounds.Left
- currentY += rowHeight
- Next
- ' Draw the second DataGridView
- startY += dgv1Height + headerHeight + horizontalSpacing
- If dgv2Height > e.MarginBounds.Height - titleHeight - dgv1Height - headerHeight Then
- dgv2Height = e.MarginBounds.Height - titleHeight - dgv1Height - headerHeight
- End If
- dgv2Bounds = New Rectangle(startX, startY, dgv2Width, dgv2Height)
- headerBounds = dgv2Bounds
- headerBounds.Height = headerHeight
- e.Graphics.FillRectangle(Brushes.LightGray, headerBounds)
- e.Graphics.DrawRectangle(Pens.Black, dgv2Bounds)
- currentY += headerHeight
- For i As Integer = 0 To dgv2ColumnCount - 1
- cellBounds = New Rectangle(startX, startY, DGV_2.Columns(i).Width, headerHeight)
- e.Graphics.DrawString(DGV_2.Columns(i).HeaderText, headerFont, Brushes.Black, cellBounds)
- startX += DGV_2.Columns(i).Width
- Next
- currentY += rowHeight
- For i As Integer = currentRow2 To totalRows2
- If currentY + rowHeight > e.MarginBounds.Height + e.MarginBounds.Top Then
- e.HasMorePages = True
- currentRow2 = i
- startX = e.MarginBounds.Left
- currentY = e.MarginBounds.Top
- Exit Sub
- End If
- startX = dgv2Bounds.Left
- For j As Integer = 0 To dgv2ColumnCount - 1
- cellBounds = New Rectangle(startX, currentY, DGV_2.Columns(j).Width, rowHeight)
- e.Graphics.DrawString(DGV_2.Rows(i).Cells(j).FormattedValue.ToString(), normalFont, Brushes.Black, cellBounds)
- startX += DGV_2.Columns(j).Width
- Next
- startX = e.MarginBounds.Left
- currentY += rowHeight
- Next
- End Sub
VB.NET-Quellcode
-
- Private Sub btn_Charts_Click(sender As Object, e As EventArgs) Handles btn_Charts.Click
- Dim pd As New PrintDocument()
- Dim ps As New PaperSize("A4 Landscape", 827, 1169 + pd.DefaultPageSettings.Margins.Top + pd.DefaultPageSettings.Margins.Bottom)
- ps.PaperName = CStr(PaperKind.A4)
- pd.DefaultPageSettings.Landscape = True
- pd.DefaultPageSettings.PaperSize = ps
- AddHandler pd.PrintPage, AddressOf PrintDataGridViews
- pd.Print()
- End Sub
Asperger Autistin. Brauche immer etwas um gewisse Sachen zu verstehen.