469,280 Members | 1,855 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,280 developers. It's quick & easy.

adding rows from one datatable to another

Sam
Hi,
The following code :
For Each r As DataRow In dtFlow.Rows
Dim arow As DataRow = dtAllFlows.NewRow
arow = r
dtAllFlows.Rows.Add(arow)
Next

leads to the error 'This row already belongs to another table '
at the line dtAllFlows.Rows.Add(arow).
I guess I should just do dtAllFlows = dtFlow.copy, but I can't as the
code above is contained in a For loop and at each iteration, I add new
rows (dtFlow changes at each iteration).

How can I solve this problem ?

Regards

Nov 21 '05 #1
21 21157
Sam,

I assume that this is what you want to do

importrow
http://msdn.microsoft.com/library/de...rtrowtopic.asp

I hope this helps,

Cor
Nov 21 '05 #2
Sam
Thanks Cor ;-)

Nov 21 '05 #3
Sam
Actually, I might have spoken too quickly.

For Each r As DataRow In dtFlow.Rows
Dim arow As DataRow = dtAllFlows.NewRow
arow = r
dtAllFlows.ImportRow(arow)
Next

It looked right but in fact when I try to access the data:

dtallflows.Rows(0).Item(0)

gives :

Run-time exception thrown : System.IndexOutOfRangeException - Cannot
find column 0.

What's going on ?

Nov 21 '05 #4
Sam

When you try to copy a complete table, than you can use table.copy

However this is a sample from me how to sort a datatable, you see than the
use of the import in it.

\\\
Dim dv As New DataView(dt)
dv.Sort = "bla"
Dim dtnew As DataTable = dt.Clone
For Each dvr As DataRowView In dv
dtnew.ImportRow(dvr.Row)
Next
///

I hope this helps,

Cor
Nov 21 '05 #5
Sam
As I said in the first thread, I don't want to do a copy as I add the
rows in a for loop (concatenate if you prefer). therefore I have to use
ImportRow, which leads to the error mentionned in my second thread...
Can you help further ?

Thx

Nov 21 '05 #6
Sam,.

The only thing you can do is copy it, use an extra dataview, remove it from
the first.

However you cannot have a reference in one datarow to two tables.

There is this property in the datarow that should explain that to you if my
text did not.

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

It is a litte bit difficult to set one address to two tables.

I hope this helps,

Cor
Nov 21 '05 #7
Sam
Ok, I do that now :

Dim dvTmp As new DataView
dvTmp.Table = dtFlow
dtFlow = Nothing
For Each r As DataRowView In dvTmp
dtAllFlows.Rows.Add(DirectCast(r, DataRow))
Next

But the Add line doesn't work ( invalid cast ). How can I add a
datarowview to a datatable ??

Thx

Nov 21 '05 #8
Sam
This should be correct I guess...

dtAllFlows.Rows.Add(r.Row)

But I keep getting the error ; the datarow already exists in another
table :(
How come ??

Nov 21 '05 #9
Sam,

Did you read my message?

How you want to do this

myproperty a = adress of datatable wherein it is connected
datarow.a = table1
datarow.a = both table1 and table2

Cor
Nov 21 '05 #10
Sam
Cor,
I read your message but :

1. the link u gave me does not work
2. I don't understand this sentence :

The only thing you can do is copy it, use an extra dataview, remove it
from
the first.

Could you explain that to me if you don't mind please ?

Thx

Nov 21 '05 #11
Sam,

Again.

You cannot add a reference from a datarow to 2 datatables. (That is what you
do with Add or insertAt)

Therefore is the only thing that you can do.
Create a copy from the original and add that to your new table
Remove the reference from the first and than add it to the new table
Create an extra dataview instead of that extra table.

The copy you have already, the second seems for me out of sense and the
thirth is probably what you need.

dim dv as new dataview(mydatatable)

In fact you have now a complete view on your table. If you want only some
rows than you need a rowfilter

dv.rowfilter = "City = 'Amsterdam'"

I hope this helps

Cor
Nov 21 '05 #12
Sam
I'm very very confused. Sorry :(
Are you actually saying I should do that :

1. Create a copy from the original and add that to your new table
dim dtAllFlows as datatable
dtAllFlows = dtFlow.copy

2.Remove the reference from the first and than add it to the new table
???? what does this mean ?

3. Create an extra dataview instead of that extra table.
What for ? :
Dim dvTmp As new DataView
dvTmp.Table = dtFlow

Could you give a very simple example please, as I really don't
understand the above ?
Sorry for bothering :(
Thx

Nov 21 '05 #13
Sam,

I am only saying that you cannot have the same datarow in 2 datatables.

However you only tell that you want this and I only try to explain you why
this is impossible.

Cor
Nov 21 '05 #14
Sam
I understood this. But you've also tried to explain a work around and I
dont understand it !

You said :
Therefore is the only thing that you can do.
Create a copy from the original and add that to your new table
Remove the reference from the first and than add it to the new table
Create an extra dataview instead of that extra table.
Could you give a little example of this (just a few lines of code) as I
can't get it.

Thx a lot

Nov 21 '05 #15
Sam,

In addition, I give you 3 solutions that can solve your problem. However I
don't know your problem except that an original row cannot be added.

Cor
Nov 21 '05 #16
Sam,

Quickly writen in this messages so watch typos or others.

The first
\\\
dim dtnew as datatable = dtOld.Clone
dtnew.rows.Add(dt.newrow)
dtnew.importrow(dtOld.Rows(0))
///
The second
\\\
dim dtnew as datatable = dtOld.Clone
dtnew.rows.Add(dt.newrow)
dim dr as datarow = dtnew.rows(0)
dt.rows.remove(0)
dtnew.rows.Add(dr)
///
The third
\\\
dim dv = new dataview(dtOld)
'in a dv the show on a datarow is to get (as I showed you before) as a
datarowview
///

I hope this give some ideas

Cor
Nov 21 '05 #17
Sam
Cor,

All of these three methods have an issue.
As I said before, my code is in a loop. I think it's a good idea I give
the algorithm I'm trying to write so one can help me :

for ........
'reset dtFlows
'fill dtFlows with values based on some requests

'add rows of dtFlows to dtAllFlows (I know I can't do that directely)
next

Therefore, doing one of your two methods wouldn't work as I don't want
to do
dim dtnew as datatable = dtOld.Clone in each loop (that would delete
all the existing rows added before)
Your third method :
dim dv = new dataview(dtOld)
OK but I want to put my dv in a datatable then, how ? Actually to be
more precise, I want to add this new datatable (dataview) to a dataset.

Hope I'm clear enough.

Sam

Nov 21 '05 #18
Sam,

You mean that you want to do this

Dim ds as new dataset
ds.tables.add(tableOld.copy)

Here the same isue, you cannot add a table to two dataset, because every
datatable has a property dataset which can only hold one dataset.

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

I know it is hard, I cannot change it.

Cor
Nov 21 '05 #19
Sam
NOOOOOOOOOOOO !!!!!!! :-) I think we'll never understand each other on
this one, LOL
I don't want to add a table to two datasets. I just want to :

loop
reset datatable
fill datatable with data

fill newdatatable with rows of datatable above (here is the catch and
I must find another way to do it, as you explained with a lot of effort
that it's impossible ;-))

next

Then add newdatatable to a dataset (this is no problem at all)

Hope this is clear ! Again as I'm in a loop I cannot do
dim dtnew as datatable = dtOld.Clone
as you mentionned before otherwise I would loose the data stored in
dtnew that have been added in previous loops.

Sam

Nov 21 '05 #20
Sam,
NOOOOOOOOOOOO !!!!!!! :-) I think we'll never understand each other on
this one, LOL
I don't want to add a table to two datasets. I just want to :


I know however what I showed you is the same as

\\\
dim dt as datatable = dtold.clone
for each row as datarow in dtold
dt.importrow(row)
next
///

However mayby you want to do it selective than you need this one.

:-)

Cor
Nov 21 '05 #21
hi guys..

Pls be careful when using the below options if your appln architecture is
using rowstates
1) importrow does not modify the rowstate as added.

2) methods like remove() are not advisable if what you want to do is just
delete it is like calling delete and acceptchanges.

3) i too did not understand this one..

Tnx

"Cor Ligthert" wrote:
Sam,

Quickly writen in this messages so watch typos or others.

The first
\\\
dim dtnew as datatable = dtOld.Clone
dtnew.rows.Add(dt.newrow)
dtnew.importrow(dtOld.Rows(0))
///
The second
\\\
dim dtnew as datatable = dtOld.Clone
dtnew.rows.Add(dt.newrow)
dim dr as datarow = dtnew.rows(0)
dt.rows.remove(0)
dtnew.rows.Add(dr)
///
The third
\\\
dim dv = new dataview(dtOld)
'in a dv the show on a datarow is to get (as I showed you before) as a
datarowview
///

I hope this give some ideas

Cor

Nov 21 '05 #22

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Clayton Hamilton | last post: by
1 post views Thread by J. Babe | last post: by
3 posts views Thread by Jim Heavey | last post: by
reply views Thread by Luis Esteban Valencia | last post: by
4 posts views Thread by VMI | last post: by
reply views Thread by ganesh22 | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.