Click here to Skip to main content
15,902,276 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: VS2015 - MsFlexgrid not displaying when building form Pin
Mycroft Holmes28-Jan-16 22:56
professionalMycroft Holmes28-Jan-16 22:56 
GeneralRe: VS2015 - MsFlexgrid not displaying when building form Pin
Denis Oxon28-Jan-16 23:17
Denis Oxon28-Jan-16 23:17 
GeneralRe: VS2015 - MsFlexgrid not displaying when building form Pin
Mycroft Holmes28-Jan-16 23:20
professionalMycroft Holmes28-Jan-16 23:20 
GeneralRe: VS2015 - MsFlexgrid not displaying when building form Pin
Denis Oxon29-Jan-16 2:11
Denis Oxon29-Jan-16 2:11 
GeneralRe: VS2015 - MsFlexgrid not displaying when building form Pin
Dave Kreskowiak29-Jan-16 2:25
mveDave Kreskowiak29-Jan-16 2:25 
QuestionCalling BCP from .net on Windows 2012 Pin
byka28-Jan-16 7:15
byka28-Jan-16 7:15 
AnswerRe: Calling BCP from .net on Windows 2012 Pin
Dave Kreskowiak29-Jan-16 14:56
mveDave Kreskowiak29-Jan-16 14:56 
QuestionHow to print a full DataGridView (Urgent) Pin
Dorsaf Ouersighni25-Jan-16 22:15
Dorsaf Ouersighni25-Jan-16 22:15 
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, Smile | :)
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

GeneralRe: How to print a full DataGridView (Urgent) Pin
Sascha Lefèvre25-Jan-16 22:40
professionalSascha Lefèvre25-Jan-16 22:40 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Dorsaf Ouersighni25-Jan-16 22:52
Dorsaf Ouersighni25-Jan-16 22:52 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Sascha Lefèvre25-Jan-16 23:00
professionalSascha Lefèvre25-Jan-16 23:00 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Dorsaf Ouersighni25-Jan-16 23:33
Dorsaf Ouersighni25-Jan-16 23:33 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Sascha Lefèvre26-Jan-16 0:06
professionalSascha Lefèvre26-Jan-16 0:06 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Dorsaf Ouersighni26-Jan-16 1:36
Dorsaf Ouersighni26-Jan-16 1:36 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Sascha Lefèvre26-Jan-16 2:03
professionalSascha Lefèvre26-Jan-16 2:03 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Dorsaf Ouersighni26-Jan-16 2:44
Dorsaf Ouersighni26-Jan-16 2:44 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Sascha Lefèvre26-Jan-16 4:01
professionalSascha Lefèvre26-Jan-16 4:01 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Dorsaf Ouersighni26-Jan-16 4:36
Dorsaf Ouersighni26-Jan-16 4:36 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Sascha Lefèvre26-Jan-16 4:45
professionalSascha Lefèvre26-Jan-16 4:45 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Sascha Lefèvre26-Jan-16 5:05
professionalSascha Lefèvre26-Jan-16 5:05 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Dorsaf Ouersighni26-Jan-16 21:04
Dorsaf Ouersighni26-Jan-16 21:04 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Jörgen Andersson26-Jan-16 22:52
professionalJörgen Andersson26-Jan-16 22:52 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Dorsaf Ouersighni27-Jan-16 2:39
Dorsaf Ouersighni27-Jan-16 2:39 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Jörgen Andersson27-Jan-16 3:22
professionalJörgen Andersson27-Jan-16 3:22 
GeneralRe: How to print a full DataGridView (Urgent) Pin
Sascha Lefèvre26-Jan-16 2:29
professionalSascha Lefèvre26-Jan-16 2:29 

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.