我已经查看了其他大约20个帖子以寻求帮助,但这些帖子似乎都不是我想要的。我在下面列出了所有内容。我试过让它等于我,但我仍然得到索引错误。我正在处理的数据库并不总是有信息。即使数据库中没有任何内容,我怎么能让它返回?
List<DataClassLibrary.LineData> ScaleGroup1and3 = list.FindAll(ld => ld.ScaleGroup == DataClassLibrary.ScaleGroup.GROUP_1 ||
ld.ScaleGroup == DataClassLibrary.ScaleGroup.BOTH);
List<DataClassLibrary.LineData> ScaleGroup2and3 = list.FindAll(ld => ld.ScaleGroup == DataClassLibrary.ScaleGroup.GROUP_2 ||
ld.ScaleGroup == DataClassLibrary.ScaleGroup.BOTH);
Label[] Group1and3ItemLabels = new Label[8] { L1, L7, L13, L19, L25, L31, L37, L43 };
Label[] Group1and3BrandLabels = new Label[8] { L2, L8, L14, L20, L26, L32, L38, L44 };
Label[] Group1and3RacksLabels = new Label[8] { L3, L9, L15, L21, L27, L33, L39, L45 };
Label[] Group1and3CasesLabels = new Label[8] { L50, L51, L53, L55, L57, L59, L61, L63};
Label[] Group2and3ItemLabels = new Label[] { L4, L10, L16, L22, L28, L34, L40, L46 };
Label[] Group2and3BrandLabels = new Label[] { L5, L11, L17, L23, L29, L35, L41, L47 };
Label[] Group2and3rackslabels = new Label[] { L6, L12, L18, L24, L30, L36, L42, L48 };
Label[] Group2and3CasesLabels = new Label[] { L49, L52, L54, L56, L58, L60, L62, L64 };
for (int i = 0; i <= ScaleGroup1and3.Count; i++)
{
Group1and3ItemLabels[i].Text = list[i].ItemCode;
Group1and3BrandLabels[i].Text = list[i].BrandCode;
Group1and3CasesLabels[i].Text = list[i].CasesRemaining.ToString();
}
for (int i = 0; i <= ScaleGroup2and3.Count; i++)
{
Group2and3ItemLabels[i].Text = list[i].ItemCode;
Group2and3BrandLabels[i].Text = list[i].BrandCode;
Group2and3CasesLabels[i].Text = list[i].CasesRemaining.ToString();
}
答案 0 :(得分:1)
将<=
更改为<
,以便在DB未返回任何内容时不执行循环:
for (int i = 0; i < ScaleGroup1and3.Count; i++)
...
for (int i = 0; i < ScaleGroup2and3.Count; i++)
实际上你需要更改它们,因为列表中的索引是从0开始的,这意味着元素
ScaleGroup1and3[ScaleGroup1and3.Count]
不存在。
答案 1 :(得分:1)
确保ScaleGroup1and3
和ScaleGroup2and3
不超过8个元素,for
声明应如下所示:
for (int i = 0; i < ScaleGroup1and3.Count; i++)
for (int i = 0; i < ScaleGroup2and3.Count; i++)