我正在使用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.但颜色仍然没有变化。
任何人都有任何想法?
答案 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
}
答案 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;
}
}