我用下面的代码来实现动态变色功能,看起来是对的:)public void datagrid1_ItemDataBound(object sender,System.Web.UI.WebControls.DataGridItemEventArgs e)
{ if (e.Item.ItemIndex >= 0)
{
//如果TitleOfCourtesy列的值 >7 则设置该行的背景色为蓝色
if(double.Parse(e.Item.Cells[1].Text)>7)
e.Item.Cells[1].BackColor=Color.red;
}
}
但是,由于这个列中有空值,所以在执行double.Parse(e.Item.Cells[1].Text)类型转换的时候总是报错.我用e.Item.Cells[1].Text!=string.Empty来进行判断也不行,郁闷!
{ if (e.Item.ItemIndex >= 0)
{
//如果TitleOfCourtesy列的值 >7 则设置该行的背景色为蓝色
if(double.Parse(e.Item.Cells[1].Text)>7)
e.Item.Cells[1].BackColor=Color.red;
}
}
但是,由于这个列中有空值,所以在执行double.Parse(e.Item.Cells[1].Text)类型转换的时候总是报错.我用e.Item.Cells[1].Text!=string.Empty来进行判断也不行,郁闷!
if(Convert.ToInt16(DataBinder.Eval(e.Item.DataItem,"iAge"))<30)e.Item.BackColor=Color.Pink;
是类型转换错误!!
还有没有好的办法??
//table为你的datagrid的id号
function setbkcolor(table)
{
for(int i=0;i<table.rows[i].length;i++)
if(parseInt(table.rows[i].cells[index].innerHTML)>7)///index为的多少列,
table.rows[i].cells[index].bgColor="red";
}试试应该可以的
{
if (e.Item.Cells[1] != null)
{
if(double.Parse(e.Item.Cells[1].Text)>7)
e.Item.Cells[1].BackColor=Color.red;
}
}