我可以将datagridview拆分为2个网格吗? [英] Can I split a datagridview into 2 grids?

查看:85
本文介绍了我可以将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

推荐答案

或许,您正在打印表单或DataGridView对象本身,而不是正确地完成工作。



相反,使用 PrintDocument [ ^ ]你可以确切地确定每个标志的位置(以及它的大小) - 所以如果你想要打印两列,你就可以。如果你想要三个它并且它在横向上打印,你也可以这样做。

它可能看起来很复杂,但它真的非常简单,从长远来看非常值得去掌握。 />


该链接包含一个基本示例。
Probably, you are printing the form or DataGridView object itself, instead of "doing teh job properly".

Instead, use a PrintDocument[^] and you can decide exactly where everyuthign goes (and how large it is) - so if you want two columns printed, you can. If you want three and it printed in landscape, you can do that as well.
It may seem complicated, but it's pretty simple really, and well worth getting to grips with in the long run.

The link includes a basic example.


您好OriginalGriff,



感谢您的回复....



这是我目前正在使用的代码,这样您就可以看到自己是否正常工作 。



这部分是点击事件......



Hi OriginalGriff,

Thank you for your response....

Here is the code I am using at the moment so you can see yourself if I am "doing the job properly".

This part is the "click event"......

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个网格吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆