C#,gridview,根据字段属性改变背景:
假设GridView中包含“姓名”“地址”“状态”等字段,现在要把记录中状态为“0”的行背景设为红色,其它的为蓝色,则可以在GridView1的DataBound()事件中书写代码如下:
代码一:
void DetailsView1_DataBound(object sender, EventArgs e)
{
//e.Row.Cells[0]绑定的字段是state
if (e.Row.Cells[0].Text == "0")
e.Row.BackColor = System.Drawing.Color.Red;
else
e.Row.BackColor = System.Drawing.Color.Blue;
}
代码二:
void DetailsView1_DataBound(object sender, EventArgs e)
{
//e.Row.Cells[0]绑定的字段是state
for (int i = 0; i < DetailsView1.Rows.Count; i++)
{
if (DetailsView1.Rows[i].Cells[0].Text == "0")
DetailsView1.Rows[i].BackColor = System.Drawing.Color.Red;
else
DetailsView1.Rows[i].BackColor = System.Drawing.Color.Blue;
}
}