how to search/replace items in specific column of a datatable

Discussion in 'ASP .Net' started by TB, Feb 26, 2006.

  1. TB

    TB Guest

    Before displaying the result of a table called "people" in a datagrid
    called "mydatagrid", I need to modify the content of a column called
    "moreinfo" in the in-memory datatable (but not the corresponding table
    of the database).

    I was thinking of doing something like this:

    Dim strSQL as string = "Select ID, firstname, lastname, moreinfo from
    people order by lastname"
    Dim myConnection As MySqlConnection = New
    MySqlConnection(connectionstring)
    Dim myDataAdapter As MySqlDataAdapter = New MySqlDataAdapter(strSQL,
    myConnection)
    Dim myDataSet As DataSet = new Dataset
    Dim row As DataRow
    Dim searchstring as string = "colour"
    Dim replacestring as string = "color"
    myDataAdapter.Fill(mydataset, "mytable")
    For Each row In myDataSet.Tables("tbl").Rows
    Replace(row("moreinfo"), searchstring, replacestring)
    Next

    mydatagrid.DataSource = myDataSet.Tables("tbl") 'datagrid previously
    defined
    mydatagrid.DataBind()

    However when the datagrid is displayed, no replace action seems to have
    taken place, as the original data (including the word "colour") of the
    "moreinfo" column of the "people" table is displayed instead.

    What am I doing wrong here?

    Thanks

    TB
     
    TB, Feb 26, 2006
    #1
    1. Advertising

  2. Hi TB,

    It should be

    For Each row In myDataSet.Tables("tbl").Rows
    row("moreinfo") = row("moreinfo").ToString.Replace(searchstring,
    replacestring)
    Next


    HTH

    Elton Wang

    "TB" wrote:

    > Before displaying the result of a table called "people" in a datagrid
    > called "mydatagrid", I need to modify the content of a column called
    > "moreinfo" in the in-memory datatable (but not the corresponding table
    > of the database).
    >
    > I was thinking of doing something like this:
    >
    > Dim strSQL as string = "Select ID, firstname, lastname, moreinfo from
    > people order by lastname"
    > Dim myConnection As MySqlConnection = New
    > MySqlConnection(connectionstring)
    > Dim myDataAdapter As MySqlDataAdapter = New MySqlDataAdapter(strSQL,
    > myConnection)
    > Dim myDataSet As DataSet = new Dataset
    > Dim row As DataRow
    > Dim searchstring as string = "colour"
    > Dim replacestring as string = "color"
    > myDataAdapter.Fill(mydataset, "mytable")
    > For Each row In myDataSet.Tables("tbl").Rows
    > Replace(row("moreinfo"), searchstring, replacestring)
    > Next
    >
    > mydatagrid.DataSource = myDataSet.Tables("tbl") 'datagrid previously
    > defined
    > mydatagrid.DataBind()
    >
    > However when the datagrid is displayed, no replace action seems to have
    > taken place, as the original data (including the word "colour") of the
    > "moreinfo" column of the "people" table is displayed instead.
    >
    > What am I doing wrong here?
    >
    > Thanks
    >
    > TB
    >
    >
     
    =?Utf-8?B?RWx0b24gVw==?=, Feb 26, 2006
    #2
    1. Advertising

  3. TB

    TB Guest

    Of course!

    Thanks a lot.

    TB
     
    TB, Feb 26, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Beza
    Replies:
    0
    Views:
    681
  2. jg
    Replies:
    1
    Views:
    731
    =?Utf-8?B?U2hhaWxlc2ggSmFubnU=?=
    Aug 17, 2004
  3. Beza

    DataTable - replace values in column

    Beza, Jul 7, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    209
  4. GoodMorningSky

    How to add New Column to existing DataTable in specific position.

    GoodMorningSky, Oct 5, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    4
    Views:
    1,264
    Alex Homer
    Oct 6, 2004
  5. William FERRERES
    Replies:
    7
    Views:
    220
    William FERRERES
    Jul 9, 2007
Loading...

Share This Page