【问题标题】:VB.NET - how to add a title on top of printed page in datagridview?VB.NET - 如何在 datagridview 的打印页面顶部添加标题?
【发布时间】:2017-06-16 02:16:38
【问题描述】:

我在 VB.net 2010 上有一个 windows 应用程序,里面有一个 datagridview,

一切正常,打印也很好,但我想在打印的页面上添加一个标题!从我拥有的文本框中,有可能吗?

请尽快重播,附上我的 PrintDocument 代码!

With DataGridView1
        Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
        fmt.LineAlignment = StringAlignment.Center
        fmt.Trimming = StringTrimming.EllipsisCharacter
        Dim y As Single = e.MarginBounds.Top
        Do While mRow < .RowCount
            Dim row As DataGridViewRow = .Rows(mRow)
            Dim x As Single = e.MarginBounds.Left
            Dim h As Single = 0
            For Each cell As DataGridViewCell In row.Cells
                Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
                e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
                If (newpage) Then
                    e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
                Else
                    e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
                End If
                x += rc.Width
                h = Math.Max(h, rc.Height)
            Next
            newpage = False
            y += h
            mRow += 1
            If y + h > e.MarginBounds.Bottom Then
                e.HasMorePages = True
                mRow -= 1
                newpage = True
                Exit Sub
            End If
        Loop
        mRow = 0
    End With

另外,我可以从页面顶部调整空间吗? 提前谢谢^^

【问题讨论】:

    标签: vb.net printdocument


    【解决方案1】:

    此代码可能有效,请尝试。但它和你的代码有点不同。

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
    
    With Printer
    Printer.Title = "Your Title" 'You can change this by TextBox1.text
    Printer.SubTitle = "Your Subtitle" 'You can change this by TextBox2.text
    Printer.SubTitleFormatFlags = StringFormatFlags.LineLimit Or _
    StringFormatFlags.NoClip
    Printer.PageNumbers = True
    Printer.PageNumberInHeader = False
    Printer.PorportionalColumns = True
    Printer.HeaderCellAlignment = StringAlignment.Near
    Printer.Footer = "Your Footer"
    Printer.FooterSpacing = 15
    Printer.PrintDataGridView(Me.DataGridView1)
    End With
    

    结束子

    【讨论】:

    • 打印机未声明,我的 vb.net 说 =(
    • 试试第二个答案。
    【解决方案2】:

    如果上面的代码不起作用,你可以试试这个。

    Private PrintGrid As DataGridViewPrint
    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
    Dim fpr As New frmPrint()
    With fpr
    .Title = DataGridView1.CaptionText
    .ShowDialog()
    If .Result > 0 Then
    PrintGrid = New DataGridViewPrint(PrintDocument1, DataGridView1, .bBlackWhite)
    PrintGrid.PrintTitle = .bTitle
    PrintGrid.Title = .Title
    Select Case .Result
    Case 1 ' Print
    ' The Print method prints the DataGridView without using a print dialog.
    ' Use a PrintDialog when you want to offer the user the ability to choose print settings.
    If PrintDialog1.ShowDialog() = DialogResult.OK Then PrintDocument1.Print()
    Case 2 ' Page Setup
    PageSetupDialog1.ShowDialog()
    Case 3 ' Preview
    PrintPreviewDialog1.Icon = fpr.Icon
    PrintPreviewDialog1.ShowDialog()
    End Select
    End If
    End With
    End Sub
    
    ' Specify the output to print by handling the PrintPage event 
    ' and by using the Graphics included in the PrintPageEventArgs.
    Private Sub printDocument1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
    ' Print method of DataGridViewPrint class starts the custom DataGridView's printing process.
    e.HasMorePages = PrintGrid.Print(e.Graphics)
    End Sub
    

    【讨论】:

      【解决方案3】:

      我发现这个解决方案很有用!

      尝试将此添加到 Printducoment 方法中:

      ' Create string to draw.
      Dim drawString As [String] = "Sample Text"
      
      ' Create font and brush.
      Dim drawFont As New Font("Arial", 16)
      Dim drawBrush As New SolidBrush(Color.Black)
      
      ' Create rectangle for drawing.
      Dim x As Single = 150.0F
      Dim y As Single = 150.0F
      Dim width As Single = 200.0F
      Dim height As Single = 50.0F
      Dim drawRect As New RectangleF(x, y, width, height)
      
      ' Draw rectangle to screen.
      Dim blackPen As New Pen(Color.Black)
      e.Graphics.DrawRectangle(blackPen, x, y, width, height)
      
      ' Set format of string.
      Dim drawFormat As New StringFormat
      drawFormat.Alignment = StringAlignment.Center
      
      ' Draw string to screen.
      e.Graphics.DrawString(drawString, drawFont, drawBrush, _
      drawRect, drawFormat)
      

      在我的 Datagrideview 页面上绘制文本时效果很好 ^^

      感谢“mark ashraf”的回答

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-22
        • 1970-01-01
        • 2023-03-24
        • 2016-04-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多