C# - 更改数据表特定列的所有行的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17338639/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:12:59  来源:igfitidea点击:

C# - Change value of all rows of a specific colum of a DataTable

c#datatabledatarow

提问by lugeno

I have a DataTable. What I want to do is change the value of all rows of the Colum "X" of the DataTable.

我有一个数据表。我想要做的是更改数据表的列“X”的所有行的值。

For example:

例如:

if row value is "TRUE" then change it into "Yes" else change it into "No"

如果行值为“TRUE”,则将其更改为“是”,否则将其更改为“否”

采纳答案by kml

maybe you could try this

也许你可以试试这个

int columnNumber = 5; //Put your column X number here
for(int i = 0; i < yourDataTable.Rows.Count; i++)
{
    if (yourDataTable.Rows[i][columnNumber].ToString() == "TRUE")
    { yourDataTable.Rows[i][columnNumber] = "Yes"; }
    else
    { yourDataTable.Rows[i][columnNumber] = "No"; }
}

Hope this helps...

希望这可以帮助...

回答by Tim Schmelter

A simple loop:

一个简单的循环:

foreach(DataRow row in table.Rows)
{
    string oldX = row.Field<String>("X");
    string newX = "TRUE".Equals(oldX, StringComparison.OrdinalIgnoreCase) ? "Yes" : "No";
    row.SetField("X", newX);
}  

StringComparison.OrdinalIgnoreCaseenables case insensitive comparison, if you don't want "Equals" to be "Yes" simply use the ==operator.

StringComparison.OrdinalIgnoreCase启用不区分大小写的比较,如果您不希望“等于”为“是”,只需使用==运算符即可。