满足条件时突出显示GridView行

时间:2011-12-12 06:11:01

标签: c# asp.net visual-studio visual-studio-2005

我正在使用VS2005 C# Server-side编码。

我很想知道在VS2005 version中,当条件满足时,是否可以在GridView中highlight行?例如。如果风险列在该特定行的数据库中存储为,则该行将为highlighted in Red

有可能吗?


修改

当前代码:

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.但颜色仍然没有变化。

任何人都有任何想法?

5 个答案:

答案 0 :(得分:11)

是的,将OnRowDataBound="yourGridview_RowDataBound"添加到您的gridview。每个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 :(得分: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';");

    }

  }

答案 2 :(得分:1)

您应该订阅网格的RowDataBound事件并抓住您的列中提到Risk为High的行,然后将行的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 Formatting the GridView Based on the Underlying Data

答案 3 :(得分: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
        }
    }
}

答案 4 :(得分:-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;
            }

        }