如何通过PrintDocument打印DataTable? [英] How to print DataTable via PrintDocument?

查看:62
本文介绍了如何通过PrintDocument打印DataTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在尝试在我的数据表中打印数据。我有6列,共有60行数据。



我设法生成了一个打印但有些问题。例如,它没有检测到页面可以有更多的行,所以我只看到大约25行,而其余的行已经被遗忘。然而,我做了一些关于检测天气的工作,它需要打印更多页面,它确实在预览中显示正确数量的页面,但它似乎只是复制了第一页。





首先:我需要限制打印到每页约20行的行(带列名)。

第二:我需要查看来自Datatable的所有数据正确打印在每个页面上(从第1页到第2页继续到第3页),这将使其达到60.

第三:在这个阶段,我甚至看不到页脚......但它应该打印在每页上。



这是我的代码到目前为止:



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:

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.

推荐答案

我建​​议快速浏览一下这篇文章: - 绝对新手的.NET打印指南 [ ^ ]



基本上你的问题是_PrintPage事件被调用e.HasMorePages设置为true时每页一次。



这意味着任何跟踪您在打印数据流中的位置的代码都必须在事件。



(这些位例如: -

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:-
int columnCount = tmp_Table.Columns.Count;
int maxRows = tmp_Table.Rows.Count;





和你的_PrintPage处理程序应该打印最后一行打印的行,直到没有行为止(在这种情况下设置e.HasMorePages = False)或者您已达到每页要打印的行数。


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


您好,

document_PrintPage将在每次e.HasMorePages时调用设置为True。我建议你使用两个全局变量来存储number_of_item和item_per_page_count,这样就不会打印重复的记录。

查看这篇文章,这可能对您有所帮助:

在C#中打印和预览多个页面

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


这篇关于如何通过PrintDocument打印DataTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆