如何与日期一起循环?

时间:2017-03-22 23:25:16

标签: c# asp.net webforms

我需要使用具有以下逻辑的日期选择器为一个月的日子着色:从数据库中的日期开始,我希望在接下来的8天内用橙色,然后在接下来的2天用绿色等等下个月。

我有以下代码,但我需要一个循环:

private void calFecha_DayRender(object source, DayRenderEventArgs e)  
{
    if (ddloperadores.SelectedValue != "Todos")
    {                
        DataTable asistencia = OperadoresForaneosAsistencia((int)WAPS.Globals.ConvertTo(txtNumOperador.Text, 0)).Tables[0];                  

        if (asistencia.Rows.Count > 0)
        {
            DataRow iRow = asistencia.Rows[0];
            string Tipo = iRow["TipoJornada"].ToString();
            Tipo = asistencia.Rows[0]["TipoJornada"].ToString();

            if (Tipo == "1") //Tipos de Jornadas
            {
                DateTime FechaJornada = Convert.ToDateTime(iRow["Jornada_Ini"]);                      
                DateTime fechatemp = DateTime.Today;                        
                DateTime primero = new DateTime(fechatemp.Year, fechatemp.Month, 1);
                DateTime ultimo = new DateTime(fechatemp.Year, fechatemp.Month + 1, 1).AddDays(-1);                      

                int counter = 1;
                for (DateTime n = FechaJornada; n <= ultimo; n=n.AddDays(1))
                {
                    int rem = counter % 10;

                    if (rem >= 1 && rem <= 8)
                    {
                        e.Cell.BackColor = System.Drawing.Color.DarkGreen;
                    }
                    else
                    {
                        e.Cell.BackColor = System.Drawing.Color.DarkOrange;
                    }

                    counter++;
                }                        
            }
        }
    }
}

calendar image

1 个答案:

答案 0 :(得分:1)

在您的评论参考中,请在循环中执行以下操作:

int counter = 1;
for(n = FechaJornada; n <= ultimo; n.AddDay(1))
{
   int rem = counter % 10;
   if (rem >= 1 && rem <= 8)
       e.Cell.BackColor = System.Drawing.Color.DarkGreen;
   else
       e.Cell.BackColor = System.Drawing.Color.DarkOrange;

   counter++;
}

编辑:搬入白天后

   // compute FechaJornada then...
   if(FechaJornada < e.Day.Date)
   {
      int rem = (e.Day.Date - FechaJornada).Days % 10;
      if (rem >= 1 && rem <= 8)
          e.Cell.BackColor = System.Drawing.Color.DarkGreen;
      else
          e.Cell.BackColor = System.Drawing.Color.DarkOrange;
   }
   else
      e.Cell.BackColor = System.Drawing.Color.White;