如何将特定行的列背景设置为不同的颜色

How to set the background of a column for specific row to different color

我正在从代码隐藏填充 GridView。每个行条目显示 Due Date(格式:mm-dd-yyyy)。

我如何修改上面的内容以便添加以下内容:<asp:BoundField HeaderStyle-Width="1%" HeaderText="" ItemStyle-CssClass="taskTableColumn" />(另一列)在 asp:TemplateFieldasp:HyperLinkField 之间,它将查看今天是否已过截止日期来自 Due Date 列。如果已超过截止日期,请将背景设置为该行的该列的 #C000000

你可以试试这个,肯定要换个条件

protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
//Check if it is not header or footer row
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        if(e.Row.RowIndex == 0)
             e.Row.Cells[0].BackColor = Color.Red;
        if(e.Row.RowIndex == 1)
 e.Row.Cells[0].BackColor = Color.Green;
    }
}