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.
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 |


| 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 |

