簡體   English   中英

打印 DataGridView 時如何隱藏列?

[英]How do I hide a column when printing a DataGridView?

我在 DataGridView 的所有行的最后一個“刪除”按鈕。 我想在打印時不顯示刪除按鈕。 我怎樣才能做到這一點? 另外我不需要顯示刪除按鈕的位置。

我嘗試將列的Visible屬性設置為False ,但這並沒有從打印輸出中隱藏該列。

Me.DataGridView1.Columns("Remove").Visible = False
With DataGridView1
    Using fmt As New StringFormat With {
        .Alignment = StringAlignment.Center,
        .LineAlignment = StringAlignment.Center,
        .Trimming = StringTrimming.EllipsisCharacter,
        .FormatFlags = StringFormatFlags.LineLimit Or StringFormatFlags.NoWrap
    }

        Dim y As Single = e.MarginBounds.Top

        Using headerFont As New Font("Arial", 12, FontStyle.Bold)

            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)
                    If newpage Then
                        e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText, headerFont, Brushes.Black, rc, fmt)  '
                    Else
                        e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), headerFont, 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
        End Using
    End Using
    mRow = 0
End With

如果您找到不想打印的列的索引,那么您可以在迭代單元格時檢查該索引並簡單地跳過該迭代,如下所示:

Me.DataGridView1.Columns("Remove").Visible = False

Dim skipRowNum = DataGridView1.Columns("Remove").Index

With DataGridView1
    Using fmt As New StringFormat With {
        .Alignment = StringAlignment.Center,
        .LineAlignment = StringAlignment.Center,
        .Trimming = StringTrimming.EllipsisCharacter,
        .FormatFlags = StringFormatFlags.LineLimit Or StringFormatFlags.NoWrap
    }

        Dim y As Single = e.MarginBounds.Top

        Using headerFont As New Font("Arial", 12, FontStyle.Bold)

            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
                    If cell.ColumnIndex = skipRowNum Then
                        Continue For
                    End If

                    Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
                    If newPage Then
                        e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText, headerFont, Brushes.Black, rc, fmt)  '
                    Else
                        e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), headerFont, 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
        End Using
    End Using
    mRow = 0
End With

您可以通過使用 List(Of Integer) 為要跳過的列輕松擴展它以排除多於一列的打印。

Dim skipRowNum = DataGridView1.Columns("Column1").Index
        With DataGridView1
            Using fmt As New StringFormat With {
                .Alignment = StringAlignment.Center,
                .LineAlignment = StringAlignment.Center,
                .Trimming = StringTrimming.EllipsisCharacter,
                .FormatFlags = StringFormatFlags.LineLimit Or StringFormatFlags.NoWrap
            }
                Dim y As Single = e.MarginBounds.Top
                y = y + e.MarginBounds.Y + 30
                Using headerFont As New Font("Arial", 12, FontStyle.Bold)

                    Do While mRow < .RowCount
                        Dim row As DataGridViewRow = .Rows(mRow)
                        Dim x As Single = e.MarginBounds.Left
                        Dim h As Single = 0
                        x = x + e.MarginBounds.X + 180
                        For Each cell As DataGridViewCell In row.Cells
                            If cell.ColumnIndex = skipRowNum Then
                                Continue For
                            End If
                            Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
                            If newpage Then
                                e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText, headerFont, Brushes.Black, rc, fmt)  '
                            Else
                                e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), headerFont, 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
                End Using
            End Using
            mRow = 0
        End With

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM