当GridView希望依据单元格的值来动态改变行列的背景色时,可以将判断写在RowDataBound 事件中,在此事件中可以取得该单元格的值来做判断并改变显示的颜色;
例如有一个 value列,要依此列的值大小来決定列的颜色,当value大于0时背景色为蓝色,反之为红色;后台代码如下(*.aspx.cs):
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView oRow;
if (e.Row.RowType == DataControlRowType.DataRow)
{
oRow = (DataRowView)e.Row.DataItem;
//依 value 值来决定单元格的颜色,>0,蓝色色;<0,红色;
if (Convert.ToDouble(oRow["value"].ToString()) < 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;
}
}
}
{
DataRowView oRow;
if (e.Row.RowType == DataControlRowType.DataRow)
{
oRow = (DataRowView)e.Row.DataItem;
//依 value 值来决定单元格的颜色,>0,蓝色色;<0,红色;
if (Convert.ToDouble(oRow["value"].ToString()) < 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;
}
}
}