Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

I decided to write this example because I always encounter this kind of question at http://forums.asp.net/.

This example basically changes the background color of the calendar dates based on the event dates from the database. You can tweak the codes provided in this example to suit your needs.

Here are the code blocks below:

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {

        DataTable dt = GetDates();
        DateTime eventDate;
        string eventType = string.Empty;

        if (dt.Rows.Count > 0){

            for (int i = 0; i < dt.Rows.Count; i++){
                //Where ColumnFieldForDate and ColumnFieldForEventType are the field names from your database
                eventDate = Convert.ToDateTime(dt.Rows[i]["ColumnFieldForDate"]);
                eventType = dt.Rows[i]["ColumnFieldForEventType"].ToString();
                if (e.Day.Date == eventDate)
                {
                    if (eventType == "Meeting")
                    {
                        e.Cell.BackColor = System.Drawing.Color.Blue;
                    }
                    else if (eventType == "Holiday")
                    {
                        e.Cell.BackColor = System.Drawing.Color.Red;
                    }
                    else
                    {
                        e.Cell.BackColor = System.Drawing.Color.Yellow;
                    }
                }
            }
        }
    }


    public DataTable GetDates()
    {

        DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE");
        try
        {
            connection.Open();
            SqlCommand sqlCmd = new SqlCommand("SELECT * FROM TableName", connection);
            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
            sqlDa.Fill(dt);
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Fetch Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
        finally
        {
            connection.Close();
        }

        return dt;
    }

As you can see the code above is very straight forward and self explanatory.That's it!


Technorati Tags: ,,
Posted on Sunday, May 3, 2009 5:41 PM ASP.NET , C# , Tips&Tricks | Back to top


Comments on this post: Changing the Background Color of the Calendar Control based on the Dates from the Database.

# re: Changing the Background Color of the Calendar Control based on the Dates from the Database.
Requesting Gravatar...
Very Nice & usefullArticle For Beginners.......
Left by Guru on Feb 02, 2010 8:00 PM

# re: Changing the Background Color of the Calendar Control based on the Dates from the Database.
Requesting Gravatar...
Very Nice & usefull Article For Beginners and all.......
Left by Guru on Feb 02, 2010 8:02 PM

# re: Changing the Background Color of the Calendar Control based on the Dates from the Database.
Requesting Gravatar...
very nice .....thankss
Left by irtiza on Mar 14, 2012 2:47 AM

# re: Changing the Background Color of the Calendar Control based on the Dates from the Database.
Requesting Gravatar...
Hi, im in need of help!!
im currently trying to do this exact thing, however by using visual studio which is in asp.net. May i noe how is it possible? i tried using the codes that u have provided and did some editing to match asp.net. However some parts doesnt seem to work :( like for eg. the for loop..may i noe if its possible for u to guide me or if possible show me an asp.net example.
Thanks in advance! ;)
Left by SARES on Jul 02, 2012 5:52 PM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net