Hello everyone,
I've been looking for the code to print my full DataGridView with Visual Basic 2010 Express for a long period and I tested all codes that are displayed with searches on google but in vain.
But finally I find a C # code that I converted to VB which suits me perfectly but the only problem is that it runs infinitely (infinite loop at the number of pages)
NOTE: The code doesn't generate an error and it's an urgent case because I need it for my job.
Thanks,
Here is my code:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Try
Dim iHeaderHeight As Double
Dim iLeftMargin As Integer = e.MarginBounds.Left
Dim iTopMargin As Integer = e.MarginBounds.Top
Dim bMorePagesToPrint As Boolean = False
Dim iTmpWidth As Integer = 0
If bFirstPage Then
For Each GridCol As DataGridViewColumn In DataGridView_impot.Columns
iTmpWidth = CType(Math.Floor(CType((CType(GridCol.Width, Double) / (CType(iTotalWidth, Double) * (CType(iTotalWidth, Double) * (CType(e.MarginBounds.Width, Double) / CType(iTotalWidth, Double))))), Double)), Integer)
iHeaderHeight = (CType(e.Graphics.MeasureString(GridCol.HeaderText, GridCol.InheritedStyle.Font, iTmpWidth).Height, Integer) + 11)
arrColumnLefts.Add(iLeftMargin)
arrColumnWidths.Add(iTmpWidth)
iLeftMargin = (iLeftMargin + iTmpWidth)
Next
End If
Dim iRow As Double
While (iRow <= (DataGridView_impot.Rows.Count - 1))
Dim GridRow As DataGridViewRow = DataGridView_impot.Rows(iRow)
iCellHeight = (GridRow.Height + 5)
Dim iCount As Integer = 0
If (iTopMargin + (iCellHeight >= (e.MarginBounds.Height + e.MarginBounds.Top))) Then
bNewPage = True
bFirstPage = False
bMorePagesToPrint = True
Exit While
Else
If bNewPage Then
e.Graphics.DrawString(Me.lbl_rs.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, (e.MarginBounds.Top - (e.Graphics.MeasureString(Me.lbl_rs.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)))
e.Graphics.DrawString(Me.lbl_date_now.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), Brushes.Black, (e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(Me.lbl_date_now.Text, New Font(DataGridView_impot.Font, FontStyle.Bold), e.MarginBounds.Width).Width)), (e.MarginBounds.Top - (e.Graphics.MeasureString(Me.lbl_rs.Text, New Font(New Font(DataGridView_impot.Font, FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13)))
iTopMargin = e.MarginBounds.Top
For Each GridCol As DataGridViewColumn In DataGridView_impot.Columns
e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iHeaderHeight))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iHeaderHeight))
e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, New SolidBrush(GridCol.InheritedStyle.ForeColor), New RectangleF(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iHeaderHeight), strFormat)
iCount = (iCount + 1)
Next
bNewPage = False
iTopMargin = (iTopMargin + iHeaderHeight)
End If
iCount = 0
For Each Cel As DataGridViewCell In GridRow.Cells
If (Not (Cel.Value) Is Nothing) Then
e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, New SolidBrush(Cel.InheritedStyle.ForeColor), New RectangleF(CType(arrColumnLefts(iCount), Integer), CType(iTopMargin, Single), CType(arrColumnWidths(iCount), Integer), CType(iCellHeight, Single)), strFormat)
End If
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iCellHeight))
iCount = (iCount + 1)
Next
End If
iRow = (iRow + 1)
iTopMargin = (iTopMargin + iCellHeight)
End While
If bMorePagesToPrint Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
Catch exc As Exception
MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
|