none
Print Data Grid View content RRS feed

  • Question

  • I want to edit the code so that it prints from right to left 

    One of the members gave me this code works well but prints from left to right
    codecode 


    Dim strFormat As StringFormat
        'Used to format the grid rows.
        Dim arrColumnLefts As New ArrayList()
        'Used to save left coordinates of columns
        Dim arrColumnWidths As New ArrayList()
        'Used to save column widths
        Dim iCellHeight As Integer = 0
        'Used to get/set the datagridview cell height
        Dim iTotalWidth As Integer = 0
        '
        Dim iRow As Integer = 0
        'Used as counter
        Dim bFirstPage As Boolean = False
        'Used to check whether we are printing first page
        Dim bNewPage As Boolean = False
        ' Used to check whether we are printing a new page
        Dim iHeaderHeight As Integer = 0
        'Used for the header height
        Private Sub Form9_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'Dim timer As New Timer
            'timer.Interval = 3000
            'AddHandler timer.Tick, AddressOf timer_Tick
            loaddate()
            'timer.Start()
        End Sub
    
        Private Sub loaddate()
            Dim Str As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C# and VB Support\Example(VB)\Sample\Data1.mdf;Integrated Security=True"
            Dim sql As String = "select * from Test12"
            Dim dt As New DataTable
            Using con As New SqlConnection(Str)
                con.Open()
                Using cmd As New SqlCommand(sql, con)
                    Dim adapter As New SqlDataAdapter(cmd)
                    adapter.Fill(dt)
                    DataGridView1.DataSource = dt
                End Using
            End Using
        End Sub
        'Private Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
        '    loaddate()
        'End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'Open the print dialog       
            PrintDialog1.Document = PrintDocument1
            PrintDialog1.UseEXDialog = True
            'Get the document
            If DialogResult.OK = PrintDialog1.ShowDialog() Then
                PrintDocument1.DocumentName = "Test Page Print"
                PrintDocument1.Print()
            End If
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.WindowState = FormWindowState.Maximized
            PrintPreviewDialog1.ShowDialog()
        End Sub
    
        Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Try
                '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 DataGridView1.Columns
                        iTmpWidth = CInt(Math.Floor(CDbl(CDbl(GridCol.Width) / CDbl(iTotalWidth) * CDbl(iTotalWidth) * (CDbl(e.MarginBounds.Width) / CDbl(iTotalWidth)))))
    
                        iHeaderHeight = CInt(e.Graphics.MeasureString(GridCol.HeaderText, GridCol.InheritedStyle.Font, iTmpWidth).Height) + 11
    
                        ' Save width and height of headers
                        arrColumnLefts.Add(iLeftMargin)
                        arrColumnWidths.Add(iTmpWidth)
                        iLeftMargin += iTmpWidth
                    Next
                End If
                'Loop till all the grid rows not get printed
                While iRow <= DataGridView1.Rows.Count - 1
                    Dim GridRow As DataGridViewRow = DataGridView1.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
                            e.Graphics.DrawString("Customer Summary", New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString("Customer Summary", New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)
    
                            Dim strDate As [String] = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString()
                            'Draw Date
                            e.Graphics.DrawString(strDate, New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(strDate, New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top - e.Graphics.MeasureString("Customer Summary", New Font(New Font(DataGridView1.Font, FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13)
    
                            'Draw Columns                 
                            iTopMargin = e.MarginBounds.Top
                            For Each GridCol As DataGridViewColumn In DataGridView1.Columns
                                e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New Rectangle(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iHeaderHeight))
    
                                e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iHeaderHeight))
    
                                e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, New SolidBrush(GridCol.InheritedStyle.ForeColor), New RectangleF(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iHeaderHeight), strFormat)
                                iCount += 1
                            Next
                            bNewPage = False
                            iTopMargin += iHeaderHeight
                        End If
                        iCount = 0
                        'Draw Columns Contents                
                        For Each Cel As DataGridViewCell In GridRow.Cells
                            If Cel.Value IsNot Nothing Then
                                e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font, New SolidBrush(Cel.InheritedStyle.ForeColor), New RectangleF(CInt(arrColumnLefts(iCount)), CSng(iTopMargin), CInt(arrColumnWidths(iCount)), CSng(iCellHeight)), strFormat)
                            End If
                            'Drawing Cells Borders 
                            e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iCellHeight))
                            iCount += 1
                        Next
                    End If
                    iRow += 1
                    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
    
        Private Sub PrintDocument1_BeginPrint(sender As Object, e As Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
            Try
                strFormat = New StringFormat()
                strFormat.Alignment = StringAlignment.Near
                strFormat.LineAlignment = StringAlignment.Center
                strFormat.Trimming = StringTrimming.EllipsisCharacter
    
                arrColumnLefts.Clear()
                arrColumnWidths.Clear()
                iCellHeight = 0
                iRow = 0
                bFirstPage = True
                bNewPage = True
    
                ' Calculating Total Widths
                iTotalWidth = 0
                For Each dgvGridCol As DataGridViewColumn In DataGridView1.Columns
                    iTotalWidth += dgvGridCol.Width
                Next
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            End Try
        End Sub



    • Edited by ahmeddc Monday, November 13, 2017 5:43 AM
    Monday, November 13, 2017 5:42 AM

Answers