Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / Windows Forms
Article

Printable ListView

Rate me:
Please Sign up or sign in to vote.
4.72/5 (42 votes)
19 Sep 20052 min read 221K   19.8K   82   50
A customized ListView control with printing capability.

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.

C#
// 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.

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

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


Written By
Web Developer MerchandisingPlaza.com
Italy Italy
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.

For four year I had been a freelance software developer and consultant.

Now I am the leading project manager for MerchandisingPlaza.com!

Thanks very much.

Comments and Discussions

 
Question5 Stars - Thanks Pin
Member 107763003-Jan-19 22:40
Member 107763003-Jan-19 22:40 
QuestionBlank Preview Pin
Member 107763003-Jan-19 0:51
Member 107763003-Jan-19 0:51 
AnswerRe: Blank Preview Pin
Member 107763003-Jan-19 22:39
Member 107763003-Jan-19 22:39 
QuestionHow to use this? Pin
Jeremy Mandinggin9-Mar-17 16:43
Jeremy Mandinggin9-Mar-17 16:43 
Questionhow i can maximezed form using dll call Pin
vbiste6-Mar-15 12:03
vbiste6-Mar-15 12:03 
QuestionHow can I Add Pages? Pin
FinalByte18-Nov-14 1:25
FinalByte18-Nov-14 1:25 
AnswerRe: How can I Add Pages? Pin
FinalByte20-Nov-14 20:05
FinalByte20-Nov-14 20:05 
QuestionThanks Pin
sungangmr30-Apr-13 20:59
professionalsungangmr30-Apr-13 20:59 
Questionhow can i apply this Pin
demir12318-Apr-13 5:53
demir12318-Apr-13 5:53 
Questionlistview Pin
Member 844235920-Nov-12 20:34
Member 844235920-Nov-12 20:34 
QuestionHELP PLEASE URGENT!! Pin
Member 844235919-Nov-12 18:54
Member 844235919-Nov-12 18:54 
QuestionLicences Pin
_Primrose1-Oct-12 5:16
_Primrose1-Oct-12 5:16 
AnswerRe: Licences Pin
Matteo D'Avena2-Oct-12 1:59
Matteo D'Avena2-Oct-12 1:59 
Questionsolved Pin
jacob waswa11-Aug-12 21:19
professionaljacob waswa11-Aug-12 21:19 
QuestionURGENT HELP NEEDED Pin
jacob waswa11-Aug-12 20:33
professionaljacob waswa11-Aug-12 20:33 
Questiongr8 Job ... thx Pin
Member 261443618-Nov-11 20:14
Member 261443618-Nov-11 20:14 
QuestionFitToPage bug Pin
wcm77713-Oct-11 6:36
wcm77713-Oct-11 6:36 
Questionprint with back color of the list view Pin
mahmoud19907-Sep-11 1:32
mahmoud19907-Sep-11 1:32 
AnswerRe: print with back color of the list view Pin
Valentin Pronkin30-Aug-15 22:33
Valentin Pronkin30-Aug-15 22:33 
General{"InvalidArgument=Value of '2' is not valid for 'index'.\r\nParameter name: index"} Pin
g1xx3r3-Jun-11 2:51
g1xx3r3-Jun-11 2:51 
GeneralRe: {"InvalidArgument=Value of '2' is not valid for 'index'.\r\nParameter name: index"} Pin
Member 1396582530-Aug-18 0:10
Member 1396582530-Aug-18 0:10 
QuestionRight-to-left Pin
User 67901896-Feb-11 13:14
User 67901896-Feb-11 13:14 
AnswerRe: Right-to-left Pin
Hamed Bonakdar25-Feb-11 2:40
Hamed Bonakdar25-Feb-11 2:40 
GeneralMy vote of 5 Pin
jcarter12121211-Oct-10 12:21
jcarter12121211-Oct-10 12:21 
GeneralMy vote of 5 Pin
Eduardo_David11-Jul-10 2:36
Eduardo_David11-Jul-10 2:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.