在打印预览窗口中查看Datagridview

时间:2014-10-23 08:59:18

标签: visual-studio-2010 datagridview vb.net-2010 printdocument print-preview

我正在vb.net中创建一个应用程序。我的VB表单中有一个datagridview控件。我需要在printpreview窗口中查看它的内容。我在表单中有其他控件,如标签和文本框,我可以在printpreview中查看所有内容。在Datagridview控件的情况下,我有一个工作的printpreview代码,我从网络。我的问题是,我需要更改datagridview控件的x和y位置。使用以下代码,datagridview控件将显示在其他控件上。我不知道如何在这段代码中做到这一点。请帮我。 我需要更改DataGridView的x和y位置,如下面的代码(50和225)所示。  e.Graphics.DrawString(Label7.Text,Label7.Font,Brushes.Black,50,225)

我用来显示gridview的代码如下所示。

代码:


Dim ColumnCount As Integer = DataGridView1.ColumnCount         Dim RowCount As Integer = DataGridView1.RowCount

    Dim CellTopPos As Integer = PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Top

    For Row = 0 To RowCount - 2

        Dim CellLeftPos As Integer = PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Left

        For Cell = 0 To ColumnCount - 1

            Dim CellValue As String = DataGridView1.Rows(Row).Cells(Cell).Value.ToString()
            Dim CellWidth = DataGridView1.Rows(Row).Cells(Cell).Size.Width + 10
            Dim CellHeight = DataGridView1.Rows(Row).Cells(Cell).Size.Height

            Dim Brush As New SolidBrush(Color.Black)
            e.Graphics.DrawString(CellValue, New Font("arial", 9), Brush, CellLeftPos, CellTopPos)
            e.Graphics.DrawRectangle(Pens.Black, CellLeftPos, CellTopPos, CellWidth, CellHeight)

            CellLeftPos += CellWidth
        Next

        CellTopPos += DataGridView1.Rows(Row).Cells(0).Size.Height
    Next

1 个答案:

答案 0 :(得分:0)

使用此类..遗憾的是有字符限制,所以我无法发布代码 http://www.codeproject.com/Articles/18042/Another-DataGridView-Printer