Shaggy
Shaggy

Reputation: 5790

Replace string with another in DataColumn of DataTable

I have DataTable which retrieves multiple columns and rows. One of its column ("Comments") Contains data *. I want replace that character with \n.

dtOutput = Generix.getTickets(DateTime.Parse("1-1-1900"), DateTime.Now,"",iTicket, "", "", "", "","",iDispatched,uCode);
string sOutput = "";
foreach (DataRow drOutput in dtOutput.Rows)
{
   sOutput += ((sOutput == "") ? "" : "~");
   foreach (DataColumn dcOutput in dtOutput.Columns)
   {                                    
      sOutput += ((sOutput == "") ? "" : "|") + Convert.ToString(drOutput[dcOutput]);
   }
}

I am able to merge all columns in one String. But how to replace character with another in store it in Same string as of "sOutput".

Upvotes: 4

Views: 14624

Answers (3)

Alex Filipovici
Alex Filipovici

Reputation: 32541

foreach (DataColumn dcOutput in dtOutput.Columns)
{                    
    sOutput += ((sOutput == "") ? "" : "|") + Convert.ToString((dcOutput.ColumnName=="Comments") ? drOutput[dcOutput].ToString().Replace("*","\n") : drOutput[dcOutput]);
}

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460048

foreach (DataRow row in dt.Rows)
    row.SetField<string>("Comment", 
       row.Field<string>("Comment").Replace("*", @"\n"));

Upvotes: 3

Habib
Habib

Reputation: 223207

In foreach loop you can modify the row by accessing against the column index ("Comments") and use string.Replace to replace "*" with "\n"

foreach (DataRow drOutput in dtOutput.Rows)
{
     drOutput["Comments"] = drOutPut["Comments"].ToString().Replace('*','\n');
     //your remaining code
}

Upvotes: 5

Related Questions