使用 IF ELSE 语句循环遍历 Gridview

Looping Through Gridview with IF ELSE statement

我正在尝试向 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));
                }
            }

grid.Rows[0] returns 网格中的第一行,要循环所有行。所以使用循环变量 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));
    }
}