Hallo und HILFEEEEEEE
Ich versuche nun schon seit Stunden den Fehler zu finden.
Ich möchte das DGV im Querformat ausdrucken. Es sollte immer auf ein DIN A4 Blatt passen, da ja nur immer 28 - 31 Zeilen entstehen.
Hier der letzte Code den ich habe. Anbei ein Bild vom Ausdruck ;;-(
Spoiler anzeigen
Ich versuche nun schon seit Stunden den Fehler zu finden.
Ich möchte das DGV im Querformat ausdrucken. Es sollte immer auf ein DIN A4 Blatt passen, da ja nur immer 28 - 31 Zeilen entstehen.
Hier der letzte Code den ich habe. Anbei ein Bild vom Ausdruck ;;-(
VB.NET-Quellcode
-
- Private WithEvents PrintDocument1 As New Printing.PrintDocument()
- Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
- Dim ps As New Printing.PaperSize("Custom", 583, 827)
- ps.RawKind = Printing.PaperKind.A4Rotated ' Legt das Querformat fest
- PrintDocument1.DefaultPageSettings.PaperSize = ps ' Setzt die Druckereinstellungen auf Querformat
- End Sub
- Private Sub frm_Main_Load(sender As Object, e As EventArgs) Handles Me.Load
- PrintDocument1.DefaultPageSettings.Landscape = True
- PrintDocument1.DefaultPageSettings.PaperSize = New PaperSize("A4Querformat", 827, 583)
- AddHandler PrintDocument1.BeginPrint, AddressOf PrintDocument1_BeginPrint
- '....
- End Sub
- Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
- Dim pageSettings As PageSettings = PrintDocument1.DefaultPageSettings
- Dim printArea As RectangleF = pageSettings.PrintableArea
- PrintDocument1.DefaultPageSettings.Landscape = True
- PrintDocument1.DefaultPageSettings.PaperSize = New PaperSize("A4", 827, 583)
- Dim printWidth As Integer = PrintDocument1.DefaultPageSettings.PaperSize.Width - PrintDocument1.DefaultPageSettings.Margins.Left - PrintDocument1.DefaultPageSettings.Margins.Right
- Dim printHeight As Integer = PrintDocument1.DefaultPageSettings.PaperSize.Height - PrintDocument1.DefaultPageSettings.Margins.Top - PrintDocument1.DefaultPageSettings.Margins.Bottom
- Dim leftMargin As Single = pageSettings.Margins.Left
- Dim rightMargin As Single = pageSettings.Margins.Right
- Dim topMargin As Single = pageSettings.Margins.Top
- Dim bottomMargin As Single = pageSettings.Margins.Bottom
- Dim numRows As Integer = DGV_1.RowCount
- Dim numCols As Integer = DGV_1.ColumnCount
- Dim cellFont As Font = DGV_1.DefaultCellStyle.Font
- Dim headerFont As Font = New Font(cellFont.FontFamily, cellFont.Size, FontStyle.Bold)
- Dim rowHeight As Single = DGV_1.RowTemplate.Height
- Dim headerHeight As Single = rowHeight * 2
- Dim cellPadding As Single = 5
- Dim currentRow As Integer = 0
- Dim currentCol As Integer = 0
- Dim cellBounds As RectangleF
- Dim rowBounds As RectangleF
- While currentRow < numRows
- rowBounds = New RectangleF(leftMargin, topMargin + (currentRow * rowHeight), printWidth, rowHeight)
- cellBounds = New RectangleF(leftMargin + cellPadding, topMargin + (currentRow * rowHeight) + cellPadding, rowHeight - (cellPadding * 2), rowHeight - (cellPadding * 2))
- If currentRow Mod 2 = 0 Then
- e.Graphics.FillRectangle(Brushes.White, rowBounds)
- Else
- e.Graphics.FillRectangle(Brushes.LightGray, rowBounds)
- End If
- For currentCol = 0 To numCols - 1
- Dim headerBounds As RectangleF = New RectangleF(leftMargin + (currentCol * cellBounds.Height), topMargin + (currentRow * rowHeight), cellBounds.Height, headerHeight)
- Dim cellValue As String = DGV_1.Columns(currentCol).HeaderText
- e.Graphics.FillRectangle(Brushes.LightGray, headerBounds)
- e.Graphics.DrawString(cellValue, headerFont, Brushes.Black, headerBounds)
- cellValue = DGV_1.Rows(currentRow).Cells(currentCol).FormattedValue.ToString()
- cellBounds = New RectangleF(leftMargin + (currentCol * cellBounds.Height) + cellPadding, topMargin + (currentRow * rowHeight) + cellPadding, rowHeight - (cellPadding * 2), cellBounds.Height - (cellPadding * 2))
- e.Graphics.DrawString(cellValue, cellFont, Brushes.Black, cellBounds)
- Next
- currentRow += 1
- If currentRow * rowHeight + topMargin + bottomMargin > printHeight Then
- 'e.HasMorePages = True
- Return
- End If
- End While
- e.HasMorePages = False
- End Sub
Asperger Autistin. Brauche immer etwas um gewisse Sachen zu verstehen.