This blog is about the dotnet.all types of codes,news about dotnet including asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.ncluding asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.asp.net programming,dot net programming,dotnet programs,dotnet source code,source code.

Free Hosting

Free Hosting

Friday, February 1, 2008

How to print the content of a Listview control using Visual Basic .NET

The Listview control can be used as a simple grid control to output tabular data, reports structured in columns and data formatted in the "familiar" spread-sheet mode. As most .NET programmers will have found, all you have to do is set the View property to "Details" and you have a simple grid control. If you are new to this and want to see how to layout such a ListView grid and how to populate it with data you can download a demonstration project from here.

In many instances, populated grids are long enough or contain data of sufficient complexity to make it worth offering our users a print option. The .NET Frameworks provide a base class ( PrintDocument) ready and waiting to provide Page Set-Up, Print Preview and Printing functionality. That's the good news, the bad news is that you have to write the code that actually manages the printing of the grid content in discrete pages. But more good news - you can download the source code of a class from here that will manage all that for you. All you have to do is point your print menu options or buttons at the relevant methods exposed by this class.

At it's simplest, all you have to do is create an instance of the class (passing it the ListView control it is going to work with) and then call one of the three public methods when required. This class is capable, however, of further development but already provides some additional (optional) features that you might want to use but these involve nothing more than setting some properties.

So what can the ListViewPrint class do?
It's primary function is to print the content of a ListView formatted as a simple grid control. It will manage ListView grids that extend to multiple pages in both the vertical and horizontal dimensions - printing the pages to the user's choice of printer or in the print preview mode.

The ListViewPrint class can also append a title to the first (or all pages) with full control over the title font if required.

The ListViewPrint class can repeat column headings above each column on all pages - ensuring that the end user always understands the content of each page.

The ListViewPrint class can be set to "compress" the output to fit a single page width - with the vertical dimension being compressed proportionately. This is not wildly sophisticated but very effective for reports that are just a little too wide to fit the width of a single sheet of standard A4 or Letter (depending upon your local).

Adding ListView printing to your Visual Basic .NET application
The simplest method (and the one that lets you follow the code during execution) is to copy the ListviewPrint class code file (ListViewPrint.vb) into your project folder. Then use the Project/Add Existing Item menu to locate and add the ListViewPrint class to your project. Alternately you could copy and paste the code from the ListViewPrint class into a class that you created directly within your project.

Next, you need to declare an instance of the ListViewPrint class in your code using the WithEvents keyword. Plus you will need to initialise that instance and pass it the identity of the ListView it will be responsible for printing.

Friend WithEvents ListViewPrint1 As ListViewPrint
ListViewPrint1 = New ListViewPrint(ListViewGrid)

Most users are used to locating print functions using the file menu. So drop an instance of the MainMenu control onto your form and reate a File menu with sub-entries for Page Setup, Print Preview and Print.

In the Click event for Page set-up you just need to call the related method in the ListViewPrint class - something like:

Private Sub mnuFilePageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFilePageSetup.Click
ListViewPrint1.PageSetUp()
End Sub

With similar links for the Print Preview and Print menu item clicks as well

Private Sub mnuFilePrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFilePrintPreview.Click
ListViewPrint1.PrintPreview()
End Sub

Private Sub mnuFilePrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFilePrint.Click
ListViewPrint1.PrintGrid()
End Sub

That is all the code you need to get started with printing ListView control contents. However there are some optional properties that you might like to set.

GridFont - I would advise setting this value to equal the current Font being used by the ListView although a sensible default will be substituted if you skip setting this property.

ReportTitle - setting this string property will result in the string being output as a title on the first printed page.

TitleFont - This property can be used to set the required font for the title when it is printed.

RepeatTitle - setting this boolean attribute to True will result in the title string being repeated at the top of every page.

RepeatColumnHeadings - this boolean attribute will result in the re-printing of the grid column headings on every printed page as well as on the first.

PrintGridLines - this boolean attribute (set by default to True) can be used to print or suppress the printing of the grid cell boarders.

CompressToPage - this boolean property can be set to compress the width of the printed output sufficiently to fit the width of the current page. The vertical size of the grid is compressed in proportion but may require more than one page in height.

Downloading the ListView printing class

Click here to download the source code for the ListView Print class

Remember, you can also download the Visual Basic ,.NET source for a project demonstrating the use of the ListView Print class and companion DataGrid Print class.

The future of the ListViewPrint class
The ListViewPrint class will continue to be developed alongside the companion DataGridPrint class (for printing DataGrid control contents). For future updates and added functionality please revisit this page from time to time. We will post the version number and revision date near the top of the page.

Please let us know if you add some interesting additional functionality as we will be happy to review and publish contributions.

Can the ListViewPrint class be used in a C# project?
Yes it can. The simplest was is to compile the class into a DLL and then add a reference to that DLL to your project. You would need to distribute the DLL alongside your executable but the full functionality of the class would be available to you. Alternately, you could use one of the facilities available on the Internet to convert the code to C#.

1 comments:

The Shandy Drinking Northerner said...

Hi - code looks just what I'm looking for, but I can't download it from the URL - seems the site is offline. Any chance you can repost the ListViewPrint class?

dotnet(.Net) Project Source code Downloads and Tutorials

Email Subscrption



Enter your email address:

Delivered by FeedBurner

Feedburner Count

Unique Visitor

Design by araba-cı | MoneyGenerator Blogger Template by GosuBlogger