我正在尝试向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 " + (String.Format("{0:c}", discountValue));
}
}
答案 0 :(得分: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 " + (String.Format("{0:c}", discountValue));
}
}