I’m using VB.Net 2008 application program.

I’m using DataGridView. I have a Print option where i need to print the DataGridView.

I’m using this code for Printing and for Print Preview.

Dim MyDataGridViewPrinter As DataGridViewPrinter


    Private Function SetupThePrinting() As Boolean
        Dim MyPrintDialog As PrintDialog = New PrintDialog()

        MyPrintDialog.AllowCurrentPage = False
        MyPrintDialog.AllowPrintToFile = False
        MyPrintDialog.AllowSelection = False
        MyPrintDialog.AllowSomePages = True
        MyPrintDialog.PrintToFile = False
        MyPrintDialog.ShowHelp = False
        MyPrintDialog.ShowNetwork = False

        PrintDocument1.PrinterSettings = MyPrintDialog.PrinterSettings
        PrintDocument1.DefaultPageSettings.Margins = New Margins(10, 10, 10, 10)

               MyDataGridViewPrinter = New DataGridViewPrinter(DataGridView1, PrintDocument1, False, True, "Price Manager", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
       
        Return True
    End Function

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

        Try
            more = MyDataGridViewPrinter.DrawDataGridView(e.Graphics)
            If more Then e.HasMorePages = True
        Catch Ex As Exception
        End Try
    End Sub

    ' The Print Preview Button
    Private Sub btnPrintPreview_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
        If SetupThePrinting() Then
            Dim MyPrintPreviewDialog As PrintPreviewDialog = New PrintPreviewDialog()
            MyPrintPreviewDialog.Document = PrintDocument1
            MyPrintPreviewDialog.ShowDialog()
        End If
    End Sub

    ' The Print Button
    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        If SetupThePrinting() Then PrintDocument1.Print()
    End Sub

Attached the class i'm using for print.

I have 10 fields showing in datagridview. As the last 5 columns header is long, the datagridview is not fitting inside 1 page (not showing all 10 fields in one single page). But is there a way i can "Text Wrap" the top header column and lock in the width, then it should print across one page perfectly.

I searched a lot, but I couldn’t find it.

If you know how i can "Text Wrap" the top header column and lock in the width to print across one page, please help me. If you can provide an example, that will be so helpful.

Thanks in advance.

Attachments
This is the class im using for Print

[CODE]
Imports System
'Imports System.Text
'Imports System.Collections
'Imports System.Collections.Generic
Imports System.Drawing
Imports System.Drawing.Printing
'Imports System.Data
'Imports System.Windows.Forms

Class DataGridViewPrinter

    Private TheDataGridView As DataGridView  ' The DataGridView Control which will be printed
    Private ThePrintDocument As PrintDocument ' The PrintDocument to be used for printing
    Private IsCenterOnPage As Boolean ' Determine if the report will be printed in the Top-Center of the page
    Private IsWithTitle As Boolean  ' Determine if the page contain title text
    Private TheTitleText As String ' The title text to be printed in each page (if IsWithTitle is set to true)
    Private TheTitleFont As Font ' The font to be used with the title text (if IsWithTitle is set to true)
    Private TheTitleColor As Color ' The color to be used with the title text (if IsWithTitle is set to true)
    Private IsWithPaging As Boolean ' Determine if paging is used

    Private Shared CurrentRow As Integer = 0 ' A static parameter that keep track on which Row (in the DataGridView control) that should be printed

    Private Shared PageNumber As Integer

    Private PageWidth As Integer
    Private PageHeight As Integer
    Private LeftMargin As Integer
    Private TopMargin As Integer
    Private RightMargin As Integer
    Private BottomMargin As Integer

    Private CurrentY As Single ' A parameter that keep track on the y coordinate of the page, so the next object to be printed will start from this y coordinate

    Private RowHeaderHeight As Single
    Private RowsHeight As ArrayList
    Private ColumnsWidth As ArrayList
    Private TheDataGridViewWidth As Single

    ' Maintain a generic list to hold start/stop points for the column printing
    ' This will be used for wrapping in situations where the DataGridView will not fit on a single page
    Private mColumnPoints As ArrayList
    Private mColumnPointsWidth As ArrayList
    Private mColumnPoint As Integer

    'The class constructor.
    Public Sub New(ByRef aDataGridView As DataGridView, ByRef aPrintDocument As PrintDocument, ByVal CenterOnPage As Boolean, ByVal WithTitle As Boolean, ByVal aTitleText As String, ByVal aTitleFont As Font, ByVal aTitleColor As Color, ByVal WithPaging As Boolean)
        TheDataGridView = aDataGridView
        ThePrintDocument = aPrintDocument
        IsCenterOnPage = CenterOnPage
        IsWithTitle = WithTitle
        TheTitleText = aTitleText
        TheTitleFont = aTitleFont
        TheTitleColor = aTitleColor
        IsWithPaging = WithPaging

        PageNumber = 0

        RowsHeight = New ArrayList
        ColumnsWidth = New ArrayList

        mColumnPoints = New ArrayList
        mColumnPointsWidth = New ArrayList

        'Claculating the PageWidth and the PageHeight.
        If Not ThePrintDocument.DefaultPageSettings.Landscape Then
            PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width
            PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height
        Else
            PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Width
            PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Height
        End If

        ''Claculating the PageWidth and the PageHeight.
        'If Not ThePrintDocument.DefaultPageSettings.Landscape Then
        '    PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Width
        '    PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Height
        'Else
        '    PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width
        '    PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height
        'End If

        'Claculating the page margins.
        LeftMargin = ThePrintDocument.DefaultPageSettings.Margins.Left
        TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top
        RightMargin = ThePrintDocument.DefaultPageSettings.Margins.Right
        BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Bottom

        'First, the current row to be printed is the first row in the DataGridView control.
        CurrentRow = 0
    End Sub

    ' The function that calculate the height of each row (including the header row), the width of each column (according to the longest text in all its cells including the header cell), and the whole DataGridView width
    Private Sub Calculate(ByVal g As Graphics)
        If PageNumber = 0 Then ' Just calculate once
            Dim tmpSize As SizeF = New SizeF()
            Dim tmpFont As Font
            Dim tmpWidth As Single
            Dim i As Integer
            Dim j As Integer

            TheDataGridViewWidth = 0
            For i = 0 To TheDataGridView.Columns.Count - 1
                tmpFont = TheDataGridView.ColumnHeadersDefaultCellStyle.Font
                If tmpFont Is Nothing Then ' If there is no special HeaderFont style, then use the default DataGridView font style
                    tmpFont = TheDataGridView.DefaultCellStyle.Font
                End If
                tmpSize = g.MeasureString(TheDataGridView.Columns(i).HeaderText, tmpFont)
                tmpWidth = tmpSize.Width
                RowHeaderHeight = tmpSize.Height

                For j = 0 To TheDataGridView.Rows.Count - 1
                    tmpFont = TheDataGridView.Rows(j).DefaultCellStyle.Font
                    If tmpFont Is Nothing Then ' If the there is no special font style of the CurrentRow, then use the default one associated with the DataGridView control
                        tmpFont = TheDataGridView.DefaultCellStyle.Font
                    End If

                    tmpSize = g.MeasureString("Anything", tmpFont)
                    RowsHeight.Add(tmpSize.Height)

                    tmpSize = g.MeasureString(TheDataGridView.Rows(j).Cells(i).EditedFormattedValue.ToString(), tmpFont)
                    If (tmpSize.Width > tmpWidth) Then
                        tmpWidth = tmpSize.Width
                    End If
                Next
                If TheDataGridView.Columns(i).Visible Then
                    TheDataGridViewWidth += tmpWidth
                End If
                ColumnsWidth.Add(tmpWidth)
            Next

            ' Define the start/stop column points based on the page width and the DataGridView Width
            ' We will use this to determine the columns which are drawn on each page and how wrapping will be handled
            ' By default, the wrapping will occurr such that the maximum number of columns for a page will be determine
            Dim k As Integer

            Dim mStartPoint As Integer = 0
            For k = 0 To TheDataGridView.Columns.Count - 1
                If TheDataGridView.Columns(k).Visible Then
                    mStartPoint = k
                    Exit For
                End If
            Next
            Dim mEndPoint As Integer = TheDataGridView.Columns.Count
            For k = TheDataGridView.Columns.Count - 1 To 0 Step -1
                If TheDataGridView.Columns(k).Visible Then
                    mEndPoint = k + 1
                    Exit For
                End If
            Next

            Dim mTempWidth As Single = TheDataGridViewWidth
            Dim mTempPrintArea As Single = CType(PageWidth, Single) - CType(LeftMargin, Single) - CType(RightMargin, Single)

            ' We only care about handling where the total datagridview width is bigger then the print area
            If TheDataGridViewWidth > mTempPrintArea Then
                mTempWidth = 0.0
                For k = 0 To TheDataGridView.Columns.Count - 1
                    If TheDataGridView.Columns(k).Visible Then
                        mTempWidth += ColumnsWidth(k)
                        ' If the width is bigger than the page area, then define a new column print range
                        If (mTempWidth > mTempPrintArea) Then
                            mTempWidth -= ColumnsWidth(k)
                            mColumnPoints.Add(New Integer() {mStartPoint, mEndPoint})
                            mColumnPointsWidth.Add(mTempWidth)
                            mStartPoint = k
                            mTempWidth = ColumnsWidth(k)
                        End If
                    End If
                    ' Our end point is actually one index above the current index
                    mEndPoint = k + 1
                Next
            End If
            ' Add the last set of columns
            mColumnPoints.Add(New Integer() {mStartPoint, mEndPoint})
            mColumnPointsWidth.Add(mTempWidth)
            mColumnPoint = 0
        End If
    End Sub

    'The funtion that print the title and the header row.
    ' The funtion that print the title, page number, and the header row
    Private Sub DrawHeader(ByVal g As Graphics)
        CurrentY = CType(TopMargin, Single)

        ' Printing the page number (if isWithPaging is set to true)
        If IsWithPaging Then
            PageNumber += 1
            Dim PageString As String = "Page " + PageNumber.ToString()

            Dim PageStringFormat As StringFormat = New StringFormat()
            PageStringFormat.Trimming = StringTrimming.Word
            PageStringFormat.FormatFlags = StringFormatFlags.NoWrap Or StringFormatFlags.LineLimit Or StringFormatFlags.NoClip
            PageStringFormat.Alignment = StringAlignment.Far

            Dim PageStringFont As Font = New Font("Tahoma", 8, FontStyle.Regular, GraphicsUnit.Point)

            Dim PageStringRectangle As RectangleF = New RectangleF(CType(LeftMargin, Single), CurrentY, CType(PageWidth, Single) - CType(RightMargin, Single) - CType(LeftMargin, Single), g.MeasureString(PageString, PageStringFont).Height)

            g.DrawString(PageString, PageStringFont, New SolidBrush(Color.Black), PageStringRectangle, PageStringFormat)

            CurrentY += g.MeasureString(PageString, PageStringFont).Height
        End If

        ' Printing the title (if IsWithTitl

Thanks Oxiegen for your help.

i tried as you mentioned. but its not showing in landscape format.

Is there a way i can print this in Landscape format, so entire columns will be fit in one page?

this is the code used in class for checking Landscape. but when i try both, it shows same page format.

'Claculating the PageWidth and the PageHeight.
        If Not ThePrintDocument.DefaultPageSettings.Landscape Then
            PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Width
            PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Height
        Else
            PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width
            PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height
        End If

Is it possible? If you know how to print this in Landscape format, please help me. if you can provide an example, that will be so helpful.

Thanks in advance.

This is how you set the orientation to landscape.

Dim settings As New System.Drawing.Printing.PrinterSettings
settings.DefaultPageSettings.Landscape = True 'This is the key

'And to retain those margins you're setting
settings.DefaultPageSettings.Margins = New Margins(10, 10, 10, 10)

PrintDocument1.PrinterSettings = settings
This article has been dead for over six months. Start a new discussion instead.