hello guys i just need help in understanding this block of code. this code is a datagridview printing code that i want to edit to suit my needs but i dont know where to start i tried searching youtube tutorial but i cant seem to find any that explain this(printdocument control). so i want to make the margin a little narrow and to change font for column header and records. i tried changing default/properities of the datagridview and exploring a bit changing all numbers i see in the block of code but i cant seem to understand how it works. and if possible i also want to add a header where i can put a logo.
Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
Try
Dim actualWidth As Integer = dgvInventoryLog.Columns.Cast(Of DataGridViewColumn).Sum(Function(c) c.Width)
Dim percentage As Decimal = CDec(((100 / actualWidth) * e.MarginBounds.Width) / 100)
Dim header As String = "Inventory Log"
Dim footer As String
Dim startX As Integer = e.MarginBounds.Left
Dim startY As Integer = e.MarginBounds.Top
Dim r As Rectangle
Dim headerFont As New Font(dgvInventoryLog.Font.FontFamily, 15, FontStyle.Italic, GraphicsUnit.Pixel)
Dim szf As SizeF = e.Graphics.MeasureString(header, headerFont)
e.Graphics.DrawString(header, headerFont, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - szf.Width) / 2, startY - szf.Height)
footer = "Page " & pageCounter.ToString
szf = e.Graphics.MeasureString(footer, headerFont)
e.Graphics.DrawString(footer, headerFont, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - szf.Width) / 2, e.MarginBounds.Bottom + 5)
startY += 5
'this is the text alignment
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
Dim gridFont As New Font(dgvInventoryLog.Font.FontFamily, dgvInventoryLog.Font.Size, FontStyle.Regular, GraphicsUnit.Pixel)
' If startRow = 0 Then
For x As Integer = 0 To dgvInventoryLog.Columns.Count - 1
r.X = startX
r.Y = startY
r.Width = CInt(dgvInventoryLog.Columns(x).Width * percentage)
r.Height = dgvInventoryLog.Rows(0).Height
e.Graphics.DrawRectangle(Pens.Black, r)
e.Graphics.DrawString(dgvInventoryLog.Columns(x).HeaderText, gridFont, Brushes.Black, r, sf)
startX += r.Width
Next
startY += r.Height
' End If
For y As Integer = startRow To dgvInventoryLog.Rows.Count - 1
If y = dgvInventoryLog.NewRowIndex Then Continue For
startX = e.MarginBounds.Left
For x As Integer = 0 To dgvInventoryLog.Columns.Count - 1
r.X = startX
r.Y = startY
r.Width = CInt(dgvInventoryLog.Columns(x).Width * percentage)
r.Height = dgvInventoryLog.Rows(0).Height
e.Graphics.DrawRectangle(Pens.Black, r)
' e.Graphics.DrawString(If(Not dgvInventoryLog.Rows(y).Cells(x).FormattedValue Is Nothing, dgvInventoryLog.Rows(y).Cells(x).Value.ToString, ""),
'gridFont, Brushes.Black, r, sf)
If x = 6 Then
Dim movementDate As Date = CType(dgvInventoryLog.Rows(y).Cells(x).Value, Date)
e.Graphics.DrawString(movementDate.ToString("dd/MM/yyyy"),
gridFont, Brushes.Black, r, sf)
' or dd/MM/yyyy
Else
e.Graphics.DrawString(If(Not dgvInventoryLog.Rows(y).Cells(x).Value Is Nothing, dgvInventoryLog.Rows(y).Cells(x).Value.ToString, ""),
gridFont, Brushes.Black, r, sf)
End If
startX += r.Width
Next
startY += r.Height
If startY >= e.MarginBounds.Bottom - 10 Then
If y < dgvInventoryLog.Rows.Count - 1 Then
e.HasMorePages = True
pageCounter += 1
startRow = y + 1
Exit For
End If
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
this is the output
ps: im not that experience in stackoverflow(im always just copying code here and there) so if this is not where i should post question like this i will be grateful if you can direct me to the proper place.
