You must Sign In to post a response.
  • Category: .NET

    Want to Replace ", " with ";" in datatable without using loop

    Hi All,

    i have a datatable with some data separated with comma
    I want to replace all the occurrence of comma with semicolon in that datatable itself with using for loop or for each loop . Hopes to get reply soon.


    Thanks
  • #764666
    Hi Pratik,
    try this:
    dtTable.Select(string.Format("convert(DataTableColumnName, 'System.String') Like '%{0}%'", ',')).ToList<DataRow>().ForEach(r => r["DataTableColumnName"] = r["DataTableColumnName"].ToString().Replace(",", ";"));
    dtTable.AcceptChanges();
    Hope it helps.
    Regards,
    Shashikant Gurav
    shashikantgurav22@gmail.com

  • #764669

    Hi,

    To update the datatable, the simplest way is finding the columns with appropriate result and then update the result with as we want.

    Refer below sample code for your reference

    DataRow[] dr = table.Select("col1 like ','").FirstOrDefault();

    if(dr != null)
    {
    dr["col1"] = dr["col1"].ToString().Replace(",",";");
    }

    dt.Acceptchanges();


    I didn't test this, please correct the code if you found any mistakes in that.


    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #764670
    thanks for the help

  • #764677
    Hi,

    If you are comfirtable using Linq then please try below code,

    http://stackoverflow.com/questions/18830113/update-multiple-rows-in-datatable-without-loop


    Regards,
    Asheej T K
    Microsoft MVP[ASP.NET/IIS]
    DotNetSpider MVM

  • #764684
    if you don't want to use loop on each row then With the help of LINQ you can do it
    see below snippet
    DataTable recTable = new DataTable();

    // do stuff to populate table

    recTable.Select(string.Format("
     = '{0}'", someName)).ToList<DataRow>().ForEach(r => r["col1"]Contains(","));
    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


Sign In to post your comments