无法更改日历中任何一天的颜色,除了最后一个

时间:2016-08-15 19:05:31

标签: c# asp.net

我试图将日历的日期颜色设置为绿色,当它们与SQL表中的日期匹配时。

我的问题是,下面的代码只会更改处理的最后一天的颜色。我需要以绿色显示与数据库表中的日期匹配的所有日期。

protected void Calendar1_DayRender(Object sender, DayRenderEventArgs e)
{
    int i = 1;
    DateTime[] daynumber = new DateTime[224];
    using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DataWarehouseConnectionString"].ToString()))
    {
        using (SqlCommand cnn = new SqlCommand("Select dates from RCADates "))
        {
            cnn.Connection = connection;
            connection.Open();
            SqlDataReader reader = cnn.ExecuteReader();
            //if (reader.HasRows)
            //{
            while (reader.Read())
            {
                daynumber[i] = reader.GetDateTime(1);
                if (e.Day.DayNumberText == Convert.ToString(daynumber[i].Day))
                {
                    e.Cell.ForeColor = System.Drawing.Color.Green;
                }
                else //if (!e.Day.IsOtherMonth ) //  color to red for current month
                {
                    //e.Cell.BackColor = System.Drawing.Color.Red;
                    e.Cell.ForeColor = System.Drawing.Color.Red;
                }
                i += 1;
            }
            //}
            reader.Close();
        }
    }
}

0 个答案:

没有答案