根据条件改变行的颜色
< asp:DataGrid id = " dg1 " runat = server OnItemDataBound = " ibd " >
</ DataGrid >
public void ibd(Object o,DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView rv = (DataRowView)e.Item.DataItem;
// Get fourth column value.
// 此处是获得第一列的值,当然你可以修改为其他的
string nUnitsInStock = Convert.ToString(rv.Row.ItemArray[ 0 ]);
if (nUnitsInStock == " 1 " )
{
for ( int i = 0 ;i < dg1.Columns.Count;i ++ )
{
e.Item.Cells[i].ForeColor = Color.Red;
}
}
else
{
for ( int i = 0 ;i < dg1.Columns.Count;i ++ )
{
e.Item.Cells[i].ForeColor = Color.Blue;
}
}
}
}
< asp:DataGrid id = " dg1 " runat = server OnItemDataBound = " ibd " >
</ DataGrid >
public void ibd(Object o,DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView rv = (DataRowView)e.Item.DataItem;
// Get fourth column value.
// 此处是获得第一列的值,当然你可以修改为其他的
string nUnitsInStock = Convert.ToString(rv.Row.ItemArray[ 0 ]);
if (nUnitsInStock == " 1 " )
{
for ( int i = 0 ;i < dg1.Columns.Count;i ++ )
{
e.Item.Cells[i].ForeColor = Color.Red;
}
}
else
{
for ( int i = 0 ;i < dg1.Columns.Count;i ++ )
{
e.Item.Cells[i].ForeColor = Color.Blue;
}
}
}
}