Home All Groups Group Topic Archive Search About

Copying DataRows to another DataTable

Author
19 Nov 2005 1:33 AM
Nathan Sokalski
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.vb: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
njsokal***@hotmail.com
http://www.nathansokalski.com/

Author
19 Nov 2005 2:18 AM
Scott M.
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





Show quote
>
>
> 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.vb: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
> njsokal***@hotmail.com
> http://www.nathansokalski.com/
>
Author
21 Nov 2005 6:31 AM
PaulNaude
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.

Show quote
"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.vb: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
> > njsokal***@hotmail.com
> > http://www.nathansokalski.com/
> >
>
>
>
Author
21 Nov 2005 3:20 PM
Scott M.
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.


Show quote
"PaulNaude" <PaulNa***@discussions.microsoft.com> wrote in message
news:0F1F3262-7264-468C-A1B6-0ECF4E261013@microsoft.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.vb: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
>> > njsokal***@hotmail.com
>> > http://www.nathansokalski.com/
>> >
>>
>>
>>
Author
22 Nov 2005 7:09 AM
PaulNaude
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?

Show quote
"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" <PaulNa***@discussions.microsoft.com> wrote in message
> news:0F1F3262-7264-468C-A1B6-0ECF4E261013@microsoft.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.vb: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
> >> > njsokal***@hotmail.com
> >> > http://www.nathansokalski.com/
> >> >
> >>
> >>
> >>
>
>
>
Author
19 Nov 2005 8:17 AM
Cor Ligthert [MVP]
Nathan,

The methode for that is datatable.importrow

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

(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

AddThis Social Bookmark Button