2011-12-12 81 views
7

我正在使用VS2005 C# Server-side编码。当条件满足时突出显示GridView行

我很想知道,在VS2005 version,是否有可能highlight GridView条件满足时的行?例如。如果列风险存储为在该特定行的数据库中,该行将为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但颜色依然不改。

任何人有任何想法?

+0

没有,只OnDataBound得到射击一次这是不是你想要的。当您尝试OnRowDataBound时它显示任何错误吗? – S200

+0

确保您的'GridView_OnRowDataBound'方法设置为'public'。 – S200

+1

@RUHAHAO检查我的解决方案,我认为'.Text'将更适用于DataBound事件而不是'RowDataBound',因为该值实际上由控件包含,而不是单元,所以'DataBinder.Eval'应该适合您 – V4Vendetta

回答

10

是的,将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; 
     } 
    } 
} 

类似的东西。

+0

我试过你的方法,但颜色仍然没有改变。 – gymcode

+0

检查你的e.Row.Cells [3] .Text真的等于“高”(正确的情况下,没有额外的空间等)。您可以使用Response.Write(“ - ”+ e.Row.Cells [3] .Text +“ - ”)来显示值。 – S200

+0

这是正确的,在我的页面顶部充满了我的第三列中的文字。 – gymcode

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 Formatting the GridView Based on the Underlying Data

行的网格,手抓牢的
3

使用RowDataBound事件。在这种情况下,你会得到可能会同意你的条件

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尝试添加基于CSS:

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 
     } 
    } 
} 
+0

请参阅'http:// blog.jerryleelajohn.com/2009/08/rowdatabound-vs2005.html' –

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

     } 
+0

如果您添加了对您的想法的描述,而不是简单的代码,这将会很有帮助。 –

相关问题