473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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.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
3 5962
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dmitry Karneev | last post by:
Hi! I have a rather interesting problem. I have a dataset that contatin data from database. I need to clone this dataset with data but all id's in tables must be NULL and all DataRowStates must...
1
by: imakowski | last post by:
Is there posibility to create clone of an object without using serialization, create instance and copy, reflection ? I have a problem with DataColumnColection which is member of DataTable...
3
by: AVL | last post by:
Hi, I've a query in cloning. How cloning is different from creating a new instance of an object.? I suppose cloning also creates a new object and copies the exisitng object's data. Where and when...
0
by: jhcorey | last post by:
I'm looking at some code where a column was added to a datatable and then given a value. It works fine. But if I try to update a previously existing column, it doesn't take. No error is...
3
by: Gene Hubert | last post by:
I'm using DataTable.ImportRow to move data from one datatable to another... Dim dt, dtTarget As DataTable Dim dr As DataRow dt = DirectCast(Me.DataSource, DataTable) dtTarget = dt.Clone...
4
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
I'm working in Visual Studio 2005 and C#. I have a DataTable with many rows. The user can delete a row. After the row is deleted, I'd like to column values for the deleted row, but any attempt to...
6
by: rclarke | last post by:
Hi, When cloning a DataTable, it appears that seed counts are also copied to the new table e.g. dtbSource has 50 rows, clone dtbSource to dtbClone and then add the first new row to dtbClone ......
2
by: ArunDhaJ | last post by:
Hi, I'm having a DataTable with a column named "Phones" having value like (901) 789 1234<BR>(901) 789 1235<BR>(901) 789 1221 I need to perform filtering based on the phone number of the form...
4
by: ArunDhaJ | last post by:
Hi, I've to filter rows from DataTable Select There is a column named "Phone" which contains values in the following format: (901) 789 1234<BR>(901) 789 1235<BR>(901) 789 1221 I need to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.