有条件地格式化DataGrid单元格

 

private void OnDataGridDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
 if (e.Item.ItemType == ListItemType.Item ||
     e.Item.ItemType == ListItemType.AlternatingItem)
 {
  DataRowView rv = (DataRowView)e.Item.DataItem;
  // 获取第四列的值
  Int32 nUnitsInStock = Convert.ToInt32(rv.Row.ItemArray[4]);
  if (nUnitsInStock < 20)
  {
     e.Item.Cells[4].BackColor = Color.Red;
  }
 }
}

你可能感兴趣的:(datagrid)