Cloning a datatable and replacing column contents...

  • Thread starter Thread starter Coleen
  • Start date Start date
C

Coleen

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.RemoveAt(6)
ldt_new_stat3c_city.Columns.Add(ldt_new_stat3c_city.Rows(li_indx).Item(7).To
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
 
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
ldr_new_stat3c_city.Column[position] = value


I hope it can be helpfull to you

Luck,
Oscar Espinosa
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top