打印数据网格视图跳过第一行

huangapple go评论65阅读模式
英文:

Print data grid view skips the first row

问题

我已为我想要打印的数据网格视图创建了打印预览。代码运行正常,但存在轻微问题。

问题截图:打印数据网格视图跳过第一行

在数据网格视图行中,ID 1 不见了,它总是从2开始。我该如何解决这个问题?请帮助。

我的代码:

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

    Dim mRow As Integer = 0
    Dim newpage As Boolean = True
    PrintDocument1.DefaultPageSettings.Landscape = True

    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
End Sub
英文:

I have created a print preview for the data grid view that I want to print. The code works fine but there is slight problem

Problem Screenshot:打印数据网格视图跳过第一行

In the data grid view row, the ID 1 is missing, it always starts from 2. How can I solve this problem? Please help.

My code:

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

    Dim mRow As Integer = 0
    Dim newpage As Boolean = True
    PrintDocument1.DefaultPageSettings.Landscape = True

    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 &lt; .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 &gt; e.MarginBounds.Bottom Then
                e.HasMorePages = True
                mRow -= 1
                newpage = True
                Exit Sub
            End If
        Loop
        mRow = 0
    End With
End Sub

答案1

得分: 0

If (newpage) 导致绘制标题而不是第一行。

您可以将部分更改为:

If not newpage Then
    mRow += 1
End If
newpage = False
y += h
英文:

The If (newpage) causes it to draw the headers instead of the first row.

You could change the section

newpage = False
y += h
mRow += 1

to

If not newpage Then
    mRow += 1
End If
newpage = False
y += h

答案2

得分: 0

rows(0) 是第一行数据,不是标题行,而 cell.RowIndex 为 1,所以将 rows(cell.RowIndex) 更改为 rows(cell.RowIndex-1):

e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex - 1).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
英文:

rows(0) is the first data row, not header row, while cell.RowIndex is 1, so change rows(cell.RowIndex) as rows(cell.RowIndex-1) :

e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex - 1).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt) 

答案3

得分: 0

'打印每个部分以避免此类问题。标题部分属于 [`Columns`][1] 集合,所以在处理 `Rows` 和 `Cells` 部分之前,先打印它。

您的代码中还有另一个问题需要修复。`mRow` 是一个 _本地_ 变量不保留最后打印的行以跳过或新行以继续的信息当您请求新页面时它必须是一个 _类_ 字段

```vb.net
Private mRow As Integer

Private Sub PrintPreviewButton(sender As Object, e As EventArgs) _
    Handles Button1.Click
    PrintDocument1.DefaultPageSettings.Landscape = True
    mRow = 0

    Using d = New PrintPreviewDialog With {
        .Document = PrintDocument1
    }
        d.ShowDialog(Me)
    End Using
End Sub

Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) _
    Handles PrintDocument1.PrintPage
    Dim g = e.Graphics
    Dim x As Integer = e.MarginBounds.X
    Dim y As Integer = e.MarginBounds.Y

    With DataGridView1
        Using sf = New StringFormat(StringFormat.GenericTypographic) With {
        .LineAlignment = StringAlignment.Center,
        .Trimming = StringTrimming.EllipsisCharacter
        }
            For Each col As DataGridViewColumn In .Columns
                Dim rect = New Rectangle(
                    x, y,
                    col.Width, col.DataGridView.ColumnHeadersHeight)

                g.DrawRectangle(Pens.Black, rect)
                rect.Inflate(-3, 0)
                g.DrawString(col.HeaderText, .Font, Brushes.Black, rect, sf)

                x += col.Width
            Next

            y += .ColumnHeadersHeight

            For i = mRow To .RowCount - 1
                Dim row = .Rows(i)

                If row.IsNewRow Then Exit Sub

                x = e.MarginBounds.X

                For Each cell As DataGridViewCell In row.Cells
                    Dim rect = New Rectangle(New Point(x, y), cell.Size)

                    g.DrawRectangle(Pens.Black, rect)
                    rect.Inflate(-3, 0)
                    g.DrawString(cell.FormattedValue?.ToString(),
                                    .Font, Brushes.Black, rect, sf)

                    x += cell.Size.Width
                Next cell

                y += row.Height

                If y + row.Height > e.MarginBounds.Bottom Then
                    mRow = i + 1
                    e.HasMorePages = True
                    Exit For
                End If
            Next i
        End Using
    End With
End Sub

<details>
<summary>英文:</summary>
Print each part separately to avoid this kind of problems. The headers part belongs to the [`Columns`][1] collection then print it first before you handle the `Rows` and `Cells` parts.
You have another problem in your code to fix. The `mRow` is a _local_ variable that does not preserve the last printed row to skip or the new row to continue with when you request a new page. It must be a _class_ field.
```vb.net
Private mRow As Integer
Private Sub PrintPreviewButton(sender As Object, e As EventArgs) _
Handles Button1.Click
PrintDocument1.DefaultPageSettings.Landscape = True
mRow = 0
Using d = New PrintPreviewDialog With {
.Document = PrintDocument1
}
d.ShowDialog(Me)
End Using
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) _
Handles PrintDocument1.PrintPage
Dim g = e.Graphics
Dim x As Integer = e.MarginBounds.X
Dim y As Integer = e.MarginBounds.Y
With DataGridView1
Using sf = New StringFormat(StringFormat.GenericTypographic) With {
.LineAlignment = StringAlignment.Center,
.Trimming = StringTrimming.EllipsisCharacter
}
For Each col As DataGridViewColumn In .Columns
Dim rect = New Rectangle(
x, y,
col.Width, col.DataGridView.ColumnHeadersHeight)
g.DrawRectangle(Pens.Black, rect)
rect.Inflate(-3, 0)
g.DrawString(col.HeaderText, .Font, Brushes.Black, rect, sf)
x += col.Width
Next
y += .ColumnHeadersHeight
For i = mRow To .RowCount - 1
Dim row = .Rows(i)
If row.IsNewRow Then Exit Sub
x = e.MarginBounds.X
For Each cell As DataGridViewCell In row.Cells
Dim rect = New Rectangle(New Point(x, y), cell.Size)
g.DrawRectangle(Pens.Black, rect)
rect.Inflate(-3, 0)
g.DrawString(cell.FormattedValue?.ToString(),
.Font, Brushes.Black, rect, sf)
x += cell.Size.Width
Next cell
y += row.Height
If y + row.Height &gt; e.MarginBounds.Bottom Then
mRow = i + 1
e.HasMorePages = True
Exit For
End If
Next i
End Using
End With
End Sub

huangapple
  • 本文由 发表于 2023年2月19日 18:04:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定