2

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

例如:

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

4

2 回答 2

6

一个简单的循环:

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.OrdinalIgnoreCase启用不区分大小写的比较,如果您不希望“等于”为“是”,只需使用==运算符。

于 2013-06-27T08:51:06.197 回答
5

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...

于 2013-06-27T09:10:54.983 回答