Copy a DataRow from one DataTable to another

Discussion in 'ASP .Net' started by Shawn, Mar 9, 2005.

  1. Shawn

    Shawn Guest

    Hi. I have 2 DataTables, dataTable1 and dataTable2. dataTable1 contains a
    DataRow that I need to insert at index 5 in dataTable2. Here is my code:

    For Each dataRow1 In dataTable1.Rows
    If dataRow1.Item("id") = "3353" Then
    dataRow2 = dataRow1
    End If
    Next


    dataTable2.Rows.InsertAt(dataRow2, 5)

    The line above gives me this error: System.ArgumentException: This row
    already belongs to another table.
    What is the right way to do this?

    Thanks,
    Shawn
     
    Shawn, Mar 9, 2005
    #1
    1. Advertising

  2. Shawn wrote:
    > Hi. I have 2 DataTables, dataTable1 and dataTable2. dataTable1 contains a
    > DataRow that I need to insert at index 5 in dataTable2. Here is my code:
    >
    > For Each dataRow1 In dataTable1.Rows
    > If dataRow1.Item("id") = "3353" Then
    > dataRow2 = dataRow1
    > End If
    > Next
    >
    >
    > dataTable2.Rows.InsertAt(dataRow2, 5)
    >
    > The line above gives me this error: System.ArgumentException: This row
    > already belongs to another table.
    > What is the right way to do this?
    >
    > Thanks,
    > Shawn
    >
    >


    Try dataTable2.ImportRow method instead...

    --
    Craig Deelsnyder
    Microsoft MVP - ASP/ASP.NET
     
    Craig Deelsnyder, Mar 9, 2005
    #2
    1. Advertising

  3. Shawn

    Shawn Guest

    Yes, but what do I have to do to get the new DataRow into index 5 in the
    DataTable?

    Shawn


    "Craig Deelsnyder" <cdeelsny@NO_SPAM_4_MEyahoo.com> wrote in message
    news:#...
    > Shawn wrote:
    > > Hi. I have 2 DataTables, dataTable1 and dataTable2. dataTable1 contains

    a
    > > DataRow that I need to insert at index 5 in dataTable2. Here is my

    code:
    > >
    > > For Each dataRow1 In dataTable1.Rows
    > > If dataRow1.Item("id") = "3353" Then
    > > dataRow2 = dataRow1
    > > End If
    > > Next
    > >
    > >
    > > dataTable2.Rows.InsertAt(dataRow2, 5)
    > >
    > > The line above gives me this error: System.ArgumentException: This row
    > > already belongs to another table.
    > > What is the right way to do this?
    > >
    > > Thanks,
    > > Shawn
    > >
    > >

    >
    > Try dataTable2.ImportRow method instead...
    >
    > --
    > Craig Deelsnyder
    > Microsoft MVP - ASP/ASP.NET
     
    Shawn, Mar 9, 2005
    #3
  4. Why bother? You can use the .Select method to return an array of DataRows
    in any order you want. At the very least, you could add a dummy column to
    your
    table at runtime as a column to sort on and populate based on your business
    logic.

    --
    2005 Microsoft MVP C#
    Robbe Morris
    http://www.robbemorris.com
    http://www.masterado.net/home/listings.aspx



    "Shawn" <> wrote in message
    news:...
    > Yes, but what do I have to do to get the new DataRow into index 5 in the
    > DataTable?
    >
    > Shawn
    >
    >
    > "Craig Deelsnyder" <cdeelsny@NO_SPAM_4_MEyahoo.com> wrote in message
    > news:#...
    >> Shawn wrote:
    >> > Hi. I have 2 DataTables, dataTable1 and dataTable2. dataTable1 contains

    > a
    >> > DataRow that I need to insert at index 5 in dataTable2. Here is my

    > code:
    >> >
    >> > For Each dataRow1 In dataTable1.Rows
    >> > If dataRow1.Item("id") = "3353" Then
    >> > dataRow2 = dataRow1
    >> > End If
    >> > Next
    >> >
    >> >
    >> > dataTable2.Rows.InsertAt(dataRow2, 5)
    >> >
    >> > The line above gives me this error: System.ArgumentException: This row
    >> > already belongs to another table.
    >> > What is the right way to do this?
    >> >
    >> > Thanks,
    >> > Shawn
    >> >
    >> >

    >>
    >> Try dataTable2.ImportRow method instead...
    >>
    >> --
    >> Craig Deelsnyder
    >> Microsoft MVP - ASP/ASP.NET

    >
    >
     
    Robbe Morris [C# MVP], Mar 9, 2005
    #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,425
    Kevin Spencer
    Aug 15, 2003
  2. ypul
    Replies:
    3
    Views:
    41,790
  3. Rob G
    Replies:
    0
    Views:
    2,040
    Rob G
    Jun 7, 2004
  4. Ersin Gençtürk
    Replies:
    1
    Views:
    3,548
    Ersin Gençtürk
    Oct 6, 2004
  5. Bennett Haselton
    Replies:
    0
    Views:
    697
    Bennett Haselton
    Nov 11, 2004
Loading...

Share This Page