GridView中如何根据某一列值改变行的颜色

wsdydmw 2010-07-14 11:46:13
我的一个GridView中有[编号] [姓名] [成绩]三列
如果某一行的[成绩]的值小于60 则将这行背景设置为红色。
...全文
914 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wsdydmw 2010-07-16
  • 打赏
  • 举报
回复
11楼的方法我算学习到了
ajq1989 2010-07-15
  • 打赏
  • 举报
回复
private void DataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if (this.DataGridView1.Columns[e.ColumnIndex].Name.ToString().Equals("Column2"))
{
if (DataGridView1["Column2", e.RowIndex].Value.ToString().ToLower().Equals("true"))
{
DataGridView1["Column1", e.RowIndex].ReadOnly = false;
}
else
{
DataGridView1["Column1", e.RowIndex].ReadOnly = true;
}
}
}
SamuDra 2010-07-15
  • 打赏
  • 举报
回复
1,2L精神可嘉,..大半夜的不容易..= =
恭喜lz功德圆满.
yqyqyoyo 2010-07-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sp1234 的回复:]
简单:

HTML code
<asp:Label ID="lbl成绩" runat="server" Text='<%# CheckBack((int)Eval("Age"),(GridViewRow)Container) %>'></asp:Label>


C# code
protected int CheckBack(int value, GridViewRow row)
……
[/Quote]

支持这种写法!!
kkbac 2010-07-15
  • 打赏
  • 举报
回复
纯路过.
happy664618843 2010-07-15
  • 打赏
  • 举报
回复
还有一种方法:
前台绑定 <tr style=SetColor(<%#DataBinder.Eval(Container.DataItem,"score")%>)>
public string SetColor(scrore){
if(score<60){
return "color:red";
}
else{
return "color: white";
}
}
happy664618843 2010-07-15
  • 打赏
  • 举报
回复
dataview dr=ds.Tables[0].DefaultView;
if(Convert.ToInt32(dr["score"])<60){
e.Rows.cells[1].BackColor=System.Drawing.Color.red;
}
wang15072732418 2010-07-15
  • 打赏
  • 举报
回复
3,4,6楼都行
flyingyinjun 2010-07-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wangxiaofeiwuqiao 的回复:]
C# code

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
double number1 = double.Parse(e.Row.Cells[2].Text.T……
[/Quote]
正解!!!
V-Far 2010-07-15
  • 打赏
  • 举报
回复
我暂时想到的和5楼的一样.就是在rowdatabound的时候判断一下对应列的值,如果符合条件,则对该行进行处理
wuyq11 2010-07-15
  • 打赏
  • 举报
回复
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label labValue = (Label)e.Row.FindControl("lab");
if (labValue.Text == "100")
{
e.Row.BackColor = System.Drawing.Color.Red;
}
}
}
wangxiaofeiwuqiao 2010-07-15
  • 打赏
  • 举报
回复

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
double number1 = double.Parse(e.Row.Cells[2].Text.ToString());//成绩
if (number1 < 60)
{
e.Row.BackColor = System.Drawing.Color.Red;//显示红色
}

}
}

wangxiaofeiwuqiao 2010-07-15
  • 打赏
  • 举报
回复

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
double number1 = double.Parse(e.Row.Cells[2].Text.ToString());//数量1
double number2 = double.Parse(e.Row.Cells[5].Text.ToString());//数量2
if (number1 < number2 )
{
e.Row.BackColor = System.Drawing.Color.SkyBlue;//显示颜色,可自定义
}

}
}
  • 打赏
  • 举报
回复
简单:
<asp:Label ID="lbl成绩" runat="server" Text='<%# CheckBack((int)Eval("Age"),(GridViewRow)Container) %>'></asp:Label>
protected int CheckBack(int value, GridViewRow row)
{
if (value < 60)
row.BackColor = Color.Red;
return value;
vip__888 2010-07-15
  • 打赏
  • 举报
回复
if("成绩"<60)
{
//设置一下grv当前行的style.Color
}

62,073

社区成员

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

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

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

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