繁体   English   中英

VB.NET 如何从 datagridview 打印行标题?

[英]VB.NET How do I print the row headers from a datagridview?

我有一个 DataGridView,它显示了一个带有条件日期的 SQL 表(类似于议程)。 它工作正常,但我需要枚举包含“相同日期”的行,所以我添加了一个 Row Header 来枚举它们:

Private Sub SetRowHeaderNumber(ByRef row As DataGridViewRow)
        row.HeaderCell.Value = String.Format("{0}", row.Index + 1)
End Sub
For Each row As DataGridViewRow In Me.DataGridView1.Rows
            Me.SetRowHeaderNumber(row)
Next row

图 1、DataGridView、行号和打印按钮

问题是当我需要打印 DataGridView 时,因为打印时,它不显示行标题(见下图)

图二

我用于打印的代码是:

 Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoc.PrintPage, PrintDocument1.PrintPage
        Dim YAxis As Integer = 0
        Dim dat As String
        dat = Label1.Text + " de " + Label2.Text + " de " + Label3.Text
        ' 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 = 100 'Dim y As Int32 = e.MarginBounds.Top
        Dim rc As Rectangle
        Dim x As Int32 = 50 ' Dim x As Int32
        Dim h As Int32 = 0
        Dim row As DataGridViewRow

        e.Graphics.DrawString(meuesc.Text, New Font("Century Gothic", 20, FontStyle.Bold), Brushes.Black, New Point(10, YAxis + 15))
        e.Graphics.DrawString("Prazos " + dat, New Font("Century Gothic", 12, FontStyle.Bold), Brushes.Black, New Point(10, YAxis + 50))



        ' print the header text for a new page
        '   use a grey bg just like the control
        Try
            If newpage Then
                row = DataGridView1.Rows(mRow)
                x = 10 '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
                        Dim width1 As Integer = 1000
                        rc = New Rectangle(x, y, width:=150, 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
        Catch
            MsgBox("Não foi possivel obter os dados para impressão")
        End Try


        ' 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 = 10 'x = e.MarginBounds.Left
            h = 0

            ' reset X for data
            x = 10 'e.MarginBounds.Left

            ' print the data
            For Each cell As DataGridViewCell In row.Cells
                If cell.Visible Then
                    rc = New Rectangle(x, y, width:=150, 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.MarginBounds.Bottom
                e.HasMorePages = True
                '  mRow -= 1   \causes last row To rePrint On Next page
                newpage = True
                Return
            End If

        Next

    End Sub

我希望图 1 中圈出的 RowHeaders 也显示在图 2 中以进行打印。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM