int linesPerPage = 75;//75行每页
     int lineNo = 0;//存放当前要打印行的行号
     int lineQty; //存放总共要打印的行数,可以是一个估算值,略大于实际行数
     int printingPageNo = 0; //当前打印的页号
     int count = 0; //当前页的行数计数
     public static void printerpage(PrintPageEventArgs e)
     {
        string line6 = "-----------------------------------------------------------";
        drawFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;
        StringFormat drawFormat1 = new StringFormat();
        drawFormat1.FormatFlags = StringFormatFlags.DisplayFormatControl;
        x = 2;
        y =0;
      if (!e.HasMorePages)
      {
       ds = dbTotal.QueryData("select * from Billlist where stant = 'takeaway'", "list");
       y = y + 10;
        e.Graphics.DrawString("No", new Font("verdana", 8, FontStyle.Regular), Brushes.Black, 0, y, drawFormat1);
      x = x + 45;
      e.Graphics.DrawString("TbNo", new Font("verdana", 8, FontStyle.Regular), Brushes.Black, x, y);
      x = x + 75;
      e.Graphics.DrawString("Time", new Font("verdana", 8, FontStyle.Regular), Brushes.Black, x, y);
      x = x + 130;
      y = y + 20;
      lineQty = ds.Tables["list"].Rows.Count + 5;
     }
    while (count < linesPerPage && lineNo < lineQty)
     {
      if (lineNo < ds.Tables["list"].Rows.Count) //由于lineNo用于表中的下标值,因此需要加上该判断
       {
         string no = ds.Tables["list"].Rows[lineNo][7].ToString();
         string tabno = ds.Tables["list"].Rows[lineNo][5].ToString();
         string time = ds.Tables["list"].Rows[lineNo][3].ToString().Substring(0, 10);
         string tolprice = ds.Tables["list"].Rows[lineNo][1].ToString();
         e.Graphics.DrawString(no, new Font("verdana", 8, FontStyle.Regular), Brushes.Black, x, y, drawFormat1);
         x = x + 50;
         e.Graphics.DrawString(tabno, new Font("verdana", 8, FontStyle.Regular), Brushes.Black, x, y);
         x = x + 50;
         e.Graphics.DrawString(time, new Font("verdana", 8, FontStyle.Regular), Brushes.Black, x, y);
         x = x + 150;
 e.Graphics.DrawString(tolprice,new Font("verdana",8,FontStyle.Regular),Brushes.Black,x,y)                  
x = 2;
          y = y + 15;
          count++;
          }
          lineNo++;
                   
       }
       if (lineQty > lineNo)
       {
         e.HasMorePages = true;
           count = 0;
           printingPageNo++;
            circle = 0;
         }
        else
        {
         e.Graphics.DrawString(line6, new Font("verdana", 8, FontStyle.Regular), Brushes.Black, 0, y);//-----         线
         count++;
         e.HasMorePages = false;
            
         }
}分页打印,页是分了,但就是打在第一页

解决方案 »

  1.   

    if (lineQty > lineNo)
    好像不会执行啊
    while (count < linesPerPage && lineNo < lineQty)
    你这样进行循环
      

  2.   

    if (lineQty > lineNo)
    会执行啊,要不怎么会回过来把第2页也打在第一页呢? 
    while (count < linesPerPage && lineNo < lineQty)
    这个循环是这样,当每页count行数 比75小,说明还在一页;lineNo < lineQty则代表 lineNo是表里的行数比要打印的行数小。 都成立就循环啊
      

  3.   

    我单步执行,发现printpage进去2次的,但是2次进去完才会运行                printDocument1.Print();//开始打印 
    然后2页就都打在第一页
      

  4.   

    你代码不全,说不太准.你试试:(1)根踪一下,是否重复打印了,可以打印下一页时去掉已打印的内容行.
      this.printDS.Tables[0].Rows.RemoveAt(i);
        
      if(this.printDS.Tables[0].Rows.Count   >   0)
     {   
        e.HasMorePages   =   true;   
      }
     else
     {   
        e.HasMorePages   =   false;   
      }   (2) 打印一下页前
      Graphics.Clear()
      

  5.   

    没有时间细细的分析了。while (count < linesPerPage && lineNo < lineQty)
    if (lineQty > lineNo)应该是以上的两个地方出的问题。
      

  6.   


    我解决了,但是自己想不通的。我是将循环放到if(!e.HasMorePages)这个条件里。其实我单步跟踪发现每打一页都要进2次printpage。然后e.HasMorePages状态就自己变成false了。哎~~再研究研究。谢谢你
      

  7.   

    lineNo++ 后,如果是lineQty=2页, 则 lineQty=lineNo  执行的是 else { }if (lineQty > lineNo)
           {
             e.HasMorePages = true;
               count = 0;
               printingPageNo++;
                circle = 0;
             }
            else
            {
             e.Graphics.DrawString(line6, new Font("verdana", 8, FontStyle.Regular), Brushes.Black, 0, y);//-----         线
             count++;
             e.HasMorePages = false;
                
             }
      

  8.   

    lineQty是需要循环打印的总行数,lineNo是打印到的行号,就是要让他在lineQty=lineNo 停止分页,置e.HasMorePages = false;啊