Moving datarows - loses value?

  • Thread starter Thread starter Marina
  • Start date Start date
M

Marina

Hi,

I have a need to remove a DataRow that is part of one table, and physically
move it over to another table (with the same schema).

I remove the row using the Remove method, and insert it into the other table
using the InsertAt table.

In the process, the values in the row, seem to get reset to their default
value.

Why is its value being reset in the middle of the move?
 
I don't think that will work for me, as I've looked at that already. The
ImportRow method doesn't seem to have a way to define the position of the
new row. I need the row to be added at a specific index in the other table -
not at the end.
 
Hi Marina:

From your description I think the simplest way to get there is to create a
new row for the target table - since they have the same schema you won't
need to modify it or anything.

DataRow dro = TargetTable.NewRow();

Now just loop through the values and set the values of the new row
accordingly
for(int i = 0; i< TargetTable.Columns.Count; i++){

//You can use the TargetTable's Columns Count or Source Tables since the
schema is the same
dro = SourceTable.Rows[RowIndex];
}
Then just do the insertAt. I think Cor's way is better with ImportRow but
if you can't use it, this one will work in a pinch.

--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
 
Yes, I know I can do it manually. Potentially though, you are losing things
like rowstate, etc.

It is actually easier to just set the ItemArray property of the new row to
the ItemArray property of the old row, to get the actual values transferred
over.

I see no reason why doing this has to be so difficult - one would think that
a datarow should be able to exist completely independently of the
datatable - it has a detached state and everything.

But so much of its behavior is tied to the datatable, not to mention that
the default behavior is very undesirable and assumes that one wants that
functionality.

William Ryan eMVP said:
Hi Marina:

From your description I think the simplest way to get there is to create a
new row for the target table - since they have the same schema you won't
need to modify it or anything.

DataRow dro = TargetTable.NewRow();

Now just loop through the values and set the values of the new row
accordingly
for(int i = 0; i< TargetTable.Columns.Count; i++){

//You can use the TargetTable's Columns Count or Source Tables since the
schema is the same
dro = SourceTable.Rows[RowIndex];
}
Then just do the insertAt. I think Cor's way is better with ImportRow but
if you can't use it, this one will work in a pinch.

--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
Marina said:
Hi,

I have a need to remove a DataRow that is part of one table, and physically
move it over to another table (with the same schema).

I remove the row using the Remove method, and insert it into the other table
using the InsertAt table.

In the process, the values in the row, seem to get reset to their default
value.

Why is its value being reset in the middle of the move?
 
Back
Top