Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace string with another in DataColumn of DataTable

Tags:

c#

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

like image 717
Shaggy Avatar asked Jan 14 '23 20:01

Shaggy


2 Answers

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
}
like image 155
Habib Avatar answered Feb 04 '23 06:02

Habib


foreach (DataRow row in dt.Rows)
    row.SetField<string>("Comment", 
       row.Field<string>("Comment").Replace("*", @"\n"));
like image 21
Tim Schmelter Avatar answered Feb 04 '23 05:02

Tim Schmelter