Color GridView到期日期

时间:2015-04-04 02:08:11

标签: c# asp.net gridview

我在C#编程中是“新手”,我正在尝试为几天后过期的日期着色,或者它已经过期了。这是我的代码

SqlConnection cs2 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\\Users\Nikola\documents\visual studio 2010\Projects\Gym Software\Gym Software\Data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
sda = new SqlDataAdapter("Select DatumIstice FROM Korisnici", cs2);
foreach (DataGridViewRow row in dataGridView1.Rows)
{
    var now = DateTime.Now;
    var expirationDate = DateTime.Parse(row.Cells[0].Value.ToString());
    var sevenDayBefore = expirationDate.AddDays(-7);
    if (now > sevenDayBefore && now < expirationDate)
    {
        row.DefaultCellStyle.BackColor = Color.Yellow;
    }
    else if (now > expirationDate)
    {
        row.DefaultCellStyle.BackColor = Color.Red;
    }
}

最后一行应该是彩色的,但不是:example

2 个答案:

答案 0 :(得分:0)

这个怎么样:

row.BackColor = Color.Red;   

答案 1 :(得分:0)

您可以尝试这一点 - 假设到期日期为Datumlsteka,您应该从4th列,not 0th列中选择值并将其用作expirationDate

    SqlConnection cs2 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\\Users\Nikola\documents\visual studio 2010\Projects\Gym Software\Gym Software\Data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

sda = new SqlDataAdapter("Select DatumIstice FROM Korisnici", cs2);
foreach (DataGridViewRow row in dataGridView1.Rows)
{
    var now = DateTime.Now;
    var expirationDate = DateTime.Parse(row.Cells[4].Value.ToString());
    var sevenDayBefore = expirationDate.AddDays(-7);

    if (now > sevenDayBefore && now < expirationDate)
    {
        row.DefaultCellStyle.BackColor = Color.Yellow;
    }
    else if (now > expirationDate)
    {
        row.DefaultCellStyle.BackColor = Color.Red;
    }
}