Domanda

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"

È stato utile?

Soluzione 2

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

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top