如何在VB.NET中使用datagridview中的printdocument1添加页眉和页脚以及打印和日期的时间 [英] How to add header and footer and time of print and date using printdocument1 from datagridview in VB.NET

查看:81
本文介绍了如何在VB.NET中使用datagridview中的printdocument1添加页眉和页脚以及打印和日期的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我有一个使用Visual Studio 2015创建的应用程序它工作得很好但是当我想从datgridview打印数据时,打印日期和标题和页脚

使用此代码

 私有 mRow  As  整数 =  0  
私人新网作为 布尔 =



'''''

和Printdocument活动

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


'设置为长文本显示'...'
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因为Int32 = 0
Dim row As DataGridViewRow

'打印新页面的标题文本
'使用灰色bg就像控件
如果newpage那么
row = DataGridView1.Rows(mRow)
x = e.MarginBounds.Left
For Each cell As DataGridViewCell In row.Cells
',因为我们打印控件的视图,
'跳过不可见的列
如果是cell.Visible那么
rc =新的矩形(x,y,cell.Size.Width,cell.Size.Height)

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

'重复使用数据pront - 应该是一个函数
选择Case DataGridView1.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)
结束选择

e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex) .HeaderText,
DataGridView1.Font,Brus hes.Black,rc,fmt)
x + = rc.Width
h = Math.Max(h,rc.Height)
结束如果
下一个
y + = h

结束如果
newpage = False

'现在打印每行数据
Dim thisNDX As Int32
for thisNDX = mRow To DataGridView1.RowCount - 1
'无需尝试打印新行
如果DataGridView1.Rows(thisNDX).IsNewRow则退出

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

'重置X表示数据
x = e.MarginBounds.Left

'打印数据
For Each cell As DataGridViewCell In row.Cells
If cell.Visible Then
rc = New Rectangle(x,y,cell.Size.Width,cell.Size.Height)

'样本代码:如何
'up RowPrePaint规则
'如果Convert.ToDecimal(row.Cells(5).Value)< 9.99然后
'使用br作为新的SolidBrush(Color.MistyRose)
'e.Graphics.FillRectangle(br,rc)
'结束使用
'结束如果

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

选择Case DataGridView1.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 = StringAlignm ent.Near
rc.Offset(2,0)
结束选择

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

x + = rc.Width
h = Math.Max(h,rc.Height)
结束如果

下一个
y + = h
'下一行打印
mRow = thisNDX + 1

如果y + h> e.MarginBounds.Bottom然后
e.HasMorePages = True
'mRow - = 1导致最后一行在下一页重新打印
newpage = True
返回
结束If
下一步



和此处的事件

 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



和打印按钮

私有子按钮1_Click(发件人为对象,e为EventArgs)处理Button1.Click 
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub





< b>我尝试了什么:



代码已经附带示例

解决方案

如果你想添加页眉和页脚,你必须记住:

1.计算你需要保留的距离页眉和页脚

2.打印标题

3.从datagridview打印数据(不要忘记测量文本页脚所需的空间)

4.打印页脚

5.如果还有要打印的内容,请转到下一页



 < span class =code-keyword> Dim  linesPerPage  As   Single  =  0  
Dim line As 字符串 = 没有
Dim count 作为 = 0

' 计算每页的行数。
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
linesPerPage - = 4 ' 1表示标题和页脚和1表示文本和页眉/页脚之间的空格

' print header
ev.Graphics.DrawString( 标题文字在这里,printFont ,Brushes.Black,leftMargin,yPos, New StringFormat())
ev.Graphics.DrawString( ---,printFont,Brushes.Black,leftMargin,yPos, StringFormat() )

' 打印数据
虽然 count< linesPerPage
' 打印datagridview数据的逻辑就在这里
line = DataGridView。行(计数)。细胞( 0 )。值' 仅获取第一列数据
ev.Graphics.DrawString(line,printFont,Brushes.Black,leftMargin,yPos, New StringFormat())
count + = 1
结束 虽然

' print footer
ev .Graphics.DrawString( ---,printFont,Brushes.Black,leftMargin,yPos,< span class =code-keyword> New
StringFormat())
ev.Graphics.DrawString( 页脚的文字在这里,printFont,Brushes.Black,leftMargin,yPos, New StringFormat())

' 如果存在更多行,则打印另一页。
如果(行< span class =code-keyword> IsNot Nothing 然后
ev .HasMorePages = True
否则
ev.HasMorePages = 错误
结束 如果





详情请见: PrintDocument.PrintPage Event(System.Drawing.Printing) [ ^ ]

打印DataGridView [ ^ ]


Hello
I has an application created With Visual Studio 2015 it's worked Fine but in need when i want to print the data from datgridview with date of print and header and footer
iam using This Code

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


'''''''
And In Printdocument event

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.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 = DataGridView1.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 DataGridView1.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(DataGridView1.Columns(cell.ColumnIndex).HeaderText,
                                            DataGridView1.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 DataGridView1.RowCount - 1
            ' no need to try to print the new row
            If DataGridView1.Rows(thisNDX).IsNewRow Then Exit For

            row = DataGridView1.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 DataGridView1.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(),
                                      DataGridView1.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


and event here

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


and button to print

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      PrintPreviewDialog1.Document = PrintDocument1
      PrintPreviewDialog1.ShowDialog()
  End Sub



What I have tried:

The Code Already Attached with the Example

解决方案

If you would like to add header and footer, you have to remember this:
1. calculate the distance that you need to reserve for the header and footer
2. print header
3. print data from datagridview (do not forget measuring text the space you need for footer)
4. print footer
5. go to next page if there's still something to print

Dim linesPerPage As Single = 0
Dim line As String = Nothing
Dim count As Long = 0

' Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
linesPerPage -= 4 '1 for header and footer and 1 for space between text and header/footer

'print header
ev.Graphics.DrawString("Header's text goes here", printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
ev.Graphics.DrawString("---", printFont, Brushes.Black, leftMargin, yPos, New StringFormat())

'print data
While count < linesPerPage
    'your logic to print datagridview data goes here
    line = DataGridView.Rows(count).Cell(0).Value 'gets first column data only
    ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
    count += 1
End While

'print footer
ev.Graphics.DrawString("---", printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
ev.Graphics.DrawString("Footer's text goes here", printFont, Brushes.Black, leftMargin, yPos, New StringFormat())

' If more lines exist, print another page.
If (line IsNot Nothing) Then
    ev.HasMorePages = True
Else
    ev.HasMorePages = False
End If



For further details, please see: PrintDocument.PrintPage Event (System.Drawing.Printing)[^]
Printing of DataGridView[^]


这篇关于如何在VB.NET中使用datagridview中的printdocument1添加页眉和页脚以及打印和日期的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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