Search code examples
c#replacedatasetdatarowdatacolumn

Replace content in a DataSet


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\">", "");
            }
        }

Solution

  • 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