How to copy datarow from one datatable to another ?

Y

ypul

can anyone tell me please ..

how to copy one datarow from one datatable to another..

I want to extract some specific rows from one datatable and put into antoher
....

" datatableOne.rows.add(datatableTwo.rows(i))"

this statement does not work ..it says

"This row already belongs to another table"


what is the solution please ...
 
A

Ashish M Bhonkiya

Hi,
i am not sure if we can add a row which is a part of another table. I have
written a sample to explain how to do that.


DataTable myTableOne = new DataTable("firstTable");
myTableOne.Columns.Add("fCol1");
for(int i = 0; i<5;i++)
{
DataRow myDr = myTableOne.NewRow();
myDr[0] = i.ToString();

myTableOne.Rows.Add(myDr);
myDr = null;
}

DataTable myTableTwo = new DataTable("secondTable");
myTableTwo.Columns.Add("fCol1");
for(int i=0;i<myTableOne.Rows.Count;i++)
{
DataRow tempRow = myTableTwo.NewRow();
// you can loop here for each columns in the tableone if
you have more columns
tempRow[0] = myTableOne.Rows[0];

// tempRow = myTableOne.Rows;
// Remember will not work here as its the refrence
// of the row from tableone which is assigned to the
temprow so temprow will be pointing to
// the row of tabletwo

myTableTwo.Rows.Add(tempRow);
tempRow = null;
}

HTH
Regards
Ashish M Bhonkiya
 
Y

ypul

I did it myself in a different manner

dtfinal.Rows.Add(dt.Rows(introws).ItemArray)

thanks all


Ashish M Bhonkiya said:
Hi,
i am not sure if we can add a row which is a part of another table. I have
written a sample to explain how to do that.


DataTable myTableOne = new DataTable("firstTable");
myTableOne.Columns.Add("fCol1");
for(int i = 0; i<5;i++)
{
DataRow myDr = myTableOne.NewRow();
myDr[0] = i.ToString();

myTableOne.Rows.Add(myDr);
myDr = null;
}

DataTable myTableTwo = new DataTable("secondTable");
myTableTwo.Columns.Add("fCol1");
for(int i=0;i<myTableOne.Rows.Count;i++)
{
DataRow tempRow = myTableTwo.NewRow();
// you can loop here for each columns in the tableone if
you have more columns
tempRow[0] = myTableOne.Rows[0];

// tempRow = myTableOne.Rows;
// Remember will not work here as its the refrence
// of the row from tableone which is assigned to the
temprow so temprow will be pointing to
// the row of tabletwo

myTableTwo.Rows.Add(tempRow);
tempRow = null;
}

HTH
Regards
Ashish M Bhonkiya

ypul said:
can anyone tell me please ..

how to copy one datarow from one datatable to another..

I want to extract some specific rows from one datatable and put into antoher
...

" datatableOne.rows.add(datatableTwo.rows(i))"

this statement does not work ..it says

"This row already belongs to another table"


what is the solution please ...
 

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

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top