Gridview中的Foreach行打印C#

时间:2017-09-11 16:42:35

标签: c# asp.net gridview foreach

我希望gridview的每一行都在下一行打印..这段代码只是一遍又一遍地获得相同的3行。我做错了什么?

foreach (GridViewRow row in poGridview.Rows)
    {
    String itemNum = poGridview.Rows[0].Cells[0].Text;        
    String house = poGridview.Rows[0].Cells[2].Text;
    String description = poGridview.Rows[0].Cells[1].Text;
        Paragraph itemLine1 = new Paragraph(@"" + itemNum + "         " + house + "          " + description, body);
        p.Add(itemLine1);   
    }

1 个答案:

答案 0 :(得分:1)

每次循环内都引用相同的行(poGridview.Rows[0])。将引用更改为row

foreach (GridViewRow row in poGridview.Rows)
{
    String itemNum = row.Cells[0].Text;        
    String house = row.Cells[2].Text;
    String description = row.Cells[1].Text;
    Paragraph itemLine1 = new Paragraph(@"" + itemNum + "         " + house + "          " + description, body);
    p.Add(itemLine1);   
}