C# 如何使用C根据条件更改gridview单元格颜色#
我想根据条件更改grdiview单元格的颜色,条件是如果Passport将在一个月内过期,或者它已经过期,那么我想检查这两个条件,如果它即将过期或者已经过期,那么我想将颜色更改为红色。谢谢C# 如何使用C根据条件更改gridview单元格颜色#,c#,asp.net,.net,gridview,C#,Asp.net,.net,Gridview,我想根据条件更改grdiview单元格的颜色,条件是如果Passport将在一个月内过期,或者它已经过期,那么我想检查这两个条件,如果它即将过期或者已经过期,那么我想将颜色更改为红色。谢谢 protected void OnRowDataBound_gvPass(object sender, GridViewRowEventArgs e) { DateTime todaysDate = DateTime.Now.Date; if (e.Row.RowType ==
protected void OnRowDataBound_gvPass(object sender, GridViewRowEventArgs e)
{
DateTime todaysDate = DateTime.Now.Date;
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblPassportExpDate = (Label)e.Row.FindControl("PassportExpDate");
DateTime PassportExpDateDate = DateTime.Parse(lblPassportExpDate.Text);
if (PassportExpDateDate < DateTime.Today || PassportExpDateDate < todaysDate.AddDays(30))
{
//e.Row.BackColor = System.Drawing.Color.Red;
gvDriverStatus.Columns[3].ItemStyle.ForeColor = System.Drawing.Color.Red;
}
}
}
protectedvoid OnRowDataBound\u gvPass(对象发送方,GridViewRowEventArgs e)
{
DateTime todaysDate=DateTime.Now.Date;
如果(e.Row.RowType==DataControlRowType.DataRow)
{
Label lblPassportExpDate=(Label)e.Row.FindControl(“PassportExpDate”);
DateTime PassportExpDateDate=DateTime.Parse(lblPassportExpDate.Text);
如果(PassportExpDateDate
以下是一段对我有效的简化代码,您可以很容易地适应您的情况:
protected void Page_Load(object sender, EventArgs e)
{
refDate = new DateTime(1996, 7, 15);
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
if (DateTime.Parse(e.Row.Cells[3].Text) < refDate)
{
e.Row.Cells[3].BackColor = Color.Red;
}
}
}
检查此项,我有类似的逻辑,但仍然不适用于我
protected void Page_Load(object sender, EventArgs e)
{
minDate = new DateTime(1996, 7, 7);
maxDate = new DateTime(1996, 7, 15);
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
var curDate = DateTime.Parse(e.Row.Cells[3].Text);
if (minDate < curDate && curDate < maxDate)
{
e.Row.Cells[3].BackColor = Color.Red;
e.Row.Cells[3].ForeColor = Color.White;
}
}
}