【问题标题】:System.ArgumentOutOfRangeException when trying to change the color of a cell in a dataGridView尝试更改 dataGridView 中单元格的颜色时出现 System.ArgumentOutOfRangeException
【发布时间】:2016-01-25 18:31:06
【问题描述】:

我正在尝试构建一个事件管理应用程序。我有一个带有月历的窗口。这就是 DataGridView 的样子:

每个单元格代表一个月的一天,如果当天安排了一个或多个事件,则单元格必须涂成绿色。我使用以下设置器:

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,这个还应该填充和着色 datagridview。 上面的日历窗口图像是在注释以下代码 sn-p 时产生的:

                    //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

鉴于有问题的 sn-p 上方的语句 dgvEventsByMonth.Rows[i].Cells[j].Value = DaysIterator; 从未引发异常,这是什么问题?

当抛出异常时 i==3, j==5, DaysIterator==23。

我应该尝试其他方法为单元格着色吗?我不想被困在这一点上。对我来说,让事情顺利进行比解决这类问题更重要。

【问题讨论】:

  • 检查i和j的值
  • 它到底在哪里爆炸?是在mappingOfMonthDaysToEventsByMonth[DaysIterator] != null 部分还是dgvEventsByDay.Rows[i].Cells[j].Style.BackColor = Color.Green; 部分?
  • 它在 dgvEventsByDay.Rows[i].Cells[j].Style.BackColor = Color.Green;
  • i 的值也会上升到当月的天数,远远超过行数。
  • 抛出异常时 i==3, j==5, DaysIterator==23 抱歉一开始就没有提到。

标签: c# winforms exception datagridview datagridviewcellstyle


【解决方案1】:

您迭代 dgvEventsByMonth,但在 dgvEventsByDay 中为单元格设置样式。据我了解,您需要更换它

【讨论】:

    【解决方案2】:

    通过跟踪和堆栈转储,这必须来自索引引用参数到 DataGridView。我们看到的代码中没有任何内容可以确保dgvEventsByDay 具有与dgvEventsByMonth 相同的行数和列数,所以我推测它是最大行数,列索引小于 (3,5)。

    据推测,(mappingOfMonthDaysToEventsByMonth[DaysIterator] != null) 测试并不总是返回 true,而这失败了,因为它是第一次尝试将背景色设置在其真实范围之外。

    【讨论】:

      猜你喜欢
      • 2013-04-12
      • 1970-01-01
      • 2016-05-27
      • 2013-10-23
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多