Currently I managed to display my data inside a datagrid control. But i want to create a button control for a print function to enable user to print the datagrid content. Any idea?

Dani AI

Generated

asked how to let users print a datagrid. pointed at reporting tools and noted Form1.PrintForm didn’t solve the problem. Two practical paths work well depending on the scenario:

  • Use a report engine (Crystal Reports, ReportViewer). Good when you need professional page layout, automatic pagination, exports (PDF/Excel) and consistent headers/footers.
  • Print programmatically from WinForms when you want full control or a quick ad-hoc print. PrintDocument + the PrintPage event is the typical pattern. PrintForm only snapshots what’s visible; it won’t paginate or handle rows off-screen.

A minimal VB.NET approach (WinForms, DataGridView) — handle the PrintPage event, draw headers and rows, track the current row and set e.HasMorePages when you run out of space. This skeleton shows the pattern to follow:

Dim pd As New Printing.PrintDocument()
AddHandler pd.PrintPage, AddressOf pd_PrintPage
PrintPreviewDialog1.Document = pd
PrintPreviewDialog1.ShowDialog()

Private currentRow As Integer = 0

Private Sub pd_PrintPage(sender As Object, e As Printing.PrintPageEventArgs)
    Dim g = e.Graphics
    Dim font As New Font("Arial", 10)
    Dim y = e.MarginBounds.Top
    Dim rowHeight = font.Height + 6

    ' Draw column headers here...

    While currentRow < DataGridView1.Rows.Count
        Dim row = DataGridView1.Rows(currentRow)
        Dim x = e.MarginBounds.Left
        For Each cell As DataGridViewCell In row.Cells
            g.DrawString(If(cell.Value, "").ToString(), font, Brushes.Black, x, y)
            x += 100 ' compute from column widths
        Next
        y += rowHeight
        currentRow += 1
        If y + rowHeight > e.MarginBounds.Bottom Then
            e.HasMorePages = True
            Return
        End If
    End While

    e.HasMorePages = False
    currentRow = 0
End Sub

Troubleshooting tips: measure text widths with Graphics.MeasureString to compute column widths and page breaks; test with PrintPreviewDialog; for very large data sets or complex formatting use a reporting tool to avoid reimplementing pagination. For web/ASP.NET datagrids, produce a printer-friendly HTML page or generate a PDF server-side for reliable client printing. For official API details see the PrintDocument docs: PrintDocument class.

You can use a crystal report, if you do you can select the table's from youre database that you want to print, you have to search and try a little but it wil work.

I think he is more interested in printing from the datagrid directly with a code instead of using crystal report.

Currently I am looking for the same code. I have tried Form1.PrintForm

but this didn't work. I think we need to use the print document and bind data to it.

Regards

jay

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.