?
Solved

C# Calendar - show multiple rows on one date

Posted on 2006-11-28
8
Medium Priority
?
767 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
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.
Suggested Courses

764 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