本文介绍了我可以将datagridview拆分为2个网格吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个应用程序,用户可以在其中打印一些datagridviews的内容。其中一个网格相当狭窄,所以当我打印时它不会使用页面的整个宽度。但是,它确实需要2到3页的篇幅。而不是这个我希望能够在页面上拆分表,即。而不是有一个表有3个页面,我想要一个页面并排3个表格。



如果有人能给我一些关于我应该寻找什么的想法,我将非常感激。虽然有些代码会很好,但我很高兴能指出正确的方向。



非常感谢,



Aaron

Hi everyone,

I have an app in which the user can print the content of some datagridviews. One of the grids is fairly narrow, so when I print it doesn't use the full width of the page. It does, however, take up 2 or 3 pages in length. Rather than this I would like to be able to split the table across the page, ie. rather than have 3 pages with one table, I want one page with 3 tables side by side.

If anyone can give me some idea as to what I should be looking for I would be very grateful. Although some code would be nice I am happy to be pointed in the right direction.

Many Thanks,

Aaron

推荐答案


Private Sub DomainsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles DomainsToolStripMenuItem.Click
        Me.PrintPreviewDialog1.Document = New System.Drawing.Printing.PrintDocument
        PrintDocument2.DefaultPageSettings.Landscape = True
        AddHandler PrintPreviewDialog1.Document.PrintPage, _
               AddressOf PrintDocument2_PrintPage

        pageNumber = 1
        Me.PrintPreviewDialog1.ShowDialog()
    End Sub







这是打印文档.....






Here is the Print Document.....

Private Sub PrintDocument2_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument2.PrintPage
        PrintDocument2.DefaultPageSettings.Landscape = True
        Static mRow As Integer = 0
        Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
        Dim hfmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
        Dim tfmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
        Dim drwfont As New Font("Arial", 7.5)
        Dim drawfont As New Font("Arial", 11, FontStyle.Bold)
        fmt.Alignment = StringAlignment.Center
        hfmt.Alignment = StringAlignment.Center
        fmt.LineAlignment = StringAlignment.Center
        hfmt.LineAlignment = StringAlignment.Center
        fmt.Trimming = StringTrimming.EllipsisCharacter
        tfmt.Alignment = StringAlignment.Center
        tfmt.LineAlignment = StringAlignment.Center

        'Print the page number.
        e.Graphics.DrawString(Me.pageNumber, New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point), Brushes.Black, 400, 1050, tfmt)


        If pageNumber = 1 Then
            Dim MyString As String = "Document Title"
            e.Graphics.DrawString(MyString, New Font("Arial", 16, FontStyle.Bold), Brushes.Black, 230, 60)
        End If

        Dim y0 As Single = 150
        Dim y As Single = y0
        With DataGridView1
            While mRow < .RowCount
                Dim row As DataGridViewRow = .Rows(mRow)
                Dim x As Single = 20
                Dim h As Single = 0
                For Each cell As DataGridViewCell In row.Cells
                    Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width + 2, cell.Size.Height)
                    e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
                    If (y = y0) Then
                        e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText, drawfont, Brushes.Black, rc, hfmt)
                    Else
                        e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), drwfont, Brushes.Black, rc, fmt)
                    End If
                    x += rc.Width
                    h = Math.Max(h, rc.Height)

                Next
                y += h
                mRow += 1
                If (y + h) > e.MarginBounds.Bottom Then Exit While
            End While
            If mRow < .RowCount Then e.HasMorePages = True Else mRow = 0
            pageNumber += 1
        End With
    End Sub





不确定这与你提供的链接有什么比较。



目前,代码可以很好地用于更大的桌子以及更多列,但正如我在原始问题中所说,由于特定网格相当狭窄,如果表格在一个页面中分成2或3个表格,然后移动到新页面而不是一个长表格,则我更喜欢它或3页。



我希望这一切都有意义!!!?/



干杯,

Aaron



Not really sure how this might compare to the link you provided.

At the moment the code works perfectly fine for a larger table with a lot more columns but as I stated in my original question, as a particular grid is fairly narrow, I would prefer it if the table was split across a single page into 2 or 3 tables and then move to a new page rather than one long table over 2 or 3 pages.

I hope all this makes sense!!!?/

Cheers,
Aaron


这篇关于我可以将datagridview拆分为2个网格吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 15:49