Exception Occurred

时间:2017-06-09 12:48:49

标签: c#

I'm constantly getting the same error.

enter image description here

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll 
Additional information: InvalidArgument=Value of '0' is not valid for 'index'.

The Code is:

private void radGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
    {
        if (e.CellElement.ColumnInfo.HeaderText == "logo")
        {
            if (e.CellElement.RowInfo.Cells[2].Value.ToString() == "Error")
            {
                e.CellElement.Image = (Image)imageList1.Images[0];
                e.CellElement.ToolTipText = "Error";
            }
            else if (e.CellElement.RowInfo.Cells[2].Value.ToString() == "Warning")
            {
                e.CellElement.Image = imageList1.Images[1];
                e.CellElement.ToolTipText = "Warning";
            }
            else if (e.CellElement.RowInfo.Cells[2].Value.ToString() == "Message")
            {
                e.CellElement.Image = imageList1.Images[2];
                e.CellElement.ToolTipText = "Message";
            }
        }
    }
    }

//-----------------------------------------------------------------------

2 个答案:

答案 0 :(得分:0)

MSDN说这个

  

当参数的值超出被调用方法定义的允许值范围时抛出的异常。

关于ArgumentOutOfRangeException。它继续说

  

您正在按索引号检索集合的成员,索引号无效。

     

这是ArgumentOutOfRangeException的最常见原因   例外。通常,索引号对于三个中的一个无效   原因:

     
      
  •     

    该集合没有成员,您的代码假设它没有。

      
  •   
  •     

    您正在尝试检索索引为负数的项目。这个     通常是因为你搜索了一个集合的索引     特定元素并错误地认为搜索是     成功的。

      
  •   
  •     

    您正在尝试检索索引等于的元素     集合的Count属性的值。

      
  •   

我怀疑这是第一个案例,即imageList1是空的。所以你的imageList1.Images[0]会抛出异常,因为那里什么都没有。

要确定是否是这种情况,请尝试imageList1.Images.Count。鉴于您正在查看代码中的3个元素,Count必须为> = 3。

答案 1 :(得分:-2)

从外观上看,数组中没有任何内容。检查索引是否实际初始化,然后从那里继续。

相关问题