Salve, ho la necessita di stampare da un Db MySQL
Ho provato questo codice(veniva utilizzato per stampare da access tramite una datagridview, io voglio stampare i dati del database mysql o della listview forse direttamente dal MySQL è più pulito:

codice:
Imports MySql.Data.MySqlClient
Imports System.Data
Imports System.Drawing.Printing


Public Class Form1


    Public strSQL As String
    Public cmd As New MySqlCommand
    Public dr As MySqlDataReader


    Dim Matricola As Integer
    Dim Categoria As String
    Dim Prezzo As Integer


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Dim ConnString As String = "Server=" & "192.168.0.116" & ";Database=Magazzino;Uid=" & "admin" & ";Pwd=" & "123456" & ";"
        Dim CONNECTION As New MySqlConnection(ConnString)
        strSQL = "SELECT * FROM clienti where articoli;"
        CONNECTION.Open()
        cmd = New MySqlCommand(strSQL, CONNECTION)
        dr = cmd.ExecuteReader()


        Do While dr.Read()




            Matricola = dr("Matricola").ToString
            Categoria = dr("Categoria").ToString
            Prezzo = dr("Prezzo").ToString


        Loop
        dr.Close()
        cmd.Dispose()
        CONNECTION.Close()


    End Sub
    Private Sub Impostazioni_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Impostazioni.Click
        PageSetupDialog1.Document = PrintDocument1
        If PageSetupDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
        Else
            Exit Sub


        End If
    End Sub


    Private Sub Stampa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Stampa.Click


        If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            PrintDocument1.Print()
        Else
            Exit Sub
        End If


    End Sub


    Private Sub Anteprima_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Anteprima.Click
        If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.ShowDialog()
        Else
            Exit Sub
        End If
    End Sub
    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage


        Dim Font1 As New Font("Arial Black", 18)
        Dim Font2 As New Font("Arial", 18, FontStyle.Bold)
        Dim Font3 As New Font("Arial", 12, FontStyle.Bold)
        Dim Font4 As New Font("Arial", 12)
        Static i As Integer
        Dim Y As Integer = 340
        Dim str As String
        Static Pagina As Integer
        Dim Parziale As Double
        Static Incrementale As Double
        Static Totale As Double




        e.Graphics.DrawImage(Image.FromFile(Application.StartupPath & "\Marchio.jpg"), 50, 50, 150, 150)
        e.Graphics.DrawString("NORTHWIND TRADERS", Font1, Brushes.Black, 250, 100)
        e.Graphics.DrawLine(Pens.Red, 50, 220, 750, 220)


        e.Graphics.DrawString("ELENCO LISTINO VENDITA", Font2, Brushes.Black, 50, 250)
        e.Graphics.DrawString("Cod.", Font3, Brushes.Black, 50, 300)
        e.Graphics.DrawString("Articolo", Font3, Brushes.Black, 150, 300)
        e.Graphics.DrawString("Quantità", Font3, Brushes.Black, 500, 300)
        e.Graphics.DrawString("Prezzo", Font3, Brushes.Black, 650, 300)






        For i = i To i + 24


            If i < bds.Count Then
                str = dts.Tables("Products").Rows(i).Item(0)
                e.Graphics.DrawString((str.PadLeft(5 - Len(str))), Font4, Brushes.Black, 50, Y)
                str = dts.Tables("Products").Rows(i).Item(1)
                e.Graphics.DrawString(str, Font4, Brushes.Black, 150, Y)
                str = dts.Tables("Products").Rows(i).Item(2)
                e.Graphics.DrawString((str.PadLeft(10 - Len(str))), Font4, Brushes.Black, 500, Y)
                str = dts.Tables("Products").Rows(i).Item(3)
                Parziale += str
                e.Graphics.DrawString((str.PadLeft(11 - Len(str))), Font4, Brushes.Black, 650, Y)
                Y = Y + Font4.GetHeight


                e.HasMorePages = True
            Else
                e.HasMorePages = False
            End If
        Next i


        If i > bds.Count Then
            e.Graphics.DrawLine(Pens.Black, 650, (Y + 10), 720, (Y + 10))
            e.Graphics.DrawString("TOTALE ", Font3, Brushes.Black, 580, (Y + 20))
            Totale = Incrementale + Parziale
            e.Graphics.DrawString(Totale, Font3, Brushes.Black, 650, (Y + 20))
        End If








        e.Graphics.DrawLine(Pens.Red, 50, 830, 750, 830)
        Pagina += 1
        e.Graphics.DrawString("Pagina " & Pagina, Font2, Brushes.Black, 600, 850)
        e.Graphics.DrawString("Valore della pagina " & Parziale, Font2, Brushes.Black, 50, 850)
        Incrementale += Parziale
        e.Graphics.DrawString("Subtotale " & Incrementale, Font2, Brushes.Black, 50, 900)
    End Sub


End Class
L'errore sta nell'evento PrintDocument1_PrintPage

Questa è la parte da modificare:
codice:
   For i = i To i + 24

            If i < bds.Count Then
                str = dts.Tables("Products").Rows(i).Item(0)
                e.Graphics.DrawString((str.PadLeft(5 - Len(str))), Font4, Brushes.Black, 50, Y)
                str = dts.Tables("Products").Rows(i).Item(1)
                e.Graphics.DrawString(str, Font4, Brushes.Black, 150, Y)
                str = dts.Tables("Products").Rows(i).Item(2)
                e.Graphics.DrawString((str.PadLeft(10 - Len(str))), Font4, Brushes.Black, 500, Y)
                str = dts.Tables("Products").Rows(i).Item(3)
                Parziale += str
                e.Graphics.DrawString((str.PadLeft(11 - Len(str))), Font4, Brushes.Black, 650, Y)
                Y = Y + Font4.GetHeight


                e.HasMorePages = True
            Else
                e.HasMorePages = False
            End If
        Next i


        If i > bds.Count Then
            e.Graphics.DrawLine(Pens.Black, 650, (Y + 10), 720, (Y + 10))
            e.Graphics.DrawString("TOTALE ", Font3, Brushes.Black, 580, (Y + 20))
            Totale = Incrementale + Parziale
            e.Graphics.DrawString(Totale, Font3, Brushes.Black, 650, (Y + 20))
        End If
Io avevo provato a cambiare questi campi
Esempio:
Il codice:
codice:
 str = dts.Tables("Products").Rows(i).Item(0)
Lo cambiavo con:
codice:
str = Matricola
Che sarebbe la variabile.
Però questo come lo calcolo?

codice:
If i > bds.Count Then
Rimuovendo questo codice e sostituendo le righe con le variabili, funzionava però creava pagine all'infinito, sempre con lo stesso articolo.
Come posso risolvere il tutto?
Grazie in anticipo