有两个数据集ds和ds1。如果两个数据集的值匹配,则希望更改datalist的背景色。我们正在比较两个数据集,但没有比较值。
for (Int32 i = 0; i < ds.Tables[0].Rows.Count-1; i++)
{
for (Int32 x = 0; x < ds1.Tables[0].Rows.Count-1; x++)
{
if (ds.Tables[0].Rows[i][0].ToString() == ds1.Tables[0].Rows[x][0].ToString())
{
DataList1.ItemStyle.BackColor = System.Drawing.Color.Red;
}
else
{
DataList1.ItemStyle.BackColor = System.Drawing.Color.Green;
}
}
} 发布于 2018-11-19 04:15:54
您应该在找到匹配后中断循环,否则循环将继续并在每次与值匹配或不匹配时更改颜色,直到两个循环结束。下面是如何打破嵌套循环的答案:Breaking out of a nested loop
发布于 2018-11-19 04:26:06
正如@Greg所提到的,在找到匹配后,您确实需要中断循环。
此外,如果需要扫描datasets中的所有行,请更改循环定义:
for (Int32 i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (Int32 x = 0; x < ds1.Tables[0].Rows.Count; x++) 在您的代码中,您将跳过这两个datasets的最后一行,因为循环将在其计数器等于Count - 2时结束。
https://stackoverflow.com/questions/53364875
复制相似问题