Ciao a tutti,
quando io cerco di stampare la datagriview, la stampa non viene centrata e quindi viene tagliata.

Ecco la preview:




Ecco il codice:

codice:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        PrintDocument1.DefaultPageSettings.Landscape = True

        Dim printDialog1 As PrintDialog = New PrintDialog()
        printDialog1.Document = PrintDocument1
        Dim result As DialogResult = printDialog1.ShowDialog()

        If result = DialogResult.OK Then
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.ShowDialog()
        End If
    End Sub

    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim posX As Integer

        Dim posY As Integer = e.MarginBounds.Top

        Dim Rettangolo As Rectangle

        Dim Altezza As Integer = francesco.RowTemplate.Height

        Dim NormalFont As Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, FontStyle.Regular)

        Dim BoldFont As Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, FontStyle.Bold)

        Static righeStampate As Integer = 0

        Static pagineStampate As Integer = 0

        Dim righeTotali As Integer = francesco.Rows.Count + 1

        Dim righePerPagina As Integer = CType(e.MarginBounds.Height / Altezza, Integer)

        Dim nPagine As Integer

        If righeTotali Mod righePerPagina > 0 Then

            nPagine = (righeTotali \ righePerPagina) + 1

        Else

            nPagine = (righeTotali \ righePerPagina)

        End If

        posX = CType((e.MarginBounds.Width - 360) / 2 + e.MarginBounds.Left, Integer)

        Dim dimensioneCella As System.Drawing.SizeF

        Dim incremento As Integer

        For Each colonna As DataGridViewColumn In francesco.Columns

            dimensioneCella = e.Graphics.MeasureString(colonna.HeaderText.ToString, NormalFont)
            incremento = CType((colonna.Width - dimensioneCella.Width) / 2, Integer)

            Rettangolo = New Rectangle(posX, posY, colonna.Width, Altezza)

            e.Graphics.DrawRectangle(Pens.Black, Rettangolo)

            e.Graphics.DrawString(colonna.HeaderText, BoldFont, Brushes.Black, posX + incremento, posY + 5)
            posX += colonna.Width

        Next

        posY += Altezza

        Dim testo As String

        For i As Integer = righeStampate To righeStampate + righePerPagina - 2

            posX = CType((e.MarginBounds.Width - 360) / 2 + e.MarginBounds.Left, Integer)

            If i = righeTotali - 1 Then

                e.HasMorePages = False
                Exit For

            End If

            For Each colonna As DataGridViewColumn In francesco.Columns
                If francesco.Rows(i).Cells(colonna.Name).Value Is Nothing Then
                    testo = " "
                Else
                    testo = francesco.Rows(i).Cells(colonna.Name).Value.ToString
                End If
                dimensioneCella = e.Graphics.MeasureString(testo.ToString, NormalFont)
                incremento = CType((colonna.Width - dimensioneCella.Width) / 2, Integer)
                Rettangolo = New Rectangle(posX, posY, colonna.Width, Altezza)

                e.Graphics.DrawRectangle(Pens.Black, Rettangolo)

                e.Graphics.DrawString(testo, NormalFont, Brushes.Black, posX + incremento, posY + 5)
                posX += colonna.Width

            Next

            righeStampate += 1
            posY += Altezza

        Next

        pagineStampate += 1

        If pagineStampate < nPagine Then

            e.HasMorePages = True
            posY = e.MarginBounds.Top

        Else

            e.HasMorePages = False
            righeStampate = 0
            pagineStampate = 0

        End If
    End Sub