打印DataGridView时出现问题

时间:2014-12-10 04:21:08

标签: vb.net printing datagridview

您好我使用以下代码打印datagridview。它可以工作,但它会打印每一列,甚至是在表单上看不到的列。有没有办法让它只打印可见的列。感谢。

Private Structure pageDetails
    Dim columns As Integer
    Dim rows As Integer
    Dim startCol As Integer
    Dim startRow As Integer
End Structure

Private pages As Dictionary(Of Integer, pageDetails)
Dim maxPagesWide As Integer
Dim maxPagesTall As Integer

Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
    ''this removes the printed page margins
    PrintDocument1.OriginAtMargins = True
    PrintDocument1.DefaultPageSettings.Margins = New Drawing.Printing.Margins(0, 0, 0, 0)

    pages = New Dictionary(Of Integer, pageDetails)

    Dim maxWidth As Integer = CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width) - 40
    Dim maxHeight As Integer = CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Height) - 40 + Label1.Height

    Dim pageCounter As Integer = 0
    pages.Add(pageCounter, New pageDetails)

    Dim columnCounter As Integer = 0

    Dim columnSum As Integer = DataGridView1.RowHeadersWidth

    For c As Integer = 0 To DataGridView1.Columns.Count - 1
        If columnSum + DataGridView1.Columns(c).Width < maxWidth Then
            columnSum += DataGridView1.Columns(c).Width
            columnCounter += 1
        Else
            pages(pageCounter) = New pageDetails With {.columns = columnCounter, .rows = 0, .startCol = pages(pageCounter).startCol}
            columnSum = DataGridView1.RowHeadersWidth + DataGridView1.Columns(c).Width
            columnCounter = 1
            pageCounter += 1
            pages.Add(pageCounter, New pageDetails With {.startCol = c})
        End If
        If c = DataGridView1.Columns.Count - 1 Then
            If pages(pageCounter).columns = 0 Then
                pages(pageCounter) = New pageDetails With {.columns = columnCounter, .rows = 0, .startCol = pages(pageCounter).startCol}
            End If
        End If
    Next

    maxPagesWide = pages.Keys.Max + 1

    pageCounter = 0

    Dim rowCounter As Integer = 0

    Dim rowSum As Integer = DataGridView1.ColumnHeadersHeight

    For r As Integer = 0 To DataGridView1.Rows.Count - 2
        If rowSum + DataGridView1.Rows(r).Height < maxHeight Then
            rowSum += DataGridView1.Rows(r).Height
            rowCounter += 1
        Else
            pages(pageCounter) = New pageDetails With {.columns = pages(pageCounter).columns, .rows = rowCounter, .startCol = pages(pageCounter).startCol, .startRow = pages(pageCounter).startRow}
            For x As Integer = 1 To maxPagesWide - 1
                pages(pageCounter + x) = New pageDetails With {.columns = pages(pageCounter + x).columns, .rows = rowCounter, .startCol = pages(pageCounter + x).startCol, .startRow = pages(pageCounter).startRow}
            Next

            pageCounter += maxPagesWide
            For x As Integer = 0 To maxPagesWide - 1
                pages.Add(pageCounter + x, New pageDetails With {.columns = pages(x).columns, .rows = 0, .startCol = pages(x).startCol, .startRow = r})
            Next

            rowSum = DataGridView1.ColumnHeadersHeight + DataGridView1.Rows(r).Height
            rowCounter = 1
        End If
        If r = DataGridView1.Rows.Count - 2 Then
            For x As Integer = 0 To maxPagesWide - 1
                If pages(pageCounter + x).rows = 0 Then
                    pages(pageCounter + x) = New pageDetails With {.columns = pages(pageCounter + x).columns, .rows = rowCounter, .startCol = pages(pageCounter + x).startCol, .startRow = pages(pageCounter + x).startRow}
                End If
            Next
        End If
    Next

    maxPagesTall = pages.Count \ maxPagesWide

End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim rect As New Rectangle(20, 20, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label1.Height)
    Dim sf As New StringFormat
    sf.Alignment = StringAlignment.Center
    sf.LineAlignment = StringAlignment.Center

    e.Graphics.DrawString(Label1.Text, Label1.Font, Brushes.Black, rect, sf)

    sf.Alignment = StringAlignment.Near

    Dim startX As Integer = 50
    Dim startY As Integer = rect.Bottom

    Static startPage As Integer = 0

    For p As Integer = startPage To pages.Count - 1
        Dim cell As New Rectangle(startX, startY, DataGridView1.RowHeadersWidth, DataGridView1.ColumnHeadersHeight)
        e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
        e.Graphics.DrawRectangle(Pens.Black, cell)

        startY += DataGridView1.ColumnHeadersHeight

        For r As Integer = pages(p).startRow To pages(p).startRow + pages(p).rows - 1
            cell = New Rectangle(startX, startY, DataGridView1.RowHeadersWidth, DataGridView1.Rows(r).Height)
            e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
            e.Graphics.DrawRectangle(Pens.Black, cell)
            e.Graphics.DrawString(DataGridView1.Rows(r).HeaderCell.Value, DataGridView1.Font, Brushes.Black, cell, sf)
            startY += DataGridView1.Rows(r).Height
        Next

        startX += cell.Width
        startY = rect.Bottom

        For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
            cell = New Rectangle(startX, startY, DataGridView1.Columns(c).Width, DataGridView1.ColumnHeadersHeight)
            e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
            e.Graphics.DrawRectangle(Pens.Black, cell)
            e.Graphics.DrawString(DataGridView1.Columns(c).HeaderCell.Value, DataGridView1.Font, Brushes.Black, cell, sf)
            startX += DataGridView1.Columns(c).Width
        Next

        startY = rect.Bottom + DataGridView1.ColumnHeadersHeight

        For r As Integer = pages(p).startRow To pages(p).startRow + pages(p).rows - 1
            startX = 50 + DataGridView1.RowHeadersWidth
            For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
                cell = New Rectangle(startX, startY, DataGridView1.Columns(c).Width, DataGridView1.Rows(r).Height)
                e.Graphics.DrawRectangle(Pens.Black, cell)
                e.Graphics.DrawString(DataGridView1(c, r).Value, DataGridView1.Font, Brushes.Black, cell, sf)
                startX += DataGridView1.Columns(c).Width
            Next
            startY += DataGridView1.Rows(r).Height
        Next

        If p <> pages.Count - 1 Then
            startPage = p + 1
            e.HasMorePages = True
            Return
        Else
            startPage = 0
        End If

    Next

End Sub

Private Sub PrintAMToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PrintAMToolStripMenuItem.Click
    PrintDocument1.DefaultPageSettings.Landscape = True
    PrintDocument1.Print()
End Sub

1 个答案:

答案 0 :(得分:0)

我在PrintDocument1_PrintPage和PrintDocument1_BeginPrint事件中添加了If条件,以检查每次针对列的循环时列是否可见:

Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint

    ''this removes the printed page margins
    PrintDocument1.OriginAtMargins = True
    PrintDocument1.DefaultPageSettings.Margins = New Drawing.Printing.Margins(0, 0, 0, 0)

    pages = New Dictionary(Of Integer, pageDetails)

    Dim maxWidth As Integer = CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width) - 40
    Dim maxHeight As Integer = CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Height) - 40 + Label1.Height

    Dim pageCounter As Integer = 0
    pages.Add(pageCounter, New pageDetails)

    Dim columnCounter As Integer = 0

    Dim columnSum As Integer = DataGridView1.RowHeadersWidth

    For c As Integer = 0 To DataGridView1.Columns.Count - 1

        If columnSum + DataGridView1.Columns(c).Width < maxWidth Then
            columnSum += DataGridView1.Columns(c).Width
            columnCounter += 1
        Else
            If DataGridView1.Columns(c).Visible = True Then
                pages(pageCounter) = New pageDetails With {.columns = columnCounter, .rows = 0, .startCol = pages(pageCounter).startCol}
                columnSum = DataGridView1.RowHeadersWidth + DataGridView1.Columns(c).Width
                columnCounter = 1
                pageCounter += 1
                pages.Add(pageCounter, New pageDetails With {.startCol = c})
            End If
        End If
        If c = DataGridView1.Columns.Count - 1 Then

            If pages(pageCounter).columns = 0 Then
                pages(pageCounter) = New pageDetails With {.columns = columnCounter, .rows = 0, .startCol = pages(pageCounter).startCol}
            End If

        End If

    Next

    maxPagesWide = pages.Keys.Max + 1

    pageCounter = 0

    Dim rowCounter As Integer = 0

    Dim rowSum As Integer = DataGridView1.ColumnHeadersHeight

    For r As Integer = 0 To DataGridView1.Rows.Count - 2
        If rowSum + DataGridView1.Rows(r).Height < maxHeight Then
            rowSum += DataGridView1.Rows(r).Height
            rowCounter += 1
        Else
            pages(pageCounter) = New pageDetails With {.columns = pages(pageCounter).columns, .rows = rowCounter, .startCol = pages(pageCounter).startCol, .startRow = pages(pageCounter).startRow}
            For x As Integer = 1 To maxPagesWide - 1
                pages(pageCounter + x) = New pageDetails With {.columns = pages(pageCounter + x).columns, .rows = rowCounter, .startCol = pages(pageCounter + x).startCol, .startRow = pages(pageCounter).startRow}
            Next

            pageCounter += maxPagesWide
            For x As Integer = 0 To maxPagesWide - 1
                pages.Add(pageCounter + x, New pageDetails With {.columns = pages(x).columns, .rows = 0, .startCol = pages(x).startCol, .startRow = r})
            Next

            rowSum = DataGridView1.ColumnHeadersHeight + DataGridView1.Rows(r).Height
            rowCounter = 1
        End If
        If r = DataGridView1.Rows.Count - 2 Then
            For x As Integer = 0 To maxPagesWide - 1
                If pages(pageCounter + x).rows = 0 Then
                    pages(pageCounter + x) = New pageDetails With {.columns = pages(pageCounter + x).columns, .rows = rowCounter, .startCol = pages(pageCounter + x).startCol, .startRow = pages(pageCounter + x).startRow}
                End If
            Next
        End If
    Next

    maxPagesTall = pages.Count \ maxPagesWide

End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim rect As New Rectangle(20, 20, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label1.Height)
    Dim sf As New StringFormat
    sf.Alignment = StringAlignment.Center
    sf.LineAlignment = StringAlignment.Center

    e.Graphics.DrawString(Label1.Text, Label1.Font, Brushes.Black, rect, sf)

    sf.Alignment = StringAlignment.Near

    Dim startX As Integer = 50
    Dim startY As Integer = rect.Bottom

    Static startPage As Integer = 0

    For p As Integer = startPage To pages.Count - 1
        Dim cell As New Rectangle(startX, startY, DataGridView1.RowHeadersWidth, DataGridView1.ColumnHeadersHeight)
        e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
        e.Graphics.DrawRectangle(Pens.Black, cell)

        startY += DataGridView1.ColumnHeadersHeight

        For r As Integer = pages(p).startRow To pages(p).startRow + pages(p).rows - 1

            cell = New Rectangle(startX, startY, DataGridView1.RowHeadersWidth, DataGridView1.Rows(r).Height)
            e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
            e.Graphics.DrawRectangle(Pens.Black, cell)
            e.Graphics.DrawString(DataGridView1.Rows(r).HeaderCell.Value, DataGridView1.Font, Brushes.Black, cell, sf)
            startY += DataGridView1.Rows(r).Height
        Next

        startX += cell.Width
        startY = rect.Bottom

        For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
            If DataGridView1.Columns(c).Visible = True Then
                cell = New Rectangle(startX, startY, DataGridView1.Columns(c).Width, DataGridView1.ColumnHeadersHeight)
                e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
                e.Graphics.DrawRectangle(Pens.Black, cell)
                e.Graphics.DrawString(DataGridView1.Columns(c).HeaderCell.Value, DataGridView1.Font, Brushes.Black, cell, sf)
                startX += DataGridView1.Columns(c).Width
            End If

        Next

        startY = rect.Bottom + DataGridView1.ColumnHeadersHeight

        For r As Integer = pages(p).startRow To pages(p).startRow + pages(p).rows - 1
            startX = 50 + DataGridView1.RowHeadersWidth
            For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
                If DataGridView1.Columns(c).Visible = True Then
                    cell = New Rectangle(startX, startY, DataGridView1.Columns(c).Width, DataGridView1.Rows(r).Height)
                    e.Graphics.DrawRectangle(Pens.Black, cell)
                    e.Graphics.DrawString(DataGridView1(c, r).Value, DataGridView1.Font, Brushes.Black, cell, sf)
                    startX += DataGridView1.Columns(c).Width
                End If
            Next
            startY += DataGridView1.Rows(r).Height
        Next

        If p <> pages.Count - 1 Then
            startPage = p + 1
            e.HasMorePages = True
            Return
        Else
            startPage = 0
        End If

    Next

End Sub