I have a DataGridView that contains data and images, fetched from database. I have to print this data grid view.
Problem:
1. DataGridView contains data that will not fit in one page, so have to print in multiple pages.
2. The Images are not appearing in print preview.
3. Data that exceeds first page doesn't appear in next page or overwritten in the first page(print preview).
The Code I have tried prints data in the first page and in the next page there are only column headers(no rows).
*My code opens the print preview dialog first and then I have to print it.
** I have also modified the Print option(ToolStrip) of the PrintPreviewDialog..
What I have tried:
Private Sub Print_lst_Click(sender As Object, e As EventArgs) Handles Print_lst.Click
Dim b As New ToolStripButton
b.Image = CType(PrintPreviewDialog1.Controls(1), ToolStrip).ImageList.Images(0)
b.ToolTipText = "Print"
b.DisplayStyle = ToolStripItemDisplayStyle.Image
AddHandler b.Click, AddressOf PrintPreview_PrintClick
CType(PrintPreviewDialog1.Controls(1), ToolStrip).Items.RemoveAt(0)
CType(PrintPreviewDialog1.Controls(1), ToolStrip).Items.Insert(0, b)
AddHandler PrintDocument1.PrintPage, AddressOf PrintDocument1_PrintPage
PrintPreviewDialog1.Document = PrintDocument1
PrintDocument1.OriginAtMargins = True
PrintDocument1.DefaultPageSettings.Margins = New Drawing.Printing.Margins(0, 0, 0, 0)
PrintPreviewDialog1.WindowState = FormWindowState.Maximized
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub PrintPreview_PrintClick(sender As Object, e As EventArgs)
Try
PrintDialog1.Document = PrintDocument1
If PrintDialog1.ShowDialog() = DialogResult.OK Then
PrintDocument1.Print()
End If
Catch ex As Exception
End Try
clean()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim x As Integer = 50
Dim y As Integer = 50
Dim header As Boolean = True
Dim j As Integer = 0
Do While (j < Me.DataGridView1.Columns.Count)
Dim rect As Rectangle = New Rectangle(x, y, Me.DataGridView1.Columns(j).Width, Me.DataGridView1.ColumnHeadersHeight)
e.Graphics.FillRectangle(Brushes.LightGray, rect)
e.Graphics.DrawRectangle(Pens.Black, rect)
If (Not (Me.DataGridView1.Columns(j).HeaderText) Is Nothing) Then
e.Graphics.DrawString(Me.DataGridView1.Columns(j).HeaderText, SystemFonts.DefaultFont, Brushes.Black, rect)
End If
x = (x + rect.Width)
j = (j + 1)
Loop
x = 50
y = (y + Me.DataGridView1.ColumnHeadersHeight)
For whRow = whRow To Me.DataGridView1.RowCount - 1
Dim drow As DataGridViewRow = Me.DataGridView1.Rows(whRow)
j = 0
Do While (j < Me.DataGridView1.Columns.Count)
Dim cell As DataGridViewCell
cell = drow.Cells(j)
Dim rect As Rectangle = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)
e.Graphics.DrawRectangle(Pens.Black, rect)
If (Not (cell.Value) Is Nothing) Then
e.Graphics.DrawString(cell.Value.ToString, SystemFonts.DefaultFont, Brushes.Black, rect)
End If
x = (x + rect.Width)
j = (j + 1)
Loop
x = 50
y = (y + drow.Height)
If (y > e.MarginBounds.Bottom) Then
e.HasMorePages = True
End If
Next
End Sub