编写了这样一段读取和移动datatable中的数据行的代码,
但是在“myDs.Tables["myDa_Copy"].Rows.RemoveAt(d);”后增加了一条赋值语句“d = d - 1;”后就陷入死循环,无法输出结果。
请问各位高手这个问题该如何解决 for (d = 0; d < myDs.Tables["myDa_Copy"].Rows.Count; d++)
{
if (myDs.Tables["myDa_Copy"].Rows[d]["cur_nodetype"].ToString() == "Router")
{
myDs.Tables["myDa_Copy"].Rows.Add(myDs.Tables["myDa_Copy"].Rows[d].ItemArray);
//myDs.Tables["myDa_Copy"].Rows[d].Delete();
myDs.Tables["myDa_Copy"].Rows.RemoveAt(d);
router_count++;
}
}
但是在“myDs.Tables["myDa_Copy"].Rows.RemoveAt(d);”后增加了一条赋值语句“d = d - 1;”后就陷入死循环,无法输出结果。
请问各位高手这个问题该如何解决 for (d = 0; d < myDs.Tables["myDa_Copy"].Rows.Count; d++)
{
if (myDs.Tables["myDa_Copy"].Rows[d]["cur_nodetype"].ToString() == "Router")
{
myDs.Tables["myDa_Copy"].Rows.Add(myDs.Tables["myDa_Copy"].Rows[d].ItemArray);
//myDs.Tables["myDa_Copy"].Rows[d].Delete();
myDs.Tables["myDa_Copy"].Rows.RemoveAt(d);
router_count++;
}
}
int i=myDs.Tables["myDa_Copy"].Rows.Count;// 这句话提出来就好了
for (d = 0; d < i; d++)
{
if (myDs.Tables["myDa_Copy"].Rows[d]["cur_nodetype"].ToString() == "Router")
{
myDs.Tables["myDa_Copy"].Rows.Add(myDs.Tables["myDa_Copy"].Rows[d].ItemArray);
//myDs.Tables["myDa_Copy"].Rows[d].Delete();
myDs.Tables["myDa_Copy"].Rows.RemoveAt(d);
router_count++;
}
}
如果表有4条记录,也就是说myDs.Tables["myDa_Copy"].Rows.Count=4
第一次执行,myDs.Tables["myDa_Copy"].Rows.RemoveAt(0)
执行第一次之后,d=1,myDs.Tables["myDa_Copy"].Rows.Count=3,myDs.Tables["myDa_Copy"].Rows.RemoveAt(1)
执行第二次之后,d=2,myDs.Tables["myDa_Copy"].Rows.Count=2,myDs.Tables["myDa_Copy"].Rows.RemoveAt(2)
执行第三次之后,d=3,myDs.Tables["myDa_Copy"].Rows.Count=1,myDs.Tables["myDa_Copy"].Rows.RemoveAt(3)
...
{
if (myDs.Tables["myDa_Copy"].Rows[d]["cur_nodetype"].ToString() == "Router")
{
myDs.Tables["myDa_Copy"].Rows.Add(myDs.Tables["myDa_Copy"].Rows[d].ItemArray);
//myDs.Tables["myDa_Copy"].Rows[d].Delete();
myDs.Tables["myDa_Copy"].Rows.RemoveAt(d);
if (d < myDs.Tables["myDa_Copy"].Rows.Count - 1)//在这里加判断,防止死循环
d--;
router_count++;
}
}
for (d = myDs.Tables["myDa_Copy"].Rows.Count-1; d >=0; d--)