イベント管理アプリを構築しようとしています。月間カレンダーのあるウィンドウがあります。DataGridView は次のようになります。
各セルは 1 日の月を表し、その日に 1 つ以上のイベントが予定されている場合、セルは緑色にする必要があります。次のセッターを使用します。
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();
}
}
プライベート フィールド eventsByMonth を設定するだけでなく、データグリッド ビューにデータを入力して色を付ける必要があります。上記のカレンダー ウィンドウの画像は、次のコード スニペットがコメント化された結果です。
//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;
}*/
同じコードのコメントを解除すると、次の例外がスローされました。
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
dgvEventsByMonth.Rows[i].Cells[j].Value = DaysIterator;
問題のあるスニペットの上のステートメントが決して例外をスローしないという事実を考えると、何が問題なのですか?
例外がスローされると、i==3、j==5、DaysIterator==23 になります。
セルに色を付けるために他の方法を試す必要がありますか? 私はこれに行き詰まりたくありません。私にとっては、この種の問題を解決することよりも、物事を機能させることが重要です。