当我关闭打印预览对话框时,DGV标头消失 [英] DGV Header disappears when I close print preview dialog

查看:52
本文介绍了当我关闭打印预览对话框时,DGV标头消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在系统中有一个打印预览对话框控件,我希望它打印我的应用程序中具有的DGV表.我可以打印表格而没有任何问题,但是有一个非常奇怪的问题.

I have a print preview dialog control in a system and I want it to print a DGV table that I have in my application. I'm able to print the table without any problems, but there is a very curious problem.

运行应用程序并打开打印预览控件后,我得到.我意识到在关闭打印预览对话框并在系统中闲逛之后重新打开它,是我得到的.有人知道为什么会这样吗?

After I run the application and open the print preview control, I get this. I realize after closing the print preview dialog and reopening it after meddling around in the system, this is what I get. Does anyone know why this is happening?

这些是我正在使用的代码.

These are the codes I'm using.

    Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
    fmt.LineAlignment = StringAlignment.Center
    fmt.Trimming = StringTrimming.EllipsisCharacter
    Dim y As Single = e.MarginBounds.Top
    Dim rc As Rectangle
    Dim x As Int32
    Dim h As Int32 = 0

    Do While mRow < dgvChemical.RowCount
        Dim row As DataGridViewRow = dgvChemical.Rows(mRow)
        x = e.MarginBounds.Left
        h = 0

        If newPage Then
            For Each cell As DataGridViewCell In row.Cells
                rc = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)
                e.Graphics.FillRectangle(Brushes.LightGray, rc)
                e.Graphics.DrawRectangle(Pens.Black, rc)
                e.Graphics.DrawString(dgvChemical.Columns(cell.ColumnIndex).HeaderText, dgvChemical.Font, Brushes.Black, rc, fmt)
                x += rc.Width
                h = Math.Max(h, rc.Height)
            Next
            y += h
            mRow += 0
        End If
        newPage = False

        x = e.MarginBounds.Left

        For Each cell As DataGridViewCell In row.Cells
            rc = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)
            e.Graphics.DrawRectangle(Pens.Black, rc)
            e.Graphics.DrawString(cell.FormattedValue.ToString(), dgvChemical.Font, Brushes.Black, rc, fmt)

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

        y += h
        mRow += 1
        If y + h > e.MarginBounds.Bottom Then
            e.HasMorePages = True
            newPage = True
            Return
        End If
    Loop

    mRow = 0

    Dim ps As PaperSize
    For ix As Integer = 0 To PrintDocument1.PrinterSettings.PaperSizes.Count - 1
        If PrintDocument1.PrinterSettings.PaperSizes(ix).Kind = PaperKind.A3 Then
            ps = PrintDocument1.PrinterSettings.PaperSizes(ix)
            PrintDocument1.DefaultPageSettings.PaperSize = ps
            PageSetupDialog1.PageSettings.PaperSize = ps
        End If
    Next`

推荐答案

上一篇文章的答案包括一个项目符号指向,请确保在单击按钮开始打印时 mRow newpage 被重置.否则,这些表单/类级别的变量将以上次会话中的值作为开头.

The answer to your previous post includes a bullet point to be sure mRow and newpage are reset in the button click which starts the printing. Otherwise those form/class level variables will start with whatever value they were from the last session.

您可能还想重置起始页.如果用户在显示第17页的情况下离开了该页面,则下次也将在该页面上开始.这可能是合乎要求的,也可能是不合要求的.

You might also want to reset the start page. If the user left it with page 17 showing, it will also start on that page the next time. That may or may not be desirable.

Private Sub printDGV_Click(sender As Object, e As EventArgs) Handles printDGV.Click
    ' need to start fresh eash time
    mRow = 0
    newpage = True

    PrintPreviewDialog1.Document = PrintDocument1
    ' optionally reset the first page shown
    PrintPreviewDialog1.PrintPreviewControl.StartPage = 0
    PrintPreviewDialog1.ShowDialog()

End Sub

这篇关于当我关闭打印预览对话框时,DGV标头消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆