好吧,我显然没有给google提供正确的查询,或者我现在已经发现了。我希望这个论坛上有人可以帮助我。
所以我有一个数据表,我正在基于一个datareader添加行,该数据表从通过数据库执行的sql查询获取其信息。到现在为止还挺好。现在,其中一个列被称为“分析”,如果前两列匹配,我需要它的背景颜色为绿色,否则需要红色。
如果我无法触摸背景颜色,我想插入一个特殊字符,任何不被解释为文本的javascript。
简单地说,我希望css控制来自代码隐藏的gridview。我看了,看起来无济于事。我找到了this家伙,但我没有检查他的解决方案是否适用于ASP.Net/C#网站。有什么想法吗?
答案 0 :(得分:3)
在 GridView_RowDataBound 事件中,获取要更改背景颜色的单元格,如果测试条件为真,则设置单元格的颜色。
/// <summary>
/// Handles gridview row data bound event.
/// </summary>
/// <param name="sender">Sender Object</param>
/// <param name="e">Event Argument</param>
protected void Gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
// We don’t want to apply this to headers.
if (e.Row.RowType == DataControlRowType.DataRow)
{
try
{
//your data-object that is rendered in this row, if at all required.
//Object obj = e.Row.DataItem;
//find the right color from condition
string color = condition ? "#ff9900" : "some-other-color";
//set the backcolor of the cell based on the condition
e.Row.Cells[4].Attributes.Add("Style", "background-color: " + color + ";");
}
catch
{
}
}
}
答案 1 :(得分:0)
protected void GVKeywordReport_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow pr = ((DataRowView)e.Row.DataItem).Row;
int oldPos = Convert.ToInt32(pr["oldposition"]);
int newPos = Convert.ToInt32(pr["newposition"]);
GVKeywordReport.HeaderRow.Cells[3].Text = txtfrmdate.Text;
GVKeywordReport.HeaderRow.Cells[4].Text = txtEndDate.Text;
GVKeywordReport.HeaderRow.BackColor = ColorTranslator.FromHtml("#B3B300");
e.Row.Cells[0].BackColor = ColorTranslator.FromHtml("#B3B300");
e.Row.Cells[5].BackColor = ColorTranslator.FromHtml("#FFFFFF");
if (oldPos == newPos)
{
e.Row.BackColor = ColorTranslator.FromHtml("#FF950E");
e.Row.Cells[6].Text = "No Change";
}
else if (oldPos > newPos)
{
e.Row.BackColor = ColorTranslator.FromHtml("#FFFFCC");
e.Row.Cells[6].Text = "Improved";
}
else
{
e.Row.BackColor = ColorTranslator.FromHtml("#FF0000");
e.Row.Cells[6].Text = "Decreased";
}
// e.Row.Cells[0].BackColor = ColorTranslator.FromHtml("#7DA647");
}
}