Daniel_Hdez
Daniel_Hdez

Reputation: 46

How to color a calendar on ASP.NET/C#?

I'm on ASP.NET, programming with C#, Visual Studio 2010.

I've a Calendar, and the days must be filled with the data entered by employees, the data will go to my database.

Each day has 8 hours, and I want:

If the day is empty, with 0 hours filled, colored red on If the day is half-filled, with 1-7 hours filled, colored yellow on If the day is full, with 8 hours filled, colored green on

I hope I explained correctly, there is my code:

    <asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" 
                        BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest" 
                        Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="200px" 
                        ShowGridLines="True" Width="1500px" 
                        OnSelectionChanged="Calendar1_SelectionChanged"
                        OnDayRender = "Calendar1_DayRender">
                    <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
                    <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
                    <OtherMonthDayStyle ForeColor="#CC9966" />
                    <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
                    <SelectorStyle BackColor="#FFCC66" />
                    <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
                </asp:Calendar>

and C#:

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        Style ImputedDaysStyle = new Style();
        ImputedDaysStyle.BackColor = System.Drawing.Color.Green;
        ImputedDaysStyle.BorderColor = System.Drawing.Color.Purple;
        ImputedDaysStyle.BorderWidth = 1;
        Style HalfImputedDaysStyle = new Style();
        ImputedDaysStyle.BackColor = System.Drawing.Color.GreenYellow;
        ImputedDaysStyle.BorderColor = System.Drawing.Color.Purple;
        ImputedDaysStyle.BorderWidth = 1;

        if (e.Day.Date.Month.ToString() != SelectedMonth) return;
        if (HoursXDayInMonth[e.Day.Date.Day-1] >= 8) e.Cell.ApplyStyle(ImputedDaysStyle);
        else if (HoursXDayInMonth[e.Day.Date.Day - 1] >= 1) e.Cell.ApplyStyle(HalfImputedDaysStyle);

    }

If anybody knows how to do it, please tell me!

Thanks!

Upvotes: 0

Views: 1876

Answers (1)

dLcreations
dLcreations

Reputation: 377

If you have a need to highlight specific columns in calender

Then You can take a look at this link: http://www.codeproject.com/Articles/7929/Highlighting-Important-Dates-in-Calendar

Upvotes: 1

Related Questions