用DATAGRID怎么实现预警的效果?
例如:
绑定字段=1 (背景色或者。。。)就显示绿色
绑定字段=-1 就显示红色
绑定字段=0 就显示黄色
------解决方案--------------------protected void dataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.Cells[0].Text == "1 ")
{
for (int i = 0; i < e.Item.Cells.Count; i++)
{
e.Item.Cells[i].Attributes.Add( "bgcolor ", "green ");
}
}
//后面的照着上面的写就行了。
}
------解决方案--------------------protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView row = (DataRowView)e.Item.DataItem;
string sValue = row[ "字段 "].ToString();
if (sValue == "1 ")
{
e.Item.BackColor = System.Drawing.Color.Green;
}
else if (sValue == "-1 ")
{
e.Item.BackColor = System.Drawing.Color.Red ;
}
else if (sValue == "0 ")
{
e.Item.BackColor = System.Drawing.Color.Yellow;
}
}
}
------解决方案--------------------Private Sub DGPlans_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DGPlans.ItemDataBound
Dim span As TimeSpan
Try
span = Convert.ToDateTime(e.Item.Cells(6).Text).Subtract(Convert.ToDateTime(Now.ToShortDateString))
If span.Days < 5 Then '结束时间还有五天
e.Item.BackColor = Color.GreenYellow
End If
If span.Days = 1 Then '结束时间还有一天
e.Item.BackColor = Color.Yellow
End If
If span.Days = 0 Then '已经到了结束时间
e.Item.BackColor = Color.Red
End If
If span.Days < 0 Then '已经超出了结束时间
e.Item.BackColor = Color.Black
e.Item.ForeColor = Color.White
End If
Catch
End Try
End Sub