none print datagridview(connected to mysql database)? RRS feed

  • Question

  • i used the  code below to sync the datagridview on the mysql database now i want to use on printrpriview but i dont know how am i going to do it, since its connected to a databse, any idea or suggestion on what should i do ?

      Private Sub load_datagrid()
            Dim con As New MySqlConnection("server=localhost;user id=root;password=pass;persistsecurityinfo=True;database=bsit")
            Dim cmd As New MySqlCommand
            Dim da As New MySqlDataAdapter

            Dim bs As New BindingSource
                cmd.Connection = con
                cmd.CommandType = CommandType.Text
                cmd.CommandText = "Select * from"
                da.SelectCommand = cmd
                bs.DataSource = ds
                DataGridView1.DataSource = bs
            Catch ex As Exception
            End Try

    Sunday, March 19, 2017 6:40 AM

All replies

  • Hello,

    See the following post.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, March 19, 2017 10:39 AM
  • Hi Arnold,

    I put Button, PrintDDocument1, PrintPreviewDialog1 control in Form, and then load data into datagridview, the code that you can refer to.


    Dim mRow As Integer = 0
    Dim newpage As Boolean = True
    Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        With DataGridView1
            Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
            fmt.LineAlignment = StringAlignment.Center
            fmt.Trimming = StringTrimming.EllipsisCharacter
            Dim y As Single = e.MarginBounds.Top
            Do While mRow < .RowCount
                Dim row As DataGridViewRow = .Rows(mRow)
                Dim x As Single = e.MarginBounds.Left
                Dim h As Single = 0
                For Each cell As DataGridViewCell In row.Cells
                    Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
                    e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
                    If (newpage) Then
                        e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
                        e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
                    End If
                    x += rc.Width
                    h = Math.Max(h, rc.Height)
                newpage = False
                y += h
                mRow += 1
                If y + h > e.MarginBounds.Bottom Then
                    e.HasMorePages = True
                    mRow -= 1
                    newpage = True
                    Exit Sub
                End If
            mRow = 0
        End With
    End Sub
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        PrintPreviewDialog1.Document = PrintDocument1
    End Sub

    Best Regards,

    Cherry Bu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Monday, March 20, 2017 9:27 AM