Click here to Skip to main content
15,891,184 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Dorsaf Ouersighni26-Jan-16 2:58
Dorsaf Ouersighni26-Jan-16 2:58 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Richard MacCutchan26-Jan-16 3:05
mveRichard MacCutchan26-Jan-16 3:05 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Dorsaf Ouersighni26-Jan-16 3:18
Dorsaf Ouersighni26-Jan-16 3:18 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Dave Kreskowiak26-Jan-16 3:49
mveDave Kreskowiak26-Jan-16 3:49 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Dorsaf Ouersighni26-Jan-16 3:59
Dorsaf Ouersighni26-Jan-16 3:59 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Richard MacCutchan26-Jan-16 4:38
mveRichard MacCutchan26-Jan-16 4:38 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Dorsaf Ouersighni26-Jan-16 4:48
Dorsaf Ouersighni26-Jan-16 4:48 
QuestionPrint Full DataGridView URGENT case Pin
Dorsaf Ouersighni25-Jan-16 20:56
Dorsaf Ouersighni25-Jan-16 20:56 
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:

VB.NET
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Try
            Dim iHeaderHeight As Double
            'Set the left margin
            Dim iLeftMargin As Integer = e.MarginBounds.Left
            'Set the top margin
            Dim iTopMargin As Integer = e.MarginBounds.Top
            'Whether more pages have to print or not
            Dim bMorePagesToPrint As Boolean = False
            Dim iTmpWidth As Integer = 0
            'For the first page to print set the cell width and header height
            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)
                    ' Save width and height of headers
                    arrColumnLefts.Add(iLeftMargin)
                    arrColumnWidths.Add(iTmpWidth)
                    iLeftMargin = (iLeftMargin + iTmpWidth)
                Next
            End If
 
            'Loop till all the grid rows not get printed
            Dim iRow As Double
            While (iRow <= (DataGridView_impot.Rows.Count - 1))
                Dim GridRow As DataGridViewRow = DataGridView_impot.Rows(iRow)
                'Set the cell height
                iCellHeight = (GridRow.Height + 5)
                Dim iCount As Integer = 0
                'Check whether the current page settings allows more rows to print
                If (iTopMargin + (iCellHeight >= (e.MarginBounds.Height + e.MarginBounds.Top))) Then
                    bNewPage = True
                    bFirstPage = False
                    bMorePagesToPrint = True
                    Exit While
                Else
                    If bNewPage Then
                        'Draw Header => raison sociale
                        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)))
                        'Draw Date => date system
                        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)))
                        'Draw Columns                 
                        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
                    'Draw Columns Contents
                    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
 
                        'Drawing Cells Borders 
                        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 more lines exist, print another page.
            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

AnswerRe: Print Full DataGridView URGENT case Pin
Mycroft Holmes25-Jan-16 21:09
professionalMycroft Holmes25-Jan-16 21:09 
GeneralRe: Print Full DataGridView URGENT case Pin
Dorsaf Ouersighni25-Jan-16 21:23
Dorsaf Ouersighni25-Jan-16 21:23 
GeneralRe: Print Full DataGridView URGENT case Pin
Dorsaf Ouersighni25-Jan-16 21:34
Dorsaf Ouersighni25-Jan-16 21:34 
QuestionDistinguish between Reading from Barcode reader and Writing from standart keyboard Pin
satc25-Jan-16 13:07
satc25-Jan-16 13:07 
AnswerRe: Distinguish between Reading from Barcode reader and Writing from standart keyboard Pin
Sascha Lefèvre25-Jan-16 22:36
professionalSascha Lefèvre25-Jan-16 22:36 
GeneralRe: Distinguish between Reading from Barcode reader and Writing from standart keyboard Pin
satc26-Jan-16 8:11
satc26-Jan-16 8:11 
GeneralRe: Distinguish between Reading from Barcode reader and Writing from standart keyboard Pin
Sascha Lefèvre26-Jan-16 8:44
professionalSascha Lefèvre26-Jan-16 8:44 
GeneralRe: Distinguish between Reading from Barcode reader and Writing from standart keyboard Pin
satc26-Jan-16 10:01
satc26-Jan-16 10:01 
GeneralRe: Distinguish between Reading from Barcode reader and Writing from standart keyboard Pin
Sascha Lefèvre26-Jan-16 10:15
professionalSascha Lefèvre26-Jan-16 10:15 
GeneralRe: Distinguish between Reading from Barcode reader and Writing from standart keyboard Pin
satc26-Jan-16 10:27
satc26-Jan-16 10:27 
GeneralRe: Distinguish between Reading from Barcode reader and Writing from standart keyboard Pin
Sascha Lefèvre26-Jan-16 11:23
professionalSascha Lefèvre26-Jan-16 11:23 
Questionbouton imprimer DGV complet Pin
Dorsaf Ouersighni25-Jan-16 4:54
Dorsaf Ouersighni25-Jan-16 4:54 
SuggestionRe: bouton imprimer DGV complet Pin
Richard Deeming25-Jan-16 5:15
mveRichard Deeming25-Jan-16 5:15 
GeneralRe: bouton imprimer DGV complet Pin
Dorsaf Ouersighni25-Jan-16 20:45
Dorsaf Ouersighni25-Jan-16 20:45 
QuestionXML Datagridview control & saving changes Pin
Dan Chapin25-Jan-16 3:40
Dan Chapin25-Jan-16 3:40 
QuestionSetting Primary Display Pin
Jayme6518-Jan-16 23:33
Jayme6518-Jan-16 23:33 
AnswerRe: Setting Primary Display Pin
Richard Andrew x6419-Jan-16 4:39
professionalRichard Andrew x6419-Jan-16 4:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.