[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 901
  • Last Modified:

Calendar HowTo: Add Multiple Events to a SINGLE day (c#)

How do I add MULTIPLE events to a SINGLE day using the calendar in C#. Code below (from my study guide) uses a Hashtable. But the hashtable requires a unique key. This seems to be a problem if multiple events happen on the same day.

I need a clue.

thanks
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class news_calendar_Default : System.Web.UI.Page
{
 
    // create hash tables to hold values
    private Hashtable m_SchoolClosed = new Hashtable();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        m_SchoolClosed = new System.Collections.Hashtable();
 
        // populate a list of School Closings
        m_SchoolClosed.Add("11/27/2008", "Closed");
        m_SchoolClosed.Add("11/28/2008", "Closed");
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        Label1.Text = Calendar1.SelectedDate.Date.ToShortDateString();
    }
 
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (m_SchoolClosed.ContainsKey(e.Day.Date.ToShortDateString()))
        {
            if(!e.Day.IsOtherMonth && !e.Day.IsWeekend)
            {
 
                e.Cell.BackColor = System.Drawing.Color.AliceBlue;
                e.Cell.Controls.Add(new LiteralControl("<br />" + m_SchoolClosed["11/27/2008"].ToString()));
            }
        }
 
    }
}

Open in new window

0
the_sleeper
Asked:
the_sleeper
  • 2
  • 2
1 Solution
 
jjardineCommented:
Maybe instead of using a hash table you created your own class to store your details and store that in a collection.  Then for each day in the calendar, you grab all the classes with that date and add each one of those to the calendar.

Or, you could have a collection of some sort, with each row being a key (the date) and the value being an array of the events for that day.  So as you access the key from the collection (the date) it then gives you an array of the events.  Then loop through that list of events adding each one to the calendar.
0
 
the_sleeperAuthor Commented:
Thanks jjardine,

That's exactly what I am looking for: Strategy. Briefly of the two choices: which would you use and why?

thanks again.
0
 
jjardineCommented:
Well i am not sure which I would use.   The second option would make it easier to find the array of events because you can just use the date key in the hash to get them.  I say easier because the implementation is there for you.   In the first way you might have to do a bit more work to sort the collection of objects by Date or just create a new function to find each object with the matching date.  

As far as performance, I am not sure which would be faster.  Possibly the hash if you are just storing an array of strings as the events in the value of the hash row.  The custom class might create more overhead than is needed.   A lot might depend on how many events you would be loading into your collection at any given time.  
0
 
the_sleeperAuthor Commented:
Thanks Again, jjardine

sleeper
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now