Moving the position of a datarow

D

dotnetstevo

I am trying to manipulate the data in a DataTable
I want to move a row from one position to another
my code works so far. It goes like:

//index is the index of the row I want to move
DataRow drRowToMove = MyDataTable.Rows[index];
MyDataTable.Rows.RemoveAt(index);
MyDataTable.Rows.InsertAt(drRowToMove, index - 1);
MyDataTable.AcceptChanges();

It works, I get a reordered DataTable
but the problem is that the new inserted row (that should have been
moved) is blank, ie it contains no data
I have debugged the code and it appears that "drRowToMove" contains no
data after it has been "removedAt" on the line before

so....basically (to cut a long story short :)
I want to copy the data in the row BEFORE calling the RemoveAt method
so that I can insert a fresh row with the previous data

Any ideas?
 
Joined
Dec 11, 2008
Messages
1
Reaction score
0
The solution

dt is a reference t the DataTable
i is the number of the row to move
to is the new position

DataRow drTmp = dt.NewRow();
drTmp.ItemArray=dt.Rows.ItemArray;
dt.Rows.RemoveAt(i);
dt.Rows.InsertAt(drTmp , to);
dt.AcceptChanges();
 
Joined
Jan 19, 2009
Messages
1
Reaction score
0
wow, thank you pkasabov!! I had been looking for this for a while. I would never have gotten the idea that copying the ItemArray does the job. Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top