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

六月ゝ 毕业季﹏ 提交于 2020-03-03 06:46:35

问题


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

For example:

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


回答1:


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




回答2:


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.OrdinalIgnoreCase enables case insensitive comparison, if you don't want "Equals" to be "Yes" simply use the == operator.



来源:https://stackoverflow.com/questions/17338639/c-sharp-change-value-of-all-rows-of-a-specific-colum-of-a-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!