索引超出范围.必须为非负数,且小于集合错误的大小 [英] Index was out of range.Must be non_negitive and less then size of the collection error
问题描述
public class Data
{
public string Print { get; set; }
}
public class AData
{
public int A0 { get; set; }
public int A1 { get; set; }
public int A2 { get; set; }
public int A3 { get; set; }
public int A4 { get; set; }
}
List<AData> grid2Data = new List<AData>();
载入事件
Load Event
List<Data> data = new List<Data>();
grid2Data.Add(new AData() { A0 = 0, A1 = 0, A2 = 0, A3 = 0, A4 = 0 });
GridView1.datasource=grid2data;
gridview1.databind();
if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer)
{
Label lblTotal = e.Item.FindControl("lblSize") as Label;//e.Row.FindControl("lblPrint") as Label;
string[] PrintData = lblTotal.Text.Split(',');
foreach (string d in PrintData)
{
if (d == "A0")
{
TotalA0 = TotalA0 + 1;
grid2Data[0].A0 = grid2Data[0].A0+1;
}
else if (d == "A1")
{
TotalA1 = TotalA1 + 1;
grid2Data[0].A1 = grid2Data[0].A1 + 1;
}
else if (d == "A2")
{
TotalA2 = TotalA2 + 1;
grid2Data[0].A2 = grid2Data[0].A2 + 1;
}
else if (d == "A3")
{
TotalA3 = TotalA3 + 1;
grid2Data[0].A3 = grid2Data[0].A3 + 1;
}
else
{
TotalA4 = TotalA4 + 1;
grid2Data[0].A4 = grid2Data[0].A4 + 1;
}
}
grid2Data [0] .A0 = grid2Data [0] .A0 + 1;
此行显示错误
grid2Data[0].A0 = grid2Data[0].A0+1;
this line it shows the error
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
推荐答案
基本上,这就是说虽然grid2Data是有效的集合-即,您已经执行了
行
Basically, it is saying that while grid2Data is a valid collection - i.e. you have executed the line
List<adata> grid2Data = new List<adata>(); </adata></adata>
您尚未添加任何对象.因为没有第一个元素,所以索引0被插入.
我可以看到这行
YOu have not added any objects to it. The index of 0 is invlaid because there is no first element.
I can see the line
grid2Data.Add(new AData() { A0 = 0, A1 = 0, A2 = 0, A3 = 0, A4 = 0 });
这显然在您的Load事件中,但是生成错误的代码在哪里?您将其放在单独的代码块中,因此很有可能位于构造函数或类似代码中.在我提到的两行上加上一个断点,再加上错误行,看看先发生了什么,然后找出需要移动的内容.目前,我无法为您提供帮助-我看不到足够的代码! :laugh:
which is apparently in your Load event, but where is the code which is generating the error? You have it in a separate code block, so there is a good chance it is in the constructor or similar. Put a breakpoint on the two lines I mentioned, plus the error line, and see which happens first - then work out what needs to be moved. I can''t help you on that at this point - I can''t see enough of your code! :laugh:
这篇关于索引超出范围.必须为非负数,且小于集合错误的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!