给gridView的单元格加背景色

gaoqingchun2007 2009-09-12 12:11:21
我有一个GridView,id为GridView1 如下:
id name 1001001 1001002 1001003
1 n1 20 30 50
2 n2 10 40
3 n3 20 30 50
4 n4 20
5 n5 20 30 50
6 n6 40

我这个GridView是自动绑定的,因为1001001、1001002是在sql里行转为列的,我不确定后面还有多少个编号
我现在想让GridView 的列1001001、1001002、1001003.。。。这些列中只要有为空的数据或是为零的数据都更改其背景色为红色
请问各位高手该怎么做
...全文
179 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zf679 2010-09-16
  • 打赏
  • 举报
回复
但是否对双方的是否
Teng_s2000 2009-09-12
  • 打赏
  • 举报
回复
如果列不固定,可以加一个循环
for (int i = 0; i < e.Row.Cells.Count; i++)
{
}
  • 打赏
  • 举报
回复

for (int i = 0; i <GridView1.Rows.Count; i++)
{
for(int j=0;j<GridView1.Columns.Count;j++)
{
if(GridView1.Rows[i].Cells[j].Text.ToString()==" ")
{
GridView1.Rows[i].Cells[j].BackColor = System.Drawing.Color.Red;
}
}
}
Teng_s2000 2009-09-12
  • 打赏
  • 举报
回复

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// 设置行的背景颜色
if (int.Parse(e.Row.Cells[1].Text) == 0)
{
e.Row.BackColor = System.Drawing.Color.Red;
}
else
{
e.Row.BackColor = System.Drawing.Color.Gray;
}
// 设置单元格的颜色
if (int.Parse(e.Row.Cells[1].Text) == 0)
{
e.Row.Cells[1].BackColor = System.Drawing.Color.Green;
}


}

gaoqingchun2007 2009-09-12
  • 打赏
  • 举报
回复
相当于让后面三列为空的颜色变为黄色,不为空的变为绿色
chenjianyong94 2009-09-12
  • 打赏
  • 举报
回复
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// 设置行的背景颜色
if (int.Parse(e.Row.Cells[1].Text) == 0)
{
e.Row.BackColor = System.Drawing.Color.Red;
}
else
{
e.Row.BackColor = System.Drawing.Color.Gray;
}
// 设置单元格的颜色
if (int.Parse(e.Row.Cells[1].Text) == 0)
{
e.Row.Cells[1].BackColor = System.Drawing.Color.Green;
}


}

  • 打赏
  • 举报
回复
单元格的背景色嘛!~~难道不对吗?你前两列也有空的吗?要你想限定从第三列开始那么列循环的时候就从第三列开始呀!~~
gaoqingchun2007 2009-09-12
  • 打赏
  • 举报
回复
是单元格变色
sdjz1988sd 2009-09-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 teng_s2000 的回复:]
C# codeprotectedvoid GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{// 设置行的背景颜色if (int.Parse(e.Row.Cells[1].Text)==0)
{
e.Row.BackColor= System.Drawing.Color.Re¡­
[/Quote]顶
gaoqingchun2007 2009-09-12
  • 打赏
  • 举报
回复
又不是每个为空的都设置背景色,除了前两列以外的其他列
wuyq11 2009-09-12
  • 打赏
  • 举报
回复
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView oRow;
if (e.Row.RowType == DataControlRowType.DataRow)
{
oRow = (DataRowView)e.Row.DataItem;
if (string.IsNullOrEmpty(oRow["value"].ToString())||oRow["value"].ToString().Equals("0"))
{
e.Row.BackColor = System.Drawing.Color.Red;
}
else
{
e.Row.BackColor = System.Drawing.Color.Blue;
}

//或者对某一单元格着色,最后一列
if (Convert.ToDouble(oRow["value"].ToString()) < 0)
{
e.Row.Cells[e.Row.Cells.Count - 1].BackColor = System.Drawing.Color.Red;
}
else
{
e.Row.Celle[e.Row.Cells.Count - 1].BackColor = System.Drawing.Color.Blue;
}
}

}
也可遍历设置颜色

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧