cancel
Showing results for 
Search instead for 
Did you mean: 

Code for replacing character in column values in a collection

KarinaHansen
Level 3
Hi

Can anyone provide a code (C#), or give advise on, how to replace dot with comma (or other characters) for an entire column in a collection. I need to be able to define which column and I want to avoid looping.

Thank you!
1 BEST ANSWER

Helpful Answers

NickLeGuerrier
Level 4
Hi Karina,

Since you mentioned you want to be able to modify all rows for one specific column, this code should work. 

foreach(DataRow row in inputCollection.Rows)
{
    row[columnName] = row[columnName].ToString().Replace(".",",");
}

outputCollection = inputCollection;

View answer in original post

5 REPLIES 5

NicholasZejdlik
Level 9
There should be an action in the stock Collection Manipulation object for Remove Dots From Headers that should help.

In case you don't have it, this is the VB code:
For Each Column As DataColumn In Input_Collection.Columns
   Column.ColumnName=Microsoft.Visualbasic.Replace(Column.ColumnName,".","")
Next
Output_Collection = Input_Collection​


And the C# version of it:

foreach (DataColumn col in Input_Collection.Columns) {
	col.ColumnName = col.ColumnName.Replace(".", "");
}
Output_Collection = Input_Collection;

KarinaHansen
Level 3
Thanks for your reply.
This code seems to be replacing dots in the collection field names. What I meant was the column values - Sorry if this was unclear 🙂

NickLeGuerrier
Level 4
Hi Karina,

Since you mentioned you want to be able to modify all rows for one specific column, this code should work. 

foreach(DataRow row in inputCollection.Rows)
{
    row[columnName] = row[columnName].ToString().Replace(".",",");
}

outputCollection = inputCollection;

KarinaHansen
Level 3
Thanks Nick, 
I see that you replied in the other thread as well 😄

I get this error when I try to define columnName as input and run it:

Internal : Could not execute code stage because exception thrown by code stage: Unable to cast object of type 'System.Data.DataColumn' to type 'System.Data.DataRow'.

KarinaHansen
Level 3
It works now - There was a mistake in line 1.

Had it saying:
foreach(DataRow row in inputCollection.Columns)​


instead of:

foreach(DataRow row in inputCollection.Rows)


Thank you very much 🙂 🙂