Cloning a datatable and replacing column contents...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Coleen

    Cloning a datatable and replacing column contents...

    Hi all :-)

    I have a datatable that I need to clone completely except for 1 column - I
    need to replace the contents of this 1 column with the contents from another
    datatable that has the exact same number of rows. I've tried using Removeat
    to remove the column, but when I try to add the column back in, it does not
    work. All I want to do is overwrite the data that is there with the new
    data. Any suggestions? Here is a snippet of the code that I have, but it
    does not work!

    Dim ldt_new_stat3c_ city As New DataTable
    Dim ldr_new_stat3c_ city As DataRow
    ldt_new_stat3c_ city = idt_stat3c_city
    ldt_new_stat3c_ city.Columns.Re moveAt(6)
    ldt_new_stat3c_ city.Columns.Ad d(ldt_new_stat3 c_city.Rows(li_ indx).Item(7).T o
    String)

    This just adds a column at the end and does not populate it! I've also
    tried looping through the index and populating the column that way, which
    does populate the column (I think - hard to tell for sure) but it removes
    the column, and does NOT replace it with a new one, then I have the wrong
    data in the next 2 columns and am completely missing the last column! Any
    help would be MUCH appreciated, I have been working on this program for 3
    days and really need to finish it by the end of today! TIA!

    Coleen


  • Oscar_Uio

    #2
    Re: Cloning a datatable and replacing column contents...

    Hi Coleen,
    I 'm not good with vB.net, I will try to explain you.

    The best way to replace the values from a colum is use a loop and
    replace a value in each row.

    If you have the same data type in boot database you must first clone
    the table after that you must use datarow to read each row in that
    table and replace the value like it.

    I'm not sure if in VB exist some sentence as forEach of C#.
    This is the way using C#
    foreach( dataRow row in ldt_new_stat3c_ city.Rows)
    {
    row.Column[position]= value;
    }

    In VB you can use a loop FOR if doesn't exist a equivalent of the
    Foreach in C#.

    Must be like it.
    Dim ldr_new_stat3c_ city As DataRow
    for i=0 to ldt_new_stat3c_ city.Rows.Count
    ldr_new_stat3c_ city = ldt_new_stat3c_ city.Rows[i]
    ldr_new_stat3c_ city.Column[position] = value


    I hope it can be helpfull to you

    Luck,
    Oscar Espinosa

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Cloning a datatable and replacing column contents...

      Coleen,

      Why not just copy the datatable with datatable.copy and than replace the
      values of the last column using a loop?



      Cloning means copying only the schema from the table.

      I hope this helps,

      Cor


      Comment

      • Coleen

        #4
        Re: Cloning a datatable and replacing column contents...

        Hi Cor & Oscar - Thanks for responding :-)

        I ended up doing a loop using the index I had for the new datatable (column
        I needed to use values from) and putting those values into the new datatable
        (cloned with column(6) replaced) and it worked...

        For li_indx = 0 To idt_greater_tha n_city_fy05.Row s.Count - 1 Step 1
        ldt_new_stat3c_ city.Rows(li_in dx).Item(6) =
        idt_greater_tha n_city_fy05.Row s(li_indx).Item (7)
        ld_tot_235_for_ dist =
        Double.Parse(ld t_new_stat3c_ci ty.Rows(li_indx ).Item(6).ToStr ing,
        _Globalization. NumberStyles.An y)
        If li_indx < 35 Then
        ld_gt_235_for_d ist += ld_tot_235_for_ dist
        End If
        Next

        The if Statement gets my grand total for the new column - I actually got one
        of the guys I work with to take a look at what I was doing, and having
        someone look at it from "outside the box" so to speak, really shed light on
        the problem. Thanks so much for responding, I appreciate it, and Cor, I'm
        looking at the link you sent me right now...I want to learn more about
        coning and copying datatables for future use - I have a feeling I'm going to
        need it!

        Thanks again :-)

        Coleen

        "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
        news:ObWHoMbuFH A.2540@TK2MSFTN GP09.phx.gbl...[color=blue]
        > Coleen,
        >
        > Why not just copy the datatable with datatable.copy and than replace the
        > values of the last column using a loop?
        >
        >[/color]
        http://msdn.microsoft.com/library/de...scopytopic.asp[color=blue]
        >
        > Cloning means copying only the schema from the table.
        >
        > I hope this helps,
        >
        > Cor
        >
        >[/color]


        Comment

        Working...