在3行之后打破Foreach

时间:2017-10-02 16:11:29

标签: c# pdf gridview foreach

我试图在我的foreach中的每个3个循环之后进行分页,但似乎无法触发它。欢迎任何帮助是我的代码打印正常,但我想每页3个GridView行。

 int i = 0;
        // for each row in Gridview do this part of the PDF print
        foreach (GridViewRow row in poGridview.Rows)
        {
            if ((i++) % 3 == 0)
            {
                pdfDoc.NewPage();
            }

            // Declare all values
            String itemNum = row.Cells[0].Text;
            String house = row.Cells[2].Text;
            String staic = row.Cells[4].Text;
            String description = row.Cells[1].Text.Replace("&", "");
            String qtOrd = row.Cells[5].Text;
            String serial = row.Cells[12].Text.Replace(" ", " ");
            String dkQty = row.Cells[8].Text;
            String stQty = row.Cells[9].Text;
            String deptNum = row.Cells[11].Text.Replace(" ", " "); ;
            String whBarCode = row.Cells[13].Text.Replace(" ", " ");
            String turnA = row.Cells[19].Text;
            String turnN = row.Cells[20].Text;
            String turnC = row.Cells[21].Text;
            String conversion = row.Cells[7].Text;
            String unit = row.Cells[6].Text;
            decimal ucorq = decimal.Parse(qtOrd) - decimal.Parse(stQty);

            // Warehouse barcode
            Barcode39 whlsc = new Barcode39();
            whlsc.Code = whBarCode.Replace(" ", " ");

            // Turn around barcode
            Barcode39 turnAr = new Barcode39();
            turnAr.Code = (poNum + "$I" + itemNum + "$M");

            // Set up lines for paragraph 
            Paragraph itemLine1 = new Paragraph(@"" + itemNum + "              " + house + "              " + description, body);
            Paragraph itemLine2 = new Paragraph(@"          Status -> " + staic + "                                          Ordered:      " + qtOrd + " " + unit, body);
            Paragraph itemLine3 = new Paragraph(@"STOCKING: " + stQty + " EA          CONVERSION FACTOR: " + conversion + "     ", body);
            Paragraph itemLine4 = new Paragraph(@"Dock: " + dkQty + "                    Stock: " + stQty + "                         Open:     " + ucorq.ToString(), body);
            Paragraph itemLine5 = new Paragraph(@"Serial #: " + serial, body);
            Paragraph itemLine6 = new Paragraph(@"VND CATALOG: " + serial + "   ", body);
            Paragraph itemLine7 = new Paragraph(@"JOB NUMBER: " + "                                        DEFAULT STOCK LOC: ", body);
            Paragraph itemLine8 = new Paragraph(@"                                                                                                                           ", body);


            // Select command for the comments for each item
            OleDbCommand comtSearch = new OleDbCommand("SELECT CAST(POCOMT.CMNT1 as char(40) ccsid 37) as cmt1, CAST(POCOMT.CMNT2 as char(40) ccsid 37) as cmt2 FROM POCOMT LEFT OUTER JOIN POITEM ON POITEM.ITNBR = POCOMT.ITNBR"
                + $" AND POITEM.ORDNO = POCOMT.ORDNO AND POITEM.HOUSE = POCOMT.HOUSE AND POITEM.LINSQ = POCOMT.LINSQ WHERE POCOMT.ITNBR = '{itemNum}' AND POCOMT.ORDNO = '{poNum}'", cn);

            OleDbDataAdapter adp = new OleDbDataAdapter(comtSearch);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            commentGridview.DataSource = dt;
            commentGridview.DataBind();


            // Add Lines to page
            p.Add(itemLine1);
            p.Add(new Paragraph("\n"));
            p.Add(new Paragraph("\n"));
            p.Add(new Paragraph("\n"));

            p.Add(new Chunk(turnAr.CreateImageWithBarcode(cb, null, null), 0, 0));
            p.Add(new Paragraph("\n"));
            p.Add(itemLine2);
            p.Add(new Paragraph("\n"));
            p.Add(itemLine3);
            //p.Add(new Chunk(turnAr.CreateImageWithBarcode(cb, null, null), 0, 0));
            p.Add(new Paragraph("\n"));
            p.Add(itemLine4);
            p.Add(new Paragraph("\n"));
            //p.Add(itemLine5);         

            // loop through the comment fields on each item and print
            foreach (GridViewRow cmtRow in commentGridview.Rows)
            {
                String cmnt1 = cmtRow.Cells[0].Text.Replace(" ", " "); ;
                String cmnt2 = cmtRow.Cells[1].Text.Replace(" ", " "); ;
                Paragraph cmt1 = new Paragraph(@"" + cmnt1, body);
                Paragraph cmt2 = new Paragraph(@"" + cmnt2 + " ", body);
                p.Add(cmt1);
                p.Add(new Paragraph("\n"));
                p.Add(cmt2);
            }

            p.Add(new Paragraph("\n"));
            p.Add(itemLine6);
            p.Add(new Paragraph("\n"));
            p.Add(itemLine7);
            p.Add(new Paragraph("\n"));
            p.Add(itemLine8);
            p.Add(new Chunk(whlsc.CreateImageWithBarcode(cb, null, null), 0, 0));
            p.Add(new Paragraph("\n"));
            p.Add(hr);
            p.Add(new Paragraph("\n"));

        }


        p.Add(end);
        p.Add(new Paragraph("\n"));

    pdfDoc.Add(p);
    pdfDoc.NewPage();
    }
    // Close Document
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.ContentType = "Application/pdf";
    Response.End();

3 个答案:

答案 0 :(得分:2)

   int i = 0; //initiate variable outside foreach loop
   foreach (GridViewRow row in poGridview.Rows)
   {
      if (i == 3)
      {
         pdfDoc.NewPage();
         i=0; //when i=3, resets to 0 and calls newPage method
      }
      i++; //+1 to i
   }

答案 1 :(得分:1)

您在循环的每次迭代开始时将i设置为零。所以,它永远不会达到3.你可能想要这样的东西:

int i = 0;
foreach( GridViewRow row in poGridview.Rows )
{
    if( i % 3 == 0 && i > 0 )
    {
        pdfDoc.Add(p);
        pdfDoc.NewPage();
        p = <Create a new p>
    }

    i++;
}

答案 2 :(得分:0)

int i = 0;
foreach(GridViewRow row in poGridview.Rows)
{
    if ((i++) % 3 == 0)
    {
        pdfDoc.NewPage();
    }
}