【问题标题】:Calculate the total number of pages that will print from a DataGridView计算将从 DataGridView 打印的总页数
【发布时间】:2013-09-04 01:23:59
【问题描述】:

我正在尝试计算将从 DataGridView 打印的总页数。 一旦总列长度大于总打印区域,将打印新页面。 对于每个新页面,它将始终打印第 0 列,因此需要添加列宽才能获得正确的计算。

这是我目前所拥有的,似乎总是缺少页码

//dgv = the DataGridView
//RectangleF printable_area = MarginBounds

float total_width = 0;

//grab the width of each column
for (int i = 0; i < dgv.ColumnCount; i++)
{
    total_width += dgv.Columns[i].HeaderCell.Size.Width;
}
//divide the total width by the printable area's width
int pages = (int)Math.Ceiling(total_width / (printable_area.Size.Width));

//add to the total width the size of column 0 * the number of pages
total_width += dgv.Rows[0].Cells[0].Size.Width * pages;

//return the total number of pages that will be printed
return (int)Math.Ceiling(total_width / (printable_area.Size.Width));

【问题讨论】:

    标签: c# math printing datagridview


    【解决方案1】:

    起初我认为您实际上会拥有比必要更多的页面,因为您在计算中包含了第一页的 Column[0] 两次(我认为变量 i 应该从 1 开始),但随后我意识到这个计算

    int pages = (int)Math.Ceiling(total_width / (printable_area.Size.Width));
    

    将假定列可以分布在页面上。

    假设您有 4 列,每列 100 宽。现在假设您的可打印区域的宽度为 150。忽略在每页上打印第一列的要求,这将为您提供 3 页,因为 400/150 是 2.67,四舍五入为 3。但是,您真正想要的,是 4 页,因为两列永远不能放在一页上,并且宽度为 50 的额外“间隙”每页无法使用。

    这是假设您不想在每页上打印一半或部分列。如果这是您的意图,那么我看不出您的代码有任何进一步的问题

    【讨论】:

    • 我切换到 PageBounds 并在需要的地方进行了调整,现在它可以正常工作了。我逐步浏览了上面的代码并将其写在纸上,调整后计算是正确的,因此 Column[0] 没有两次添加到第 1 页。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多