Solved

C# Calendar - show multiple rows on one date

Posted on 2006-11-28
8
747 Views
Last Modified: 2011-10-03
I have a Calendar control that is bound to a Sql database.  Some days on my calendar have multiple events.  I'm able to show the dates correctly and the first event of each day but I can't figure out how to show multiple events that show up on certain dates.  Here's my code:

      private void Page_Load(object sender, System.EventArgs e)
            {
                  strConn = "data source = sqldatabase; initial catalog = database; integrated security = SSPI ";
                  conn = new SqlConnection(strConn);
                  da = new SqlDataAdapter("storedprocedure", conn);
                  da.SelectCommand.CommandType = CommandType.StoredProcedure;      
                  da.Fill(ds, "dt");
            }

            protected void CalendarDRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
            {
                  if(! e.Day.IsOtherMonth)
                  {
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                              if((dr["date"].ToString() != DBNull.Value.ToString()))                        
                              {
                                    String d = (String)dr["event"];
                              
                                    DateTime dtEvent=(DateTime)dr["Date"];
                                    if(dtEvent.Equals(e.Day.Date))
                                          {                              
                                                e.Cell.Text = dtEvent.Day.ToString() + d;
                                          }
                              }
                        
                        }
                  
                  }
                  else
                  {
                        e.Cell.Text = "";
            
                  }
            }

Any ideas are appreciated!  
0
Comment
Question by:JoanHerrick
  • 3
  • 2
8 Comments
 
LVL 6

Expert Comment

by:LeeHenry
ID: 18032372
It appears as if you are using a for loop to get all the events for that date, but then your are assigning that to e.Cell.Text, and replacing it everytime with the previous event. Rather than replacing e.Cell.Text everytime, try appending the event to a list.

something like below

  if(! e.Day.IsOtherMonth)
               {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                         if((dr["date"].ToString() != DBNull.Value.ToString()))                    
                         {
                              String d = (String)dr["event"];
                         
                              DateTime dtEvent=(DateTime)dr["Date"];
                              if(dtEvent.Equals(e.Day.Date))
                                   {                        
                                        YourListOfEvents.Append(dtEvent.Day.ToString() + d);
                                   }
                         }
                   
                    }
               
               }

Also, you could modify your web control to append the event.
0
 

Author Comment

by:JoanHerrick
ID: 18039309
I guess I'm not following you.  I don't think I'm looping to get all the events - maybe that's what I'm missing?  
0
 
LVL 6

Expert Comment

by:LeeHenry
ID: 18039743
that is what your missing.. my mistake.. can you add an event count to your dataset? if so you can do something like below.. notice i added count = (Int32)dr["eventCount"];

protected void CalendarDRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
          {
               if(! e.Day.IsOtherMonth)
               {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                         if((dr["date"].ToString() != DBNull.Value.ToString()))                    
                         {
                              String d = (String)dr["event"];
                              count = (Int32)dr["eventCount"];

                              DateTime dtEvent=(DateTime)dr["Date"];
                              if(dtEvent.Equals(e.Day.Date))
                                   {      
                                        for (n = 0; n < count; n++)
                                             {
                                                   e.Cell.Text = dtEvent.Day.ToString() + d;
                                             }
                                   }
                         }
                   
                    }
               
               }

0
 

Author Comment

by:JoanHerrick
ID: 18040859
I'm with you until I get to the for (n=0; n < count; n++) - what is n?
0
 
LVL 6

Accepted Solution

by:
LeeHenry earned 500 total points
ID: 18041133
n just a local counter variable. count is the number of events, and n starts off at the first event and gets incremented n++, untill it reaches the number of events (count).

You can name it something like this..

//declare your local variables;

int numberOfEvents;
int count;

numberOfEvents = (Int32)dr["eventCount"];

//now your for loop

for(counter = 0; counter < numberOfEvents; counter ++)
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question