Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"

like image 229
lugeno Avatar asked Dec 12 '25 22:12

lugeno


1 Answers

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.

like image 147
Tim Schmelter Avatar answered Dec 15 '25 10:12

Tim Schmelter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!