adding rows from one datatable to another

  • Thread starter Thread starter Sam
  • Start date Start date
S

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
 
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 ?
 
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
 
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
 
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
 
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 ??
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
Back
Top