How to copy datarow from one datatable to another ?

Discussion in 'ASP .Net' started by ypul, Apr 29, 2004.

  1. ypul

    ypul Guest

    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 ...
     
    ypul, Apr 29, 2004
    #1
    1. Advertising

  2. ypul

    CT Guest

    Try this:
    http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q308909

    --
    Carsten Thomsen
    Enterprise Development with VS .NET, UML, and MSF
    http://www.apress.com/book/bookDisplay.html?bID=105
    "ypul" <> wrote in message
    news:...
    > 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 ...
    >
    >
     
    CT, Apr 29, 2004
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:...
    > 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 ...
    >
    >
     
    Ashish M Bhonkiya, Apr 29, 2004
    #3
  4. ypul

    ypul Guest

    I did it myself in a different manner

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

    thanks all


    "Ashish M Bhonkiya" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    > > 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 ...
    > >
    > >

    >
    >
     
    ypul, Apr 29, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J. Babe
    Replies:
    1
    Views:
    8,357
    Kevin Spencer
    Aug 15, 2003
  2. Rob G
    Replies:
    0
    Views:
    2,022
    Rob G
    Jun 7, 2004
  3. Ersin Gençtürk
    Replies:
    1
    Views:
    3,494
    Ersin Gençtürk
    Oct 6, 2004
  4. Bennett Haselton
    Replies:
    0
    Views:
    687
    Bennett Haselton
    Nov 11, 2004
  5. Shawn
    Replies:
    3
    Views:
    30,394
    Robbe Morris [C# MVP]
    Mar 9, 2005
Loading...

Share This Page