当满足条件时,突出显示GridView行

7

我正在使用 VS2005 C# 服务器端 编程。

我想知道在 VS2005 版本 中,是否可以在满足条件时 高亮显示 GridView 中的一行?例如,如果数据库中该行的 Risk 列存储为 high,则该行将被 用红色突出显示

这是可能的吗?


编辑:

当前代码:

protected void GridView1_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
    // do your stuffs here, for example if column risk is your third column:
    if (e.Row.Cells[3].Text == "H")
    {
        e.Row.BackColor = Color.Red;
    }
}
}

我假设列单元格从0开始,所以我的单元格在第3个位置。但颜色仍然没有改变。 有人有任何想法吗?

1
不,OnDataBound只会被触发一次,这不是你想要的。当你尝试使用OnRowDataBound时,它是否显示任何错误? - Pupper
1
请确保你的 'GridView_OnRowDataBound' 方法被设置为 'public'。 - Pupper
1
@RUiHAO,请检查我的解决方案,我认为在DataBound事件中使用.Text会更适用,而不是RowDataBound,因为该值实际上包含在控件中而不是单元格中,所以DataBinder.Eval应该适合你。 - V4Vendetta
5个回答

11

是的,向您的gridview添加OnRowDataBound="yourGridview_RowDataBound"。此事件将为每个gridview行触发。

在代码后台中,加入以下内容:

public void yourGridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // do your stuffs here, for example if column risk is your third column:
        if (e.Row.Cells[2].Text == "high")
        {
            e.Row.BackColor = Color.Red;
        }
    }
}

1
请检查您的 e.Row.Cells[3].Text 是否真的等于 "High"(正确情况,没有额外的空格等)。您可以使用 Response.Write("-" + e.Row.Cells[3].Text + "-") 来显示这些值。 - Pupper
没错,我的页面顶部满是第三列的文本。 - gymcode
1
尝试暂时禁用 "if (e.Row.Cells[3].Text == "H") { }",并且只留下 "e.Row.BackColor = Color.Red;"。现在颜色是否改变? - Pupper
是的,整个表格都会变成红色。 - gymcode
那么这意味着 "if (e.Row.Cells[3].Text == "H")" 永远不会返回 true。我看不到你的数据,所以无法确定为什么它们不匹配,但你应该能够识别出两者之间的任何差异。 - Pupper

3
使用RowDataBound事件。在此事件中,您可以根据条件添加基于CSS的样式。
 void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Logic for High
      if(e.Row.Cells[1].Text > 100)
      //set color
      e.Row.Attributes.Add("style", "this.style.backgroundColor = '#FFFFFF';");

    }

  }

1
你应该订阅表格的 RowDataBound 事件,并捕获包含高风险列的行,然后将该行的 BackColor 设置为你选择的突出显示颜色。
 If (e.Row.RowType == DataControlRowType.DataRow)
 {
        //DataBinder.Eval(e.Row.DataItem,"Risk"))
        //if this is high then set the color
        e.Row.BackColor = Drawing.Color.Yellow
 }

MSDN 根据底层数据格式化 GridView


1

RowDataBound中尝试:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // searching through the rows
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      if(int.Parse(DataBinder.Eval(e.Row.DataItem,"Risk").ToString()) > 100)
        {
            e.Row.BackColor = Color.FromName("#FAF7DA"); // is a "new" row
        }
    }
}

-1
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.BackColor = Color.Yellow;

            Label l1 = (Label)e.Row.FindControl("lblage");
            if(Convert.ToInt32( l1.Text)>=30)
            {
                e.Row.BackColor = Color.Tomato;
            }

        }

1
如果您添加了一个关于您的想法的描述,而不是仅仅代码,那将会非常有帮助。 - Eduard Malakhov

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