By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,920 Members | 1,946 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,920 IT Pros & Developers. It's quick & easy.

Cloning a datatable and replacing column contents...

P: n/a
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_cit y.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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

Nov 21 '05 #2

P: n/a
Coleen,

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

http://msdn.microsoft.com/library/de...scopytopic.asp

Cloning means copying only the schema from the table.

I hope this helps,

Cor
Nov 21 '05 #3

P: n/a
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_than_city_fy05.Rows.Count - 1 Step 1
ldt_new_stat3c_city.Rows(li_indx).Item(6) =
idt_greater_than_city_fy05.Rows(li_indx).Item(7)
ld_tot_235_for_dist =
Double.Parse(ldt_new_stat3c_city.Rows(li_indx).Ite m(6).ToString,
_Globalization.NumberStyles.Any)
If li_indx < 35 Then
ld_gt_235_for_dist += 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]" <no************@planet.nl> wrote in message
news:Ob**************@TK2MSFTNGP09.phx.gbl...
Coleen,

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

http://msdn.microsoft.com/library/de...scopytopic.asp
Cloning means copying only the schema from the table.

I hope this helps,

Cor

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.