问题
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