Click here to Skip to main content
6,292,426 members and growing! (9,585 online)
Email Password   helpLost your password?
Desktop Development » List Controls » ListView controls     Intermediate

Printable ListView

By Matteo D'Avena

A customized ListView control with printing capability.
C#.NET 1.1, Win2K, WinXP, Win2003, GDI+, WinForms, VS.NET2003, Dev
Posted:19 Sep 2005
Views:56,255
Bookmarked:54 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
20 votes for this article.
Popularity: 6.01 Rating: 4.62 out of 5

1

2

3
7 votes, 35.0%
4
13 votes, 65.0%
5

Demo application

List Print Preview

Introduction

Some time during my development works I had the need of printing the contents of a ListView control. So I wrote down the code for a custom control, named PrintableListView, that solved the following two problems:

  • extend the print on multiple pages when the width of the list exceeds the width of the print page,
  • shrink, optionally, the list in a way that the resulting width fits on a single page.

Using the code

PrintableListView control is derived from System.Windows.Forms.ListView, so use it like the native ListView control. The additional functionalities are those for print and for print preview.

// call the print method for displaying the standard print dialog.

this.m_list.Print();


// call the print preview method for displaying

// the standard print preview dialog

this.m_list.PrintPreview();

To turn on the �Fit to page� option, use the FitToPage property of the control.

this.m_list.FitToPage = true;

To set the title that appears on the page header, use the Title property of the control.

Points of Interest

If you think about the ListView control, you will note that we have all the information we need to print it in the simplest way. We have the columns width, the fonts, the rows height and so on. All we have to do is to get all these information and play a little with the Graphics instance that represents the printing surface. What follows is the way I solved the problems listed in the Introduction.

Multi page print

This is quite simple. When we are printing the current column of a row, we check whether there is enough room to print it. If not, we mark that column as the first one for the next page. That�s it.

Fit to page

This point is just a little bit more complex compared to the previous one. If the total width of the list is greater than the width of the available space, we have to calculate a scaling factor, given by the list width divided by the available space. For simplicity, in my implementation, I converted all the measures in units of hundredths of an inch. To set the scale factor and unit of measure is sufficient to change the coordinate system of the Graphics object passed to the handler of the PrintPage event of the PrintDocument object.

private void OnPrintPage(object sender, PrintPageEventArgs e)
{
    �
    if (m_nStartCol==0 && m_bFitToPage && m_fListWidth>rectBody.Width)
    {
        // Calculate scale factor

        fScale = rectBody.Width / m_fListWidth;
    }
    �
    �
    // Setting scale factor and unit of measure

    g.ScaleTransform(fScale, fScale);
    g.PageUnit = GraphicsUnit.Inch;
    g.PageScale = 0.01f;
    �
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Matteo D'Avena


Member
I am from Apricena, a small town in Puglia, Italy. I graduated at the University of Bari in 1998 in Computer Science. I worked for seven years in the field of business intelligence with one of the most important italian group producing software and services for the banking and financial sector.

Now I am a freelance software developer and consultant. I developed a product “GipsyReports – Visual Reporting System” that you can download, in demo version, from my personal web site http://www.webalice.it/matteo.davena/en GipsyReports application is localizable in Italian and English languages. For any further information about GipsyReports or custom developments, feel free to contact me at the address matteo.davena@alice.it.

I am very interested in your opinion about this product.

Thanks very much.
Occupation: Web Developer
Location: Italy Italy

Other popular List Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 23 of 23 (Total in Forum: 23) (Refresh)FirstPrevNext
GeneralPrinting a ListView that contains groups PinmemberMark F.10:32 23 Mar '09  
QuestionWhat's the license associated with your code? PinmemberZaki Saadeh7:24 27 May '08  
Questionhow i can bind treeview With My Table in Database Pinmemberwaleedmaklad10:35 9 Dec '07  
Questionhow can i bind my table with treeview In C# Pinsusswaleed10:32 9 Dec '07  
Generalhow to select printer? PinmemberSuha13:32 18 Nov '07  
QuestionRight-to-left PinmemberSohailB3:28 17 Nov '07  
GeneralThanks, but Pinmemberbart_net_master4:17 20 Aug '07  
Generalhttp://puzzle.tangoing.info/ Pinsusshttp://puzzle.tangoing.info/11:29 7 Dec '07  
GeneralGreat article...Thanks... PinmemberAbhishek_Gajab3:42 14 Aug '07  
GeneralInvalidArgument=Value of '1' is not valid for 'index'. PinmemberJordan Marrazzo18:59 4 Jul '07  
GeneralRe: InvalidArgument=Value of '1' is not valid for 'index'. PinmemberJordan Marrazzo19:33 4 Jul '07  
QuestionHow to print selected pages using this Printable ListView control Pinmemberdurgamic2:08 10 Apr '07  
GeneralHow can i select required pages Pinmemberdurgamic2:05 10 Apr '07  
QuestionPrint 2 listviews on same page [modified] Pinmembercsfong3319:10 14 Mar '07  
GeneralListview print PinmemberGeorgiio11:20 7 Jan '07  
GeneralPrinting with diffent dongs Pinmemberenlikil6:44 27 Oct '06  
GeneralAbout language Pinmemberjabulino21:44 19 Mar '06  
QuestionColumn order PinmemberColin Parker2:07 11 Oct '05  
AnswerRe: Column order PinmemberMircea Puiu2:43 11 Oct '05  
GeneralRe: Column order PinmemberColin Parker2:54 11 Oct '05  
GeneralRe: Column order PinmemberMircea Puiu3:43 11 Oct '05  
Generalpippaguru... PinmemberJonathanLivingstone21:41 19 Sep '05  
GeneralRe: pippaguru... PinmemberMatteo D'Avena21:57 19 Sep '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Sep 2005
Editor: Smitha Vijayan
Copyright 2005 by Matteo D'Avena
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project