Solved

C# Calendar - show multiple rows on one date

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adding items to a C# list incrementally 5 82
Website Question - New Site 3 48
Passing client data from form to server 5 55
Saving Web Page as PDF 9 80
Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

732 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