Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a hotel booking calendar that selects the DateFrom and DateTo

I need to iterate through from the DateFrom to DateTo and display all these dates in the calendar, the code I have so far only selects the `DateFrom` and `DateTo` and displays them in the calendar as a label:

C#
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
   {
       foreach (DataRow dr in ds.Tables[0].Rows)
       {
           DateTime df = (DateTime)dr.Field<DateTime?>("DateFrom");
           DateTime dt = (DateTime)dr.Field<DateTime?>("DateTo");


           if (e.Day.Date == dt.Date)
           {
               Label lbl = new Label();
               lbl.BackColor = System.Drawing.Color.Gray;
               lbl.Text = "Booked From";
               e.Cell.Controls.Add(lbl);
            }

           if (e.Day.Date == df.Date)
           {
               Label lbl = new Label();
               lbl.BackColor = System.Drawing.Color.Gray;
               lbl.Text = "Booked To";
               e.Cell.Controls.Add(lbl);
           }
       }
Posted

1 solution

The solution I am using in case somebody who needs this comes across this is:
C#
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
  foreach (DataRow dr in ds.Tables[0].Rows)
  {
    DateTime df = (DateTime)dr.Field<DateTime?>("DateFrom");
    DateTime dt = (DateTime)dr.Field<DateTime?>("DateTo");

    if (e.Day.Date == dt.Date)
    {
        Label lbl = new Label();
        lbl.BackColor = System.Drawing.Color.Gray;
        lbl.Text = "Booked From";
        e.Cell.Controls.Add(lbl);
     }

    if (e.Day.Date == df.Date)
    {
        Label lbl = new Label();
        lbl.BackColor = System.Drawing.Color.Gray;
        lbl.Text = "Booked To";
        e.Cell.Controls.Add(lbl);
    }

    //Added Code
    if(e.Day.Date > df.Date && e.Day.Date < dt.Date)
    {
        Label lbl = new Label();
        lbl.BackColor = System.Drawing.Color.Gray;
        lbl.Text = "Day inbetween";
        e.Cell.Controls.Add(lbl);
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900