0

I am trying to build an event management app. I have a window with a month calendar. This is how the DataGridView looks like:

enter image description here

Each cell represents a day month and if one ore more events are scheduled for that day, the cell has to be colored green. I use the following setter:

public IList<Event> _EventsByMonth
{//this setter populates and colors the table
    set
    {
        eventsByMonth = value;


        dgvEventsByMonth.Rows.Add("", "", "", "", "", "", "");
        dgvEventsByMonth.Rows.Add("", "", "", "", "", "", "");
        dgvEventsByMonth.Rows.Add("", "", "", "", "", "", "");
        dgvEventsByMonth.Rows.Add("", "", "", "", "", "", "");
        dgvEventsByMonth.Rows.Add("", "", "", "", "", "", "");
        //dgvEventsByMonth.Rows.Add("", "", "", "", "", "", "");


        foreach (DataGridViewRow row in dgvEventsByMonth.Rows)
        {
            row.Height = (dgvEventsByMonth.ClientRectangle.Height - dgvEventsByMonth.ColumnHeadersHeight) / dgvEventsByMonth.Rows.Count;
        }


        if (eventsByMonth.Count != 0)
        {
            DateTime aux = new DateTime();


            mapMonthDaysToEvents();

            {
                //in case the first event in the list is recurrent, the following method is necessary
                aux = getNextOccurrenceDateOrStartDate(eventsByMonth[0]);
                //I need to know which is the first weekday of the month 
                aux = aux.AddDays(1 - eventsByMonth[0].startDatetime.Day);
            }

            int firstWeekDayOfTheMonth = (int)aux.DayOfWeek;
            int numberOfDays = DateTime.DaysInMonth(aux.Year, aux.Month);

            for (int i = 0, DaysIterator = 1; DaysIterator <= numberOfDays; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                    //go over the uncovered days at the beginning of the calendar
                    while (i == 0 && j + 1 < firstWeekDayOfTheMonth)
                    {
                        j += 1;
                    }

                    dgvEventsByMonth.Rows[i].Cells[j].Value = DaysIterator;

                    //TODO check for events in that day. if any, color the cell
                    /*if (mappingOfMonthDaysToEventsByMonth[DaysIterator] != null)
                    {
                        dgvEventsByDay.Rows[i].Cells[j].Style.BackColor = Color.Green;
                    }*/

                    DaysIterator++;
                }
            } 
        }
        dgvEventsByMonth.Refresh();
    }
}

Besides setting a private field eventsByMonth, this one should also populate and color the datagridview. The above calendar window image resulted when the following code snippet was commented:

                    //TODO check for events in that day. if any, color the cell
                    /*if (mappingOfMonthDaysToEventsByMonth[DaysIterator] != null)
                    {
                        dgvEventsByDay.Rows[i].Cells[j].Style.BackColor = Color.Green;
                    }*/

The following exception was thrown when the same code was uncommented:

Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.dll
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>EventManagementApplication.vshost.exe</AppDomain><Exception><ExceptionType>System.ArgumentOutOfRangeException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index</Message><StackTrace>   at System.Collections.ArrayList.get_Item(Int32 index)
   at System.Windows.Forms.DataGridViewCellCollection.get_Item(Int32 index)
   at EventManagementApplication.CalendarView.set__EventsByMonth(IList`1 value) in D:\probleme in C C++ Java PHP Python\probleme in C C++ Java PHP Python\C#\EventManagementApplication\EventManagementApplication\View\CalendarView.cs:line 225

What is the problem, given the fact that the statement dgvEventsByMonth.Rows[i].Cells[j].Value = DaysIterator; above the problematic snippet never throws an exception ?

When the exception is thrown i==3, j==5, DaysIterator==23.

enter image description here

Should I try other method to color a cell? I do not want to get stuck on this one. For me it is more important to make things work than solving this kind of problem.

Andrei
  • 217
  • 2
  • 12
  • Check the value of i and j. – Rahul Jan 25 '16 at 18:37
  • 1
    Where is it actually blowing up? Is it on the `mappingOfMonthDaysToEventsByMonth[DaysIterator] != null` part, or the `dgvEventsByDay.Rows[i].Cells[j].Style.BackColor = Color.Green;` part? – gmiley Jan 25 '16 at 18:39
  • It is on dgvEventsByDay.Rows[i].Cells[j].Style.BackColor = Color.Green; – Andrei Jan 25 '16 at 18:40
  • the value of `i` also goes up to the number of days in the month, which is way more than the number of rows. – Max Jan 25 '16 at 18:41
  • When the exception is thrown i==3, j==5, DaysIterator==23 Sorry for not mentioning that from the beginning. – Andrei Jan 25 '16 at 18:43
  • I am guessing that ` i==3, j==5` is valid for `dgvEventsByMonth`, but not for `dgvEventsByDay`? That or else `mappingOfMonthDaysToEventsByMonth[23]` is invalid and your compiler optimizations are moving the debuggers inference of the current source code line. Try turning of optimizations and see where the error comes from then. – RBarryYoung Jan 25 '16 at 19:48

2 Answers2

2

You iterate over dgvEventsByMonth, but set style for cell in dgvEventsByDay. As far as I understand, you need to replace it

0

By the trace and stack dump this has to be coming from an index reference parameter into the DataGridView. There is nothing in the code visible to us that insures that dgvEventsByDay has the same number of rows and columns as dgvEventsByMonth, so I would surmise that it's max row,column indexes are less than (3,5).

Presumably the (mappingOfMonthDaysToEventsByMonth[DaysIterator] != null) test is not always coming back true, and this failed because it was the first time it tried to set the backcolor outside of it's true bounds.

RBarryYoung
  • 55,398
  • 14
  • 96
  • 137