如何在while循环中移动到下一行 [英] How to move to next row in while loop

查看:131
本文介绍了如何在while循环中移动到下一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在表格中的while循环中移动到下一行



我尝试过:



How to move to next row in while loop in table

What I have tried:

while (rows["Token_no"].ToString() == cToken && DateTime.Parse(rows["Date"].ToString()) == dDDate)
{
  rows.BeginEdit();
  rows["In_out"] = "I";
  rows.EndEdit();

  // want move to next row
}

推荐答案

这样的事情:

Something like this:
foreach (DataRow row in rows)
{
	if (row["Token_no"].ToString() == cToken && DateTime.Parse(row["Date"].ToString()) == dDDate)
	{
		row["In_out"] = "I";
	}
}

这是一篇关于使用 DataTable DataRow [ ^ ]



如果你有 DataTable 表,那么你可以使用:

Here is an interesting article about working with DataTable and DataRow:[^]

If you have a DataTable table, then you can use:

While (i < table.Rows.Count)
{
   DataRow row = table.Rows[i];
   i++;
}


另一种方式(作为 RickZeeland [ ^ ])更新数据是使用Linq。



看看例子:

Another way (as an alternative to solution 1 by RickZeeland[^]) to update data is to use Linq.

Take a look at example:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID", typeof(int)));
dt.Columns.Add(new DataColumn("In_out", typeof(string)));
for(int i=0; i<50; i++)
{
	dt.Rows.Add(new Object[]{i, string.Format("row{0}",i)});
}


var query = dt.AsEnumerable()
    .Where(r=>r.Field<int>("ID") % 2 ==0)
    .ToList();

foreach (DataRow row in query)
{
    row.SetField("In_out", "I");
}





结果:



Result:

ID In_out
0  I 
1  row1 
2  I 
3  row3 
4  I 
5  row5 
6  I





根据需要更改代码。



Change the code to your needs.


这篇关于如何在while循环中移动到下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆