How to make all sundays red on month calendar in c#?

11,912

Solution 1

If you want to color individual days in the calendar, then you should take a look at Calendar.SelectedDates and Calendar.SelectedDayStyle properties

Then you could do something like this

myCal.SelectedDates.Add({DateTime object});
myCal.SelectedDayStyle.BackColor = System.Drawing.Color.Red;

This is useful e.g. when displaying dates with certain events.

If you want to color specific dates in the month, then you should take a look at Calendar.DayRender Event. This event should help you to render every sunday red by doing something like this (Using the DayOfWeek enumeration)

void DayRender(Object source, DayRenderEventArgs e) 
{
  // Change the background color of the days in the month to Red.
  if (e.Day.Date.DayOfWeek == DayOfWeek.Sunday)
     e.Cell.BackColor=System.Drawing.Color.Red;
}

Solution 2

I've done this in ASP.Net by using a per date event that can be used. Just check the day-of-the-week for the current day and if it checks out update the style (or whatever) of the current day.

If your looking at WinForms, I'd assume it would have something similar. I don't remember what the bits are named but that shouldn't be hard to find.

Share:
11,912
Grant
Author by

Grant

Updated on June 04, 2022

Comments

  • Grant
    Grant about 2 years

    I was wondering if anyone knows how to make all Sundays on the .NET month calendar to have the background colour red?