Click here to Skip to main content
16,012,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi developers,

I want to replace some wildcards in dataset. When I used regex.replace method on all columns and rows that works but If I use it on particular index number, the method does not work.
I can also tell you that I want to apply a regex.replace method on a index number not on a header name.

Please read this code:
C#
foreach (DataRow datarow in dv.Table.Rows)
{
  foreach (DataColumn datacol in dv.Table.Columns)
  {
    datarow[datacol] = System.Text.RegularExpressions.Regex.Replace(datarow[datacol].ToString(), comma.ToString(), slash.ToString());
    datarow[datacol] = System.Text.RegularExpressions.Regex.Replace(datarow[datacol].ToString(), colon.ToString(), Blank.ToString());
  }
}

Please help me.
Posted
Updated 20-Dec-10 20:19pm
v2
Comments
JF2015 21-Dec-10 2:19am    
Edited to fix formatting errors.

1 solution

That is really quite messy, and inefficient.
1) You don't need to use Regex - string.Replace is simpler.
2) Don't keep converting datatypes - it wastes time, and memory. (I assume "colon", "slash" etc. are char types)
3) Don't keep temporary values in array elements.
C#
string strColon = colon.ToString();
string strBlank = Blank.ToString();
foreach (DataRow datarow in dv.Table.Rows)
   {
   foreach (DataColumn datacol in dv.Table.Columns)
      {
      string s = datarow[datacol].ToString();
      s = s.Replace(comma, slash);
      s = s.Replace(strColon, strBlank);
      datarow[datacol] = s;
      }
   }
You don't need to store the temporary value in a named variable as I have, but it makes it easier to read.

This code (and yours for that matter) should work fine if you use the DataColumn as an index (as you currently do) or an integer index based on the number of rows / columns in the table. If it doesn't, you will have to give us a sample of the non-working code before we could comment. Edit your question to include the non-working version.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900