Professional Web Applications Themes

Copying DataRows to another DataTable - ASP.NET Data Grid Control

I am writing an ASP.NET application in which I need to copy DataRows from one DataTable to another. When I use code such as the following: temprows = nodes.Select("state='PA'") temptable.Clear() For Each row As DataRow In temprows temptable.Rows.Add(row) Next I recieve the following error: [ArgumentException: This row already belongs to another table.] System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32 pos) +450 System.Data.DataRowCollection.Add(DataRow row) +14 WebApplication1.WebForm2.Page_Load(Object sender, EventArgs e) in C:\Inetpub\wwwroot\WebApplication1\WebForm2.aspx.v b:43 System.Web.UI.Control.OnLoad(EventArgs e) +67 System.Web.UI.Control.LoadRecursive() +35 System.Web.UI.Page.ProcessRequestMain() +739 I could not find any method that is used for copying a DataRow (like the DataTable class has a Copy() method). Is there any ...

  1. #1

    Default Copying DataRows to another DataTable

    I am writing an ASP.NET application in which I need to copy DataRows from
    one DataTable to another. When I use code such as the following:


    temprows = nodes.Select("state='PA'")
    temptable.Clear()
    For Each row As DataRow In temprows
    temptable.Rows.Add(row)
    Next


    I recieve the following error:

    [ArgumentException: This row already belongs to another table.]
    System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32 pos)
    +450
    System.Data.DataRowCollection.Add(DataRow row) +14
    WebApplication1.WebForm2.Page_Load(Object sender, EventArgs e) in
    C:\Inetpub\wwwroot\WebApplication1\WebForm2.aspx.v b:43
    System.Web.UI.Control.OnLoad(EventArgs e) +67
    System.Web.UI.Control.LoadRecursive() +35
    System.Web.UI.Page.ProcessRequestMain() +739


    I could not find any method that is used for copying a DataRow (like the
    DataTable class has a Copy() method). Is there any way to copy individual
    DataRows without manually reading each property and creating a new DataRow?
    Thanks.
    --
    Nathan Sokalski
    [email]njsokalskihotmail.com[/email]
    [url]http://www.nathansokalski.com/[/url]


    Nathan Sokalski Guest

  2. #2

    Default Re: Copying DataRows to another DataTable

    temprows = nodes.Select("state='PA'")
    temptable.Clear()
    For Each row As DataRow In temprows
    Dim newRow As New DataRow = row
    temptable.Rows.Add(row)
    Next




    >
    >
    > I recieve the following error:
    >
    > [ArgumentException: This row already belongs to another table.]
    > System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32
    > pos) +450
    > System.Data.DataRowCollection.Add(DataRow row) +14
    > WebApplication1.WebForm2.Page_Load(Object sender, EventArgs e) in
    > C:\Inetpub\wwwroot\WebApplication1\WebForm2.aspx.v b:43
    > System.Web.UI.Control.OnLoad(EventArgs e) +67
    > System.Web.UI.Control.LoadRecursive() +35
    > System.Web.UI.Page.ProcessRequestMain() +739
    >
    >
    > I could not find any method that is used for copying a DataRow (like the
    > DataTable class has a Copy() method). Is there any way to copy individual
    > DataRows without manually reading each property and creating a new
    > DataRow? Thanks.
    > --
    > Nathan Sokalski
    > [email]njsokalskihotmail.com[/email]
    > [url]http://www.nathansokalski.com/[/url]
    >

    Scott M. Guest

  3. #3

    Default Re: Copying DataRows to another DataTable

    Nathan,

    The methode for that is datatable.importrow

    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatatableclassimportrowtopic.asp[/url]

    (A datarow holds in its properties the table that is used to create it. In
    that are the columndescription. Therefore it can not reference to more than
    oner datatable.)

    I hope this helps,

    Cor


    Cor Ligthert [MVP] Guest

  4. #4

    Default Re: Copying DataRows to another DataTable

    Is it possible to use this method to copy and paste rows from and to the same
    datagrid and if so, how do one get the selected rows for the copy action?

    The user simply need to duplicate some records in a datagrid (source =
    filtered dataview) and paste it over other existing records. (the user will
    then edit some of these values). Typically, only the selected cells need to
    be copied since the primary key columns need to be left unchanged.

    "Scott M." wrote:
    > temprows = nodes.Select("state='PA'")
    > temptable.Clear()
    > For Each row As DataRow In temprows
    > Dim newRow As New DataRow = row
    > temptable.Rows.Add(row)
    > Next
    >
    >
    >
    >
    >
    > >
    > >
    > > I recieve the following error:
    > >
    > > [ArgumentException: This row already belongs to another table.]
    > > System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32
    > > pos) +450
    > > System.Data.DataRowCollection.Add(DataRow row) +14
    > > WebApplication1.WebForm2.Page_Load(Object sender, EventArgs e) in
    > > C:\Inetpub\wwwroot\WebApplication1\WebForm2.aspx.v b:43
    > > System.Web.UI.Control.OnLoad(EventArgs e) +67
    > > System.Web.UI.Control.LoadRecursive() +35
    > > System.Web.UI.Page.ProcessRequestMain() +739
    > >
    > >
    > > I could not find any method that is used for copying a DataRow (like the
    > > DataTable class has a Copy() method). Is there any way to copy individual
    > > DataRows without manually reading each property and creating a new
    > > DataRow? Thanks.
    > > --
    > > Nathan Sokalski
    > > [email]njsokalskihotmail.com[/email]
    > > [url]http://www.nathansokalski.com/[/url]
    > >
    >
    >
    >
    PaulNaude Guest

  5. #5

    Default Re: Copying DataRows to another DataTable

    If you are asking if you can copy a row displayed in a datagrid and then
    paste that row back into the datagrids source so that it will overwright the
    first row, then my response is, you are going about this incorrectly. What
    it seems that you are describing is basically just editing value from a row
    in a datagrid. In this case, you need to use the PK of the row being
    displayed in the grid to locate the same underlying row of data in the grids
    datasource (datatable). Once you've done that, you can just update that
    rows values and ultimately, the original datasource.


    "PaulNaude" <PaulNaudediscussions.microsoft.com> wrote in message
    news:0F1F3262-7264-468C-A1B6-0ECF4E261013microsoft.com...
    > Is it possible to use this method to copy and paste rows from and to the
    > same
    > datagrid and if so, how do one get the selected rows for the copy action?
    >
    > The user simply need to duplicate some records in a datagrid (source =
    > filtered dataview) and paste it over other existing records. (the user
    > will
    > then edit some of these values). Typically, only the selected cells need
    > to
    > be copied since the primary key columns need to be left unchanged.
    >
    > "Scott M." wrote:
    >
    >> temprows = nodes.Select("state='PA'")
    >> temptable.Clear()
    >> For Each row As DataRow In temprows
    >> Dim newRow As New DataRow = row
    >> temptable.Rows.Add(row)
    >> Next
    >>
    >>
    >>
    >>
    >>
    >> >
    >> >
    >> > I recieve the following error:
    >> >
    >> > [ArgumentException: This row already belongs to another table.]
    >> > System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32
    >> > pos) +450
    >> > System.Data.DataRowCollection.Add(DataRow row) +14
    >> > WebApplication1.WebForm2.Page_Load(Object sender, EventArgs e) in
    >> > C:\Inetpub\wwwroot\WebApplication1\WebForm2.aspx.v b:43
    >> > System.Web.UI.Control.OnLoad(EventArgs e) +67
    >> > System.Web.UI.Control.LoadRecursive() +35
    >> > System.Web.UI.Page.ProcessRequestMain() +739
    >> >
    >> >
    >> > I could not find any method that is used for copying a DataRow (like
    >> > the
    >> > DataTable class has a Copy() method). Is there any way to copy
    >> > individual
    >> > DataRows without manually reading each property and creating a new
    >> > DataRow? Thanks.
    >> > --
    >> > Nathan Sokalski
    >> > [email]njsokalskihotmail.com[/email]
    >> > [url]http://www.nathansokalski.com/[/url]
    >> >
    >>
    >>
    >>

    Scott M. Guest

  6. #6

    Default Re: Copying DataRows to another DataTable

    The problem is that I don't know how to provide the user with copy and paste
    functionality in a datagrid. In my specific case I hide the primary key
    columns and by selecting values in other datagrids, the grid in question is
    filtered. The user need to enter values for all possible combinations of
    selected filter values, but may find it usefull to copy and paste ranges of
    cells which are identical or simmilar (which will radically reduce the number
    of cell by cell entering of values).

    Is this an option, or is the programming too advanced?

    "Scott M." wrote:
    > If you are asking if you can copy a row displayed in a datagrid and then
    > paste that row back into the datagrids source so that it will overwright the
    > first row, then my response is, you are going about this incorrectly. What
    > it seems that you are describing is basically just editing value from a row
    > in a datagrid. In this case, you need to use the PK of the row being
    > displayed in the grid to locate the same underlying row of data in the grids
    > datasource (datatable). Once you've done that, you can just update that
    > rows values and ultimately, the original datasource.
    >
    >
    > "PaulNaude" <PaulNaudediscussions.microsoft.com> wrote in message
    > news:0F1F3262-7264-468C-A1B6-0ECF4E261013microsoft.com...
    > > Is it possible to use this method to copy and paste rows from and to the
    > > same
    > > datagrid and if so, how do one get the selected rows for the copy action?
    > >
    > > The user simply need to duplicate some records in a datagrid (source =
    > > filtered dataview) and paste it over other existing records. (the user
    > > will
    > > then edit some of these values). Typically, only the selected cells need
    > > to
    > > be copied since the primary key columns need to be left unchanged.
    > >
    > > "Scott M." wrote:
    > >
    > >> temprows = nodes.Select("state='PA'")
    > >> temptable.Clear()
    > >> For Each row As DataRow In temprows
    > >> Dim newRow As New DataRow = row
    > >> temptable.Rows.Add(row)
    > >> Next
    > >>
    > >>
    > >>
    > >>
    > >>
    > >> >
    > >> >
    > >> > I recieve the following error:
    > >> >
    > >> > [ArgumentException: This row already belongs to another table.]
    > >> > System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32
    > >> > pos) +450
    > >> > System.Data.DataRowCollection.Add(DataRow row) +14
    > >> > WebApplication1.WebForm2.Page_Load(Object sender, EventArgs e) in
    > >> > C:\Inetpub\wwwroot\WebApplication1\WebForm2.aspx.v b:43
    > >> > System.Web.UI.Control.OnLoad(EventArgs e) +67
    > >> > System.Web.UI.Control.LoadRecursive() +35
    > >> > System.Web.UI.Page.ProcessRequestMain() +739
    > >> >
    > >> >
    > >> > I could not find any method that is used for copying a DataRow (like
    > >> > the
    > >> > DataTable class has a Copy() method). Is there any way to copy
    > >> > individual
    > >> > DataRows without manually reading each property and creating a new
    > >> > DataRow? Thanks.
    > >> > --
    > >> > Nathan Sokalski
    > >> > [email]njsokalskihotmail.com[/email]
    > >> > [url]http://www.nathansokalski.com/[/url]
    > >> >
    > >>
    > >>
    > >>
    >
    >
    >
    PaulNaude Guest

Similar Threads

  1. Question DataTable
    By Unregistered in forum Brainstorming Area
    Replies: 0
    Last Post: July 28th, 05:38 AM
  2. huh can`t pass datatable ????
    By M.Posseth in forum ASP.NET Web Services
    Replies: 1
    Last Post: February 24th, 06:01 PM
  3. Changing datarows appearance at databind NOT CSS
    By JemPower in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: September 11th, 08:42 AM
  4. how to get row if in datatable?
    By Sean in forum ASP.NET Data Grid Control
    Replies: 3
    Last Post: August 7th, 02:57 PM
  5. How to get row index for datatable
    By Prasad in forum ASP.NET
    Replies: 0
    Last Post: August 7th, 07:19 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139