C# 满足条件时高亮显示GridView行
我正在使用C# 满足条件时高亮显示GridView行,c#,asp.net,visual-studio,visual-studio-2005,C#,Asp.net,Visual Studio,Visual Studio 2005,我正在使用vs2005c#服务器端编码 我很想知道,在VS2005版本中,当满足条件时,是否可以突出显示网格视图中的一行?例如,如果该特定行的数据库中将列风险存储为高,则该行将以红色突出显示 可能吗 编辑: 当前代码: protected void GridView1_OnRowDataBound(Object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { //
vs2005c#服务器端
编码
我很想知道,在VS2005版本
中,当满足条件时,是否可以突出显示网格视图中的一行?例如,如果该特定行的数据库中将列风险存储为高,则该行将以红色突出显示
可能吗
编辑:
当前代码:
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。但是颜色仍然没有改变
有人知道吗?是的,将onrowdabund=“yourGridview\u rowdabund”
添加到您的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;
}
}
}
您应该订阅网格的RowDataBound
事件,抓住列中提到风险高的行,然后将该行的背景色设置为您选择的突出显示颜色
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使用行数据绑定事件。在这种情况下,您可以根据您的条件添加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';");
}
}
在RowDataBound
try:
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
}
}
}
不,OnDataBound只会被解雇一次,这不是你想要的。尝试OnRowDataBound时是否显示任何错误?请确保您的“GridView_OnRowDataBound”方法设置为“public”。@RUiHAO检查我的解决方案,我认为.Text
更适用于数据绑定事件,而不是RowDataBound
,因为值实际上包含在控件中,而不是单元格中,因此DataBinder.Eval
应该适合您检查e.Row.Cells[3]。文本实际上等于“High”(大小写正确,没有多余空格等)。您可以使用Response.Write(“-”+e.Row.Cells[3].Text+“-”)来显示值。这是正确的,在我的页面顶部有我的第三列中的全部文本。请尝试暂时禁用“if(e.Row.Cells[3].Text==“H”){}”,只需省去“e.Row.BackColor=Color.Red;”。颜色现在变了吗?是的,整个表变为红色。然后它意味着“if(e.Row.Cells[3]。Text==“H”)”永远不会返回true。我看不到您的数据,因此无法说出它们不匹配的原因,但您应该能够识别两者之间的任何差异。如果您添加了对您的想法的描述,而不是简单的代码,这将很有帮助。
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;
}
}