满足条件时突出显示GridView行

问题描述 投票:7回答:5

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

我很想知道在VS2005 version中,当满足条件时,是否可以在GridView中对highlight进行排序?例如。如果列Risk在该特定行的数据库中存储为高,则该行将为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.但颜色仍然没有变化。

任何人有任何想法?

c# asp.net visual-studio visual-studio-2005
5个回答
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;
        }
    }
}

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事件并抓住您的列中提到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


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;
            }

        }
© www.soinside.com 2019 - 2024. All rights reserved.