根据条件更改Gridview单元格颜色的问题

时间:2015-07-23 22:57:12

标签: c# asp.net gridview

我正在尝试更改gridview的单元格颜色,如果DOT<今天的日期。我的问题是它改为DOT列的所有行的红色,即使DOT列>今天的日期。
enter image description here

这是我的代码

 protected void OnRowDataBound_gvTest(object sender, GridViewRowEventArgs e)
        {

          if (e.Row.RowType == DataControlRowType.DataRow)
          {

            Label lblDOT = (Label)e.Row.FindControl("DOT");
            DateTime DOTDate = DateTime.Parse(lblDOT.Text);
            if (DOTDate < DateTime.Now)
            {
              //e.Row.BackColor = System.Drawing.Color.Red;
              gvDriverStatus.Columns[3].ItemStyle.ForeColor = System.Drawing.Color.Red;
            }


          }
        }

1 个答案:

答案 0 :(得分:3)

此:

gvDriverStatus.Columns[3].ItemStyle.ForeColor = ...

将更改整列的颜色。要仅更改单元格颜色,请使用:

e.Row.Cells[3].ForeColor = ...
相关问题