我在Windows窗体应用程序(VS2017)中工作,我有一个包含数据和图像的datagridview。我必须在带有图像的多个页面中打印datagridview。 [英] I am working in windows forms application(VS2017) and I have a datagridview that contains data and images. I have to print that datagridview in multiple pages with images.

查看:147
本文介绍了我在Windows窗体应用程序(VS2017)中工作,我有一个包含数据和图像的datagridview。我必须在带有图像的多个页面中打印datagridview。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView,它包含从数据库中获取的数据和图像。我必须打印这个数据网格视图。

问题:

1. DataGridView包含的数据不适合一个页面,因此必须打印多个页面。 />
2.图像未出现在打印预览中。

3.超出第一页的数据不会出现在下一页或在第一页中被覆盖(打印预览)。

我试过的代码在第一页和下一页打印数据只有列标题(没有行)。



*我的代码首先打开打印预览对话框,然后我必须打印它。

**我还修改了PrintPreviewDialog的Print选项(ToolStrip)..



我尝试了什么:



I have a DataGridView that contains data and images, fetched from database. I have to print this data grid view.
Problem:
1. DataGridView contains data that will not fit in one page, so have to print in multiple pages.
2. The Images are not appearing in print preview.
3. Data that exceeds first page doesn't appear in next page or overwritten in the first page(print preview).
The Code I have tried prints data in the first page and in the next page there are only column headers(no rows).

*My code opens the print preview dialog first and then I have to print it.
** I have also modified the Print option(ToolStrip) of the PrintPreviewDialog..

What I have tried:

Private Sub Print_lst_Click(sender As Object, e As EventArgs) Handles Print_lst.Click
        Dim b As New ToolStripButton
        b.Image = CType(PrintPreviewDialog1.Controls(1), ToolStrip).ImageList.Images(0)
        b.ToolTipText = "Print"
        b.DisplayStyle = ToolStripItemDisplayStyle.Image
        AddHandler b.Click, AddressOf PrintPreview_PrintClick
        CType(PrintPreviewDialog1.Controls(1), ToolStrip).Items.RemoveAt(0)
        CType(PrintPreviewDialog1.Controls(1), ToolStrip).Items.Insert(0, b)
        AddHandler PrintDocument1.PrintPage, AddressOf PrintDocument1_PrintPage
        ' PrintPreviewDialog1.StartPosition = FormStartPosition.CenterParent
        ' PrintPreviewDialog1.WindowState = FormWindowState.Maximized
        PrintPreviewDialog1.Document = PrintDocument1
        PrintDocument1.OriginAtMargins = True
        PrintDocument1.DefaultPageSettings.Margins = New Drawing.Printing.Margins(0, 0, 0, 0)
        'PrintPreviewDialog1.Icon = Icon
        PrintPreviewDialog1.WindowState = FormWindowState.Maximized
        PrintPreviewDialog1.ShowDialog()
    End Sub

    Private Sub PrintPreview_PrintClick(sender As Object, e As EventArgs)
        Try
            PrintDialog1.Document = PrintDocument1
            If PrintDialog1.ShowDialog() = DialogResult.OK Then
                PrintDocument1.Print()
            End If
        Catch ex As Exception
        End Try
        clean()
    End Sub

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

        Dim x As Integer = 50
        Dim y As Integer = 50
        Dim header As Boolean = True
        'draw headers
        Dim j As Integer = 0
        Do While (j < Me.DataGridView1.Columns.Count)
            Dim rect As Rectangle = New Rectangle(x, y, Me.DataGridView1.Columns(j).Width, Me.DataGridView1.ColumnHeadersHeight)
            e.Graphics.FillRectangle(Brushes.LightGray, rect)
            e.Graphics.DrawRectangle(Pens.Black, rect)
            If (Not (Me.DataGridView1.Columns(j).HeaderText) Is Nothing) Then
                e.Graphics.DrawString(Me.DataGridView1.Columns(j).HeaderText, SystemFonts.DefaultFont, Brushes.Black, rect)
            End If
            x = (x + rect.Width)
            j = (j + 1)
        Loop
        x = 50
        y = (y + Me.DataGridView1.ColumnHeadersHeight)
        'draw rows
        For whRow = whRow To Me.DataGridView1.RowCount - 1
            Dim drow As DataGridViewRow = Me.DataGridView1.Rows(whRow)
            j = 0
            Do While (j < Me.DataGridView1.Columns.Count)
                Dim cell As DataGridViewCell
                cell = drow.Cells(j)
                Dim rect As Rectangle = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)

                e.Graphics.DrawRectangle(Pens.Black, rect)
                If (Not (cell.Value) Is Nothing) Then
                    e.Graphics.DrawString(cell.Value.ToString, SystemFonts.DefaultFont, Brushes.Black, rect)

                End If
                x = (x + rect.Width)
                j = (j + 1)
            Loop
            x = 50
            y = (y + drow.Height)
            '----------------------New page----------------------------
            If (y > e.MarginBounds.Bottom) Then       'Print new page
                e.HasMorePages = True
'if I add y=50 or Exit Sub here then the remaining data(that doesn't fit in the firstPage is overwritten in the firstpage (2nd page contains the header only)
            End If
            '-----------------------------------------------------------------
        Next
    End Sub

推荐答案

您不会弯曲适合其他目的的现有视图。



创建一个适合页面的新视图(源自在线视图)。使用与在线版本相同的数据源运行它。



通常只是拉伸尺寸和/或控制分页符。
You don't "bend" an existing view that suits it's purpose to suit something else.

Create a new view (derived from the "online" view) that will "fit" the page. Run it with the same data source as the "online version".

Usually just a matter of "stretching" dimensions and / or controlling page breaks.


这篇关于我在Windows窗体应用程序(VS2017)中工作,我有一个包含数据和图像的datagridview。我必须在带有图像的多个页面中打印datagridview。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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