How can i check or see the print preview of datagridview where my file is an access file and pls give me sample code.
harsh01ajmera 0 Newbie Poster
Dani AI
Generated
The question is about showing a print preview for a DataGridView bound to an Access file. asked for sample code, correctly noted a preview control/dialog is involved, and showed the simple act of assigning a PrintDocument to a PrintPreviewDialog. Below are two practical, complementary approaches: a quick snapshot (fast to implement) and a more robust paginated printer (recommended for full datasets and Access-bound grids).
Quick snapshot (good for small grids / exact on‑screen look). DrawToBitmap captures the visible portion of the grid and the PrintDocument draws that bitmap. Note: DrawToBitmap will only capture what’s currently visible on screen, so it is not suitable for very large or scrolled grids.
' Form-level:
Private gridBitmap As Bitmap
Private Sub MakeSnapshot()
Dim w = DataGridView1.Width
Dim h = DataGridView1.Height
gridBitmap = New Bitmap(w, h)
DataGridView1.DrawToBitmap(gridBitmap, New Rectangle(0, 0, w, h))
End Sub
Private Sub PrintDoc_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
If gridBitmap IsNot Nothing Then
Dim dest = e.MarginBounds
e.Graphics.DrawImage(gridBitmap, dest)
End If
e.HasMorePages = False
End Sub Robust paginated printing (recommended for full data): compute column widths scaled to the printable area, draw column headers, iterate rows and draw cell text, and set e.HasMorePages when the page fills. This prints all rows and handles pagination reliably.
' Form-level state:
Private curRow As Integer = 0
Private colLefts As New List(Of Integer)
Private colWidths As New List(Of Integer)
Private Sub PrintDoc_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim g = e.Graphics
Dim left = e.MarginBounds.Left
Dim y = e.MarginBounds.Top
Dim headerFont = New Font("Arial", 9, FontStyle.Bold)
Dim cellFont = New Font("Arial", 9)
If colWidths.Count = 0 Then
Dim totalColWidth = DataGridView1.Columns.Cast(Of DataGridViewColumn).Sum(Function(c) c.Width)
Dim curLeft = left
For Each c As DataGridViewColumn In DataGridView1.Columns
Dim w = CInt(Math.Floor(c.Width / totalColWidth * e.MarginBounds.Width))
colLefts.Add(curLeft)
colWidths.Add(w)
curLeft += w
Next
End If
' headers
For i = 0 To DataGridView1.Columns.Count - 1
Dim r = New Rectangle(colLefts(i), y, colWidths(i), 20)
g.FillRectangle(Brushes.LightGray, r)
g.DrawRectangle(Pens.Black, r)
g.DrawString(DataGridView1.Columns(i).HeaderText, headerFont, Brushes.Black, r)
Next
y += 22
' rows
While curRow < DataGridView1.Rows.Count
Dim row = DataGridView1.Rows(curRow)
Dim rowHeight = Math.Max(20, CInt(cellFont.GetHeight(g)) + 4)
If y + rowHeight > e.MarginBounds.Bottom Then
e.HasMorePages = True
Return
End If
For i = 0 To DataGridView1.Columns.Count - 1
Dim r = New Rectangle(colLefts(i), y, colWidths(i), rowHeight)
g.DrawRectangle(Pens.Black, r)
Dim txt = If(row.Cells(i).Value IsNot Nothing, row.Cells(i).Value.ToString(), "")
g.DrawString(txt, cellFont, Brushes.Black, r)
Next
y += rowHeight
curRow += 1
End While
e.HasMorePages = False
colLefts.Clear() : colWidths.Clear() : curRow = 0
End Sub Troubleshooting notes: hide unused columns before printing, set AllowUserToAddRows = False to avoid the new-row, format numbers/dates beforehand, and prefer printing from the DataTable (Access) when the grid is virtualized. Attach the PrintDocument to a PrintPreviewDialog or PrintPreviewControl (as demonstrated) to show the preview.
Recommended Answers
Jump to Post— poojavb 29To do the print preview part u need to add the print priview control or Dialog on ur form....
All 2 Replies
poojavb 29 Junior Poster
To do the print preview part u need to add the print priview control or Dialog on ur form....
Member #763477
Hi,
It is very involved printing a datagridview. if you have a printdocument already setup then you can use the following code to preview the document.
Private Sub PrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreview.Click
Dim dlg As New PrintPreviewDialog()
'Dim d As Object
Dim d
dlg.Document = PrintDocument1
d = PrintDocument1.DefaultPageSettings.Bounds
dlg.ShowDialog()
End Sub
Curtis
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.