ASP.NET GridView如果单元格行值相等,则...

3
我正在填充一个数据表,然后将其绑定到网格视图上。现在,我正在读取网格中的行,并且如果值等于[x],则将该行着色。
问题在于,当我尝试在页面上显示已经着色的行时,它会被重复显示。假设我已经着色了一行,但response.write的结果会出现100次相同的内容。以下是我的代码,请希望有人能帮忙:
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    string alert = Request.QueryString["check"];

    // loop over all the Rows in the Datagridview 
    foreach (GridViewRow row in gv1.Rows)
    {
        // if condition is met color the row text 

        if (gv1.Rows[0].Cells[0].Text.ToString() == alert)
        {
            Session ["fn"] = gv1.Rows[0].Cells[2].Text;
            gv1.Rows[0].ForeColor = Color.Red;
    }
        Response.Write(Session["fn"]);
}
1个回答

2
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    string alert = Request.QueryString["check"];

        if (e.Row.Cells[0].Text.ToString() == alert)
        {
            Session ["fn"] = e.Rows.Cells[2].Text;
            e.Rows.ForeColor = Color.Red;

        Response.Write(Session["fn"]);
        }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接