当满足条件时,突出显示GridView行
本文关键字:显示 GridView 满足 条件 | 更新日期: 2023-09-27 18:17:37
我使用VS2005 C# Server-side
编码。
我很想知道,在VS2005 version
, highlight
是可能的行在GridView时满足条件?例如,如果列Risk在该特定行的数据库中存储为high,则该行将为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。但是颜色仍然没有改变。
有人知道吗?
是的,将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;
}
}
}
使用rowdataboundevent。在这种情况下,您可以根据条件
添加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
事件,并抓住您的列提到风险为高的行,然后将该行的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
in 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
}
}
}
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;
}
}