C# 如何更新位于 foreachloop 中的数据表中的列中的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9750814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to update a value in a column in a datatable which is in a foreachloop?
提问by Seesharp
I want to update all columns one by one in a datatable using a foreach loop. The code below is what I have so far. But it does not seem to work. Your help will be much appreciated.
我想使用 foreach 循环一一更新数据表中的所有列。下面的代码是我到目前为止所拥有的。但它似乎不起作用。您的帮助将不胜感激。
foreach (DataRow row in myTable.Rows)
{
Double i;
Double j = Convert.ToDouble(row["x"]);
int y = 1;
int aan = (int)row["year"];
if(y == aan)
{
i = j + 2;
}
row["x"]=i;
row.EndEdit();
myTable.AcceptChanges();
}
采纳答案by Krishna
The code works fine for me, except for a few tweaks. The code is given below:
除了一些调整外,代码对我来说很好用。代码如下:
foreach (DataRow row in myTable.Rows)
{
Double i = 0;
Double j = Convert.ToDouble(row["x"]);
int y = 1;
int aan = Convert.ToInt32(row["year"]);
if(y == aan)
{
i = j + 2;
}
row["x"]=i;
row.EndEdit();
myTable.AcceptChanges();
}
Are you facing any specific issues?
您是否面临任何具体问题?

