gridview根据条件改变单元格颜色

我想只要表格里有等于1的单元格就变颜色,我现在只能改变指定列的颜色,高手帮我看下,谢谢了
int k = dss.Tables[0].Columns.Count;//定义表x有多少列
string[] shuju = new string[k];
for (int i1 = 0; i1 < k; i1++)
{
shuju[i1] = Convert.ToString(dss.Tables[0].Columns[i1].ColumnName);//原新表的字段名数组
}
foreach (string a6 in shuju)
{
for (int i = 0; i <= dss2.Tables[0].Rows.Count - 1; i++)
{
if (GridView2.Rows[i].Cells[0].Text == "b")
{
GridView2.Rows[i].Cells[0].Attributes.Add("bgcolor", "gray");
}
}
}
我想循环遍历,表里的每个单元格,判断,变颜色
我想要Cells[0]里不是只判断第一个,而是循环每个单元格判断

还是我自己研究出来了!!
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Text == "1")
{
e.Row.Cells[i].BackColor = System.Drawing.Color.Red;
string headtxt = GridView2.HeaderRow.Cells[i].Text;
string sxstr = headtxt.Substring(0, headtxt.IndexOf("_"));//取得列名
for (int j = 0; j < e.Row.Cells.Count; j++)
{
if (GridView2.HeaderRow.Cells[j].Text == sxstr)
{
e.Row.Cells[j].BackColor = System.Drawing.Color.Red;
}
}

}
}

第1个回答  推荐于2016-03-14
这个很简单
关键是要用对方法

最简单的方法是客户端来变色

jquery
$("#GridView2 tr td:contains('1')").css("background-color","#ddd")本回答被提问者采纳
第2个回答  2010-06-09
这个我想你用Jquery实现就很简单了

不用写在代码里 非要写不过就是循环而已

protected void ShowLight(GridView gv)
{
int j = gv.Rows.Count;
for (int t = 0; t < j; t++)
{
foreach(TableCell tc in gv.Rows[t].Cells)
{
if (tc.Text == "1")
{
tc.BackColor = System.Drawing.Color.Red;
}
}
}
}
第3个回答  2010-06-10
foreach(GridViewRow row in GridView2.Rows)
{
foreach(GridViewCell cell in row.Cells)
{
cell.Style.BackColor = Color.Red;
}
}