KnowDotNet NetRefactor

Print Contents of Grid or ListView

.NET Printing Example Free Code

by Les Smith
Print this Article Discuss in Forums

I am constantly faced with the requirement to print the contents, or selected columns, of a DataGrid or ListView.  This article presents a description and all of the code for a demo project to accomplish this.  Note that this code has been updated and corrects reported issues with printing multiple pages from Print Preview and in Landscape Orientation.

The demo project contains a form with both a DataGrid and a ListView on it and prints or previews selected columns of both.  It uses the
CPrintReportString code that I demonstrated previously.  If you want a description of how that code works, refer to that article.  I have made some enhanced interfaces to that class.  Additionally, I have written classes to print selected columns of DataGrids and Listviews.

You can download the demonstration project, which includes all of the code for formatting and printing ListViews and DataGrids by clicking here.

Figure 1 shows the code for calling the PrintGrid class.  This code is called from a button click event that passes a parameter that denotes whether the user want to Preview or Print the grid contents.  The code for this method is doing several things.  
Please check the Demo Code for the latest and most correct code.  The code shown below is sample code and may have been updated by the demo project.


  1. Creates PrintStructureGrid object for each column to be printed.  These objects describe the attributes the column to be printed and how it is to be formatted.

  2. Adds the objects to a Strongly Typed PrintGridCollection.

  3. Sets up a PrintOptions object to provide properties of the printed report

  4. Creates a CPrintGrid object and calls is passing a DataView from the Grid, the PrintGridCollection, and the PrintOptions object


  5. The CPrintGrid class does all of the work of formatting the print data and calling the print class.  

    Figure 1 - Code to Call PrintGrid.

       Private Sub PrintOrPreviewGrid(ByVal Preview As PrintPreview)
          
    ' create a strongly type collection of
          ' PrintStructureDataGrid(columns)
          Dim ps As PrintStructureDataGrid
          
    Dim col As New PrintGridCollection
          
    ' print column 0 of grid
          ps = New PrintStructureDataGrid
          ps.Column = 0
          ps.Format = FormatString.Boolean
          ps.NumberDataColumnChars = 5
          ps.Alignment = Align.Center
          ps.PrintColumnWidth = 7
          col.Add(ps)

          
    ' print column 1 of grid
          ps = New PrintStructureDataGrid
          ps.Column = 1
          ps.Format = FormatString.Default
          ps.NumberDataColumnChars = 15
          ps.Alignment = Align.Left
          ps.PrintColumnWidth = 18
          col.Add(ps)

          
    ' print column 5 of grid
          ps = New PrintStructureDataGrid
          ps.Column = 2
          ps.Format = FormatString.DateTime
          ps.NumberDataColumnChars = 23
          ps.Alignment = Align.Left
          ps.PrintColumnWidth = 25
          col.Add(ps)

          ps =
    New PrintStructureDataGrid
          ps.Column = 5
          ps.Format = FormatString.Currency
          ps.NumberDataColumnChars = 10
          ps.Alignment = Align.Right
          ps.PrintColumnWidth = 12
          col.Add(ps)

          
    ' set up print options
          Dim po As New PrintOptions
          po.CharsPerLine = CharsPerLine.CPL80
          po.ColHdr1 = "Col 0".PadRight(7) & _
                       "Column 1".PadRight(18) & _
                       "Column 3".PadRight(25) & _
                       "Column 5".PadRight(12)
          po.Portrait = PrintOrientation.Portrait
          po.Title = "My Grid Report"
          po.PrintOrPreview = Preview
          po.SubTitle = "This Report is Grid Columns 0,1,2,5"
          po.Boxed =
    True
          po.LeftMarginExtender = MarginExtender.OneHalfInch
          po.RightMarginExtender = MarginExtender.OneHalfInch
          po.TopMarginExtender = MarginExtender.OneHalfInch
          po.BottomMarginExtender = MarginExtender.OneHalfInch

          
    ' print the grid
          Dim pg As New CPrintGrid
          
    Dim dv As DataView = pg.GetDataSource(Me, DataGrid1)
          pg.PrintGrid(dt, col, po)

      
    End Sub

    Figure 2 shows the Demo Form with the DataGrid populated.  To Preview or Print the Grid, click the respective button.  Figure 3 shows the Preview of the printed data.

    Figure 2 - DataGrid Print Demo Form.

    PrintGrid Demo Form


    Figure 3 - Preview of the Printed Grid Data.

    Preview Grid Print


    Figure 4 shows the code for calling the methods for printing the selected contents of a ListView.  This code does basically the same thing as the code in Figure 1, except that it is using classes and collections that deal with describing ListView data to the print methods.  Since all of the cells in a ListView are of type String, the print class assumes that any formatting of the data has already been done as the ListView is loaded and therefore, no additional formatting is allowed by the Print ListView classes.  
    Please check the Demo Code for the latest and most correct code.  The code shown below is sample code and may have been updated by the demo project.


    Figure 4 - Code for Calling the Print ListView Method.

       Private Sub PrintOrPreviewListView(ByVal Preview As PrintPreview)
          
    ' create a strongly type collection of
          ' printstructure(columns)
          Dim ps As PrintStructureListView
          
    Dim col As New PrintListViewCollection

          
    ' print column 0 of grid
          ps = New PrintStructureListView
          ps.Column = 0
          ps.NumberDataColumnChars = 5
          ps.Alignment = Align.Center
          ps.PrintColumnWidth = 7
          col.Add(ps)

          
    ' print column 1 of grid
          ps = New PrintStructureListView
          ps.Column = 1
          ps.NumberDataColumnChars = 15
          ps.Alignment = Align.Left
          ps.PrintColumnWidth = 18
          col.Add(ps)

          
    ' print column 5 of grid
          ps = New PrintStructureListView
          ps.Column = 2
          ps.NumberDataColumnChars = 20
          ps.Alignment = Align.Left
          ps.PrintColumnWidth = 22
          col.Add(ps)

          ps =
    New PrintStructureListView
          ps.Column = 3
          ps.NumberDataColumnChars = 25
          ps.Alignment = Align.Left
          ps.PrintColumnWidth = 27
          col.Add(ps)

          ps =
    New PrintStructureListView
          ps.Column = 4
          ps.NumberDataColumnChars = 10
          ps.Alignment = Align.Right
          ps.PrintColumnWidth = 12
          col.Add(ps)

          
    ' set up print options
          Dim po As New PrintOptions
          po.CharsPerLine = CharsPerLine.CPL80
          po.ColHdr1 = "Col 1".PadRight(7) & _
                       "Column 2".PadRight(18) & _
                       "Column 3".PadRight(22) & _
                       "Column 4".PadRight(27) & _
                       "Column 5".PadRight(12)
          po.Portrait = PrintOrientation.Portrait
          po.Title = "My ListView Report"
          po.PrintOrPreview = Preview
          po.SubTitle = "This Report is ListView Columns 0-5"
          po.Boxed =
    True
          po.LeftMarginExtender = MarginExtender.OneHalfInch
          po.RightMarginExtender = MarginExtender.OneHalfInch
          po.TopMarginExtender = MarginExtender.OneHalfInch
          po.BottomMarginExtender = MarginExtender.OneHalfInch

          
    ' print the grid
          Dim pg As New CPrintGrid
          pg.PrintGrid(ListView1, col, po)

      
    End Sub

    Figure 5 shows the ListView before it is printed.  Figure 6 shows a Preview of the data formatted for printing.

    Figure 5 - ListView to be Printed.

    ListView Demo Form


    Figure 6 - Preview of ListView Report.

    Preview List View  Print


    You can download the demonstration project, which includes all of the code for formatting and printing ListViews and DataGrids by clicking here.


    If you find the PrintGridClass useful and would like to make a donation to
    allow KnowDotNet to continue to provide this type of tool click the button below.





Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing