-2

Google-translate:

I would go a dataSet (below "dat") so that every time a chain of unwanted character is found, it is replaced.

Currently, I get stuck on the first line ... (see the MessageBox) (Sorry if my code is clean ^ ^ ')

Can you tell me what is wrong please?


Good morning,

i would like to replace some strings in a data set i am blocked (Sorry for my my code )

can you tell me what is not working thanks for support !

foreach (DataRow dtR in dat.Tables[0].Rows)
        {
            foreach (DataColumn dtC in dat.Tables[0].Columns)
            {
                MessageBox.Show(dtC.ToString());
                dtC.ToString().Replace("<br><img src=\"http://actualites.gestion.fr/good.jpg\">", "");
                dtC.ToString().Replace("<br><img src=\"http://actualites.gestion.fr/great.jpg\">", "");
                dtC.ToString().Replace("<br><img src=\"http://actualites.gestion.fr/no-good.jpg\">", "");
                dtC.ToString().Replace("<br><img src=\"http://actualites.gestion.fr/bof.jpg\">", "");
            }
        }
gasroot
  • 515
  • 3
  • 15
J.P.A
  • 95
  • 6

3 Answers3

2

The Replace method does not do an internal, in-place replacement; it returns a second string with different contents. You must capture the returned string and do something with it. Right now, you are performing the replacement, and dropping the new string (with the replacements) on the floor. For example:

string s = (string)dtR[dtC]; // read the contents of a cell
s = s.Replace("foo", "newfoo"); // perform some string replacements...
s = s.Replace("bar", "newbar");
s = s.Replace("blap", "newblap");
dtR[dtC] = s; // store it back into the cell
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • @user3811647 note in particular that I am access the *cell* via `dtR[dtC]`; your original code looked only at the column definition – Marc Gravell Jul 07 '14 at 08:54
  • not a response about the fact of being stuck on the first line – tschmit007 Jul 07 '14 at 09:06
  • @tschmit007 indeed, because that line (`dtC.ToString()`) seems irrelevant and demonstrably wrong; since the OP is enumerating rows and columns, I think we can assume they mean to access cell contents; hence my answer which *accesses cell contents*. Thus, I didn't retain the clearly broken line... – Marc Gravell Jul 07 '14 at 09:15
-1

EDITED:

dtC = new DataColumn(dtC.ToString().Replace("asupprimer", ""));

Cf. http://msdn.microsoft.com/fr-fr/library/system.data.datacolumn(v=vs.110).aspx

kiwixz
  • 1,380
  • 15
  • 23
-1

you second iteration must be:

foreach (DataColumn dtC in dtR)
tschmit007
  • 7,559
  • 2
  • 35
  • 43