Click here to Skip to main content
15,891,684 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

i am attempting to print data in my Datatable. I have 6 columns with 60 rows of data in total.

I have managed to produce a print but some problems. For one it does not detect that there are more rows that the page can take, so i only see about 25 rows while the rest have gone to oblivion. I did however do some work on detecting weather it needs to print more pages and it does show correct amount of pages in the Preview, however it seems to just duplicating the first page.


First: I need to limit rows printed to about 20 rows of per page (with column names).
Second: I need to see all data from Datatable correctly printed on each page (continuing from page 1 to page 2 to page 3) which would make it to 60.
Third: Well at this stage i cannot even see the footer... but it should be printed on each page.

Here is my code so far:

C#
private void document_PrintPage(object sender, PrintPageEventArgs e)
        {
            string header = "Master List    " + CI.getDateTime("dd/MM/yyyy HH:mm");
            string footer = string.Empty;
            int columnCount = tmp_Table.Columns.Count;
            int maxRows = tmp_Table.Rows.Count;

            using (Graphics g = e.Graphics)
            {
                Brush brush = new SolidBrush(Color.Black);
                Pen pen = new Pen(brush);
                Font font = new Font("Arial", 10);
                SizeF size;

                int x = 0, y = 0, width = 130;
                float xPadding;

                // Here title is written, sets to top-middle position of the page
                size = g.MeasureString(header, font);
                xPadding = (width - size.Width) / 2;
                g.DrawString(header, font, brush, x + 250, y + 5);

                x = 0;
                y += 30;

                // Writes out all column names in designated locations, aligned as a table
                foreach (DataColumn column in tmp_Table.Columns)
                {
                    size = g.MeasureString(column.ColumnName, font);
                    xPadding = (width - size.Width) / 2;
                    g.DrawString(column.ColumnName, font, brush, x + xPadding, y + 5);
                    x += width;
                }

                x = 0;
                y += 30;

                // Process each row and place each item under correct column.
                foreach (DataRow row in tmp_Table.Rows)
                {
                    rowcount++;

                    for (int i = 0; i < columnCount; i++)
                    {
                        size = g.MeasureString(row[i].ToString(), font);
                        xPadding = (width - size.Width) / 2;

                        g.DrawString(row[i].ToString(), font, brush, x + xPadding, y + 5);
                        x += width;
                    }

                    e.HasMorePages = rowcount - 1 < maxRows;

                    x = 0;
                    y += 30;
                }

                footer = "Total: " + maxRows + " |Signed:..........................";
                size = g.MeasureString(footer, font);
                xPadding = (width - size.Width) / 2;
                g.DrawString(footer, font, brush, x + 250, y + 5);

                x = 0;
                y += 30;
            }
        }


Any advice is appreciated.
Posted
Updated 22-Jul-15 17:15pm
v3

I'd suggest a quick scan through the article:- An absolute beginner's guide to printing in .NET[^]

Basically your problem is that the _PrintPage event is called once for every page while e.HasMorePages is set to true.

This means any code that tracks where you are in the data stream you are printing has to happen outside that event.

(These bits for example:-
C#
int columnCount = tmp_Table.Columns.Count;
int maxRows = tmp_Table.Rows.Count;

)
and your _PrintPage handler should print rows from the last one printed until either no rows are left (in which case set e.HasMorePages = False) or you have reached the number of rows you want to print per page.
 
Share this answer
 
Comments
Silver13 23-Jul-15 20:04pm    
I understand the how this should go down, and i have followed your document as well as numerous suggestions on how to fire e.HasMorePages event, but i think my problem here is coming from the "foreach (DataRow row in tmp_Table.Rows)"

I have set the limit to be 20 rows and once reached e.HasMorePages is set to true. That should basically print next page with the another 20 rows out of 60 until end. In my scenario it should print 3 pages, but that is not happening. It just duplicates the first 20 rows. I am not sure how else to handle this and why the e.HasMorePages is not going to next page.
Duncan Edwards Jones 24-Jul-15 2:33am    
Yes - but "foreach" always starts at the first row. You need to use an integer row index (initialised in the _BeginPrint) instead.
Silver13 27-Jul-15 22:16pm    
Also, very important to have is return statement for everytime e.HasMorePages is set to true, otherwise code wont realize it needs to start new page.

Thanks for the help.
Hello,
"document_PrintPage" will call every time when e.HasMorePages is set to True. I suggest you to use two global variables to store the number_of_item and item_per_page_count , so that no duplicate record can print .
Check this article, this may help you :
Printing and Previewing multiple pages in C#
Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900