如何在vb.net中打印带有标题的datagridview表?

时间:2016-12-07 10:42:01

标签: .net vb.net datagridview printdocument

我正在开发的系统中创建一个打印预览功能,它将预览我要打印的datagridview。我使用ooopsoft的codes作为参考,除了一个小问题之外它工作正常。

问题:

enter image description here

在您可以看到缺少序列号为1的dgv行。看来标题已覆盖第1行。我已经尝试了无数种方法来解决它,但我仍然找不到解决方案。我尝试退出打印预览对话框并再次打开它,但this是我得到的结果。我想我错过了一行代码,但我无法弄清楚是什么。请帮忙。

1 个答案:

答案 0 :(得分:5)

original code是一个不错的开始,但有一些错误和效率低下:

  • 当有新页面时,它会使用newpage标记打印标题第一行。显然你真的希望它能做到这两点
  • 每页打印一次列标题,因此根本不需要在数据打印循环中
  • 不允许使用默认对齐以外的不可见列或列,您可能需要考虑其他此类设置。
  • 因为它实际上并没有打印正确的行数,所以一旦你修复了它,你会发现它重新打印上一页的最后一行作为新页面的第一行。
  • 有一个内部装订线或边距,因此文字不会太靠近网格线打印 - 这只是使用1或2的偏移量
  • 也不必要地使用singleRectangleF
  • 也没有为文档再次显示或打印做好准备。您还需要在按钮点击或mRow活动中重置newpageBeginPrint

我添加了一些注释,并为标题行着色并演示了如何实现RowPrePaint规则之类的内容。

Private mRow As Integer = 0
Private newpage As Boolean = True

Private Sub PrintDocument1_PrintPage(sender As System.Object,
                    e As PrintPageEventArgs) Handles PrintDocument1.PrintPage

    ' sets it to show '...' for long text
    Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
    fmt.LineAlignment = StringAlignment.Center
    fmt.Trimming = StringTrimming.EllipsisCharacter
    Dim y As Int32 = e.MarginBounds.Top
    Dim rc As Rectangle
    Dim x As Int32
    Dim h As Int32 = 0
    Dim row As DataGridViewRow

    ' print the header text for a new page
    '   use a grey bg just like the control
    If newpage Then
        row = dgvZZ.Rows(mRow)
        x = e.MarginBounds.Left
        For Each cell As DataGridViewCell In row.Cells
            ' since we are printing the control's view,
            ' skip invidible columns
            If cell.Visible Then
                rc = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)

                e.Graphics.FillRectangle(Brushes.LightGray, rc)
                e.Graphics.DrawRectangle(Pens.Black, rc)

                ' reused in the data pront - should be a function
                Select Case dgvZZ.Columns(cell.ColumnIndex).DefaultCellStyle.Alignment
                    Case DataGridViewContentAlignment.BottomRight,
                         DataGridViewContentAlignment.MiddleRight
                        fmt.Alignment = StringAlignment.Far
                        rc.Offset(-1, 0)
                    Case DataGridViewContentAlignment.BottomCenter,
                        DataGridViewContentAlignment.MiddleCenter
                        fmt.Alignment = StringAlignment.Center
                    Case Else
                        fmt.Alignment = StringAlignment.Near
                        rc.Offset(2, 0)
                End Select

                e.Graphics.DrawString(dgvZZ.Columns(cell.ColumnIndex).HeaderText,
                                            dgvZZ.Font, Brushes.Black, rc, fmt)
                x += rc.Width
                h = Math.Max(h, rc.Height)
            End If
        Next
        y += h

    End If
    newpage = False

    ' now print the data for each row
    Dim thisNDX As Int32
    For thisNDX = mRow To dgvZZ.RowCount - 1
        ' no need to try to print the new row
        If dgvZZ.Rows(thisNDX).IsNewRow Then Exit For

        row = dgvZZ.Rows(thisNDX)
        x = e.MarginBounds.Left
        h = 0

        ' reset X for data
        x = e.MarginBounds.Left

        ' print the data
        For Each cell As DataGridViewCell In row.Cells
            If cell.Visible Then
                rc = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)

                ' SAMPLE CODE: How To 
                ' up a RowPrePaint rule
                'If Convert.ToDecimal(row.Cells(5).Value) < 9.99 Then
                '    Using br As New SolidBrush(Color.MistyRose)
                '        e.Graphics.FillRectangle(br, rc)
                '    End Using
                'End If

                e.Graphics.DrawRectangle(Pens.Black, rc)

                Select Case dgvZZ.Columns(cell.ColumnIndex).DefaultCellStyle.Alignment
                    Case DataGridViewContentAlignment.BottomRight,
                         DataGridViewContentAlignment.MiddleRight
                        fmt.Alignment = StringAlignment.Far
                        rc.Offset(-1, 0)
                    Case DataGridViewContentAlignment.BottomCenter,
                        DataGridViewContentAlignment.MiddleCenter
                        fmt.Alignment = StringAlignment.Center
                    Case Else
                        fmt.Alignment = StringAlignment.Near
                        rc.Offset(2, 0)
                End Select

                e.Graphics.DrawString(cell.FormattedValue.ToString(),
                                      dgvZZ.Font, Brushes.Black, rc, fmt)

                x += rc.Width
                h = Math.Max(h, rc.Height)
            End If

        Next
        y += h
        ' next row to print
        mRow = thisNDX + 1

        If y + h > e.MarginBounds.Bottom Then
            e.HasMorePages = True
            ' mRow -= 1   causes last row to rePrint on next page
            newpage = True
            Return
        End If
    Next


End Sub

enter image description here enter image description here

请注意,有一个Id列设置为在DGV中不可见,Color列居中且Price左对齐 - 这些是从控件中拾取的所有设置。另请注意,文本稍微远离网格线。

上面的最后一个要点,您还需要在按钮点击或mRow事件中重置newpageBeginPrint表示:< / p>

Private Sub PrintDocument1_BeginPrint(sender As Object, 
          e As PrintEventArgs) Handles PrintDocument1.BeginPrint
    mRow = 0
    newpage = True
    PrintPreviewDialog1.PrintPreviewControl.StartPage = 0
    PrintPreviewDialog1.PrintPreviewControl.Zoom = 1.0
End Sub

预览后,mRow变量将指示已打印所有行。如果用户单击“打印”或返回另一个“预览”,则不会打印任何内容。此代码还会重置要显示的第一页和初始缩放。