Solved

C# Calendar - show multiple rows on one date

Posted on 2006-11-28
8
733 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaScript communication inside a IFRAME 9 57
Popup window in I.E. 8 49
comparing strings that contain line breaks 18 53
Hovering effect 9 47
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

16 Experts available now in Live!

Get 1:1 Help Now