Solved

C# Calendar - show multiple rows on one date

Posted on 2006-11-28
8
728 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to count occurrences of each item in an array.

744 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now