2015-07-10 114 views
0

我试图向GridView添加条件语句;但是,它似乎只在第一行工作。使用IF ELSE语句循环Gridview

我有一个HiddenField,用于提取我的折扣值,然后是一个标签 ,其中我返回所述值,只有当值不为0.00时,才会创建一个中断。 我假设我可以简单地通过网格行循环来完成这一点;然而,如前所述,它只是在第一行工作。这里是我的代码:

// Number of rows in grid 
      int rowsCount = grid.Rows.Count; 
      //Loop through the rows 
      for (int i = 0; i < rowsCount; i++) 
      { 
       Label discountLabel = (Label)(grid.Rows[0].FindControl("discountLabel")); 
       HiddenField discount = (HiddenField)(grid.Rows[0].FindControl("HiddenField1")); 
       string discountValue = discount.Value; 
       if (discountValue == "0.00") 
       { 
        discountLabel.Text = "<br />"; 
       } 
       else 
       { 
        discountLabel.Text = "NOW&nbsp;" + (String.Format("{0:c}", discountValue)); 
       } 
      } 
+1

变化grid.Rows [0]到grid.Rows [I] – oppassum

回答

0

grid.Rows[0]返回第一行中的网格,要循环的所有行。因此,使用循环变量i代替:

for (int i = 0; i < rowsCount; i++) 
{ 
    Label discountLabel = (Label)(grid.Rows[i].FindControl("discountLabel")); 
    HiddenField discount = (HiddenField)(grid.Rows[i].FindControl("HiddenField1")); 
    string discountValue = discount.Value; 
    if (discountValue == "0.00") 
    { 
     discountLabel.Text = "<br />"; 
    } 
    else 
    { 
     discountLabel.Text = "NOW&nbsp;" + (String.Format("{0:c}", discountValue)); 
    } 
} 
+0

哦善良,要么我是盲人或没有足够的咖啡吧!谢谢。我会在10分钟内标记为正确的答案。 – Faron

+0

@Faron:顺便说一句,如果'discountValue'是一个字符串,则不能使用'String.Format(“{0:c}”,discountValue)''。那么你不会得到货币格式,因为它只适用于像'decimal'或'double'这样的数字类型。因此先解析它:'十进制折扣=十进制.Parse(折扣值)'。现在你可以使用String.Format(“{0:c}”,折扣)' –