多页中的VB .Net打印数据网格

时间:2014-09-02 14:31:29

标签: .net vb.net printing datagrid

我是VB .Net的新手,这个网站给了我很多帮助。我想从我的数据网格打印数据,但打印不正确。

使用此代码,它将仅打印1页并打印适合一页的内容。其余不适合第一页的内容将不会被打印出来。

If gridPTrans.RowCount > 0 Then 'Datagrid is not empty
    y = y + 25 'Height
    With gridPTrans
        Do While mRow < .RowCount 'Loop datagrid
            If mRow = .RowCount - 1 Then
                Exit Sub
            End If

            row = .Rows(mRow)
            drawPoint = New PointF(x, y)

            For Each cell As DataGridViewCell In row.Cells
                MessageBox.Show("Y: " & y)
                strValue = (intValue.ToString + ".  " + gridPTrans.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString())
                e.Graphics.DrawString(strValue, drawFont, drawBrush, drawPoint)
                intValue += 1
                MessageBox.Show(" " & strValue)
            Next
            y += 18
            mRow += 1
        Loop 
        mRow = 0
    End With 
End If 

If y > e.MarginBounds.Bottom Then
    e.HasMorePages = True
    MessageBox.Show("e.HasMorePages: " & e.HasMorePages)
    y = (e.MarginBounds.Top) / 2
    Exit Sub
End If

我尝试了不同的代码。使用此代码,它将打印第一页的无限页面。

If gridPTrans.RowCount > 0 Then 'TRANSFER is not empty
    y = y + 25
    With gridPTrans
        Do While mRow < .RowCount 'Loop datagrid
            If mRow = .RowCount - 1 Then
                Exit Sub
            End If

            row = .Rows(mRow)
            drawPoint = New PointF(x, y)

            If y > e.MarginBounds.Bottom Then
                e.HasMorePages = True
                MessageBox.Show("e.HasMorePages: " & e.HasMorePages)
                y = (e.MarginBounds.Top) / 2
                Exit Sub
            End If

            'Print datagrid
            For Each cell As DataGridViewCell In row.Cells
                MessageBox.Show("Y: " & y)
                strValue = (intValue.ToString + ".  " + gridPTrans.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString())
                e.Graphics.DrawString(strValue, drawFont, drawBrush, drawPoint)
                intValue += 1
                MessageBox.Show(" " & strValue)
            Next
            y += 18
            mRow += 1
        Loop
        mRow = 0    
    End With 
End If 

0 个答案:

没有答案